-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/api-46/notifications-module #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6486530
add notification
Dazzlin00 9b7accc
Merge branch 'dev' into feature/API-46/Notifications-Module
Dazzlin00 da3819d
notification module working
Dazzlin00 b845726
Merge branch 'dev' into feature/API-46/Notifications-Module
Dazzlin00 ac59359
reorganized
Dazzlin00 a24b171
Fix
Dazzlin00 05edca7
Merge branch 'dev' into feature/API-46/Notifications-Module
Dazzlin00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { | ||
| Controller, | ||
| Get, | ||
| Post, | ||
| Param, | ||
| UseGuards, | ||
| Req, | ||
| HttpCode, | ||
| HttpStatus, | ||
| ParseUUIDPipe, | ||
| } from '@nestjs/common'; | ||
| import { NotificationService } from '../services/notification.service'; | ||
| import { AuthGuard, CustomRequest } from 'src/auth/auth.guard'; | ||
| import { ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger'; | ||
| import { UserRole } from 'src/user/entities/user.entity'; | ||
|
|
||
| @Controller('notification') | ||
| export class NotificationController { | ||
| constructor(private readonly notificationService: NotificationService) {} | ||
|
|
||
| @Get() | ||
| @UseGuards(AuthGuard) | ||
| @ApiBearerAuth() | ||
| @ApiOperation({ summary: 'List notifications' }) | ||
| @ApiResponse({ | ||
| status: HttpStatus.OK, | ||
| description: 'Notification list', | ||
| }) | ||
| async getNotifications(@Req() req: CustomRequest) { | ||
| if (req.user.role === UserRole.ADMIN) { | ||
| return await this.notificationService.getAllNotifications(); | ||
| } else { | ||
| return await this.notificationService.getUserNotifications(req.user.id); | ||
| } | ||
| } | ||
| @Post(':orderId/read') | ||
| @UseGuards(AuthGuard) | ||
| @ApiBearerAuth() | ||
| @HttpCode(HttpStatus.OK) | ||
| @ApiOperation({ summary: 'Mark notification as read' }) | ||
| @ApiResponse({ | ||
| status: HttpStatus.OK, | ||
| description: 'Notification marked as read', | ||
| }) | ||
| async markAsRead( | ||
| @Param('orderId', ParseUUIDPipe) orderId: string, | ||
| @Req() req: CustomRequest, | ||
| ): Promise<void> { | ||
| await this.notificationService.markAsReadAsCustomer(orderId, req.user.id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { ApiProperty } from '@nestjs/swagger'; | ||
| import { | ||
| IsBoolean, | ||
| IsOptional, | ||
| IsString, | ||
| IsUUID, | ||
| IsNotEmpty, | ||
| } from 'class-validator'; | ||
|
|
||
| export class NotificationDTO { | ||
| @ApiProperty({ description: 'Notification message' }) | ||
| @IsNotEmpty() | ||
| @IsString() | ||
| message: string; | ||
|
|
||
| @ApiProperty({ | ||
| description: 'Indicates if the notification has been read', | ||
| default: false, | ||
| }) | ||
| @IsNotEmpty() | ||
| @IsBoolean() | ||
| isRead: boolean; | ||
|
|
||
| @ApiProperty({ | ||
| description: 'Identifier of the order associated with the notification', | ||
| required: false, | ||
| nullable: true, | ||
| }) | ||
| @IsOptional() | ||
| @IsUUID() | ||
| orderId?: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm'; | ||
| import { BaseModel } from 'src/utils/entity'; | ||
| import { Order } from 'src/order/entities/order.entity'; | ||
|
|
||
| @Entity() | ||
| export class Notification extends BaseModel { | ||
| @ManyToOne(() => Order, { onDelete: 'CASCADE' }) | ||
| @JoinColumn({ name: 'order_id' }) | ||
| order: Order; | ||
|
|
||
| @Column({ type: 'text' }) | ||
| message: string; | ||
|
|
||
| @Column({ name: 'is_read', type: 'boolean', default: false }) | ||
| isRead: boolean; | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/notification/migrations/1744646198491-create-notification-migration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class CreateNotificationMigration1744646198491 | ||
| implements MigrationInterface | ||
| { | ||
| name = 'CreateNotificationMigration1744646198491'; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "notification" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, "message" text NOT NULL, "is_read" boolean NOT NULL DEFAULT false, "order_id" uuid, CONSTRAINT "PK_705b6c7cdf9b2c2ff7ac7872cb7" PRIMARY KEY ("id"))`, | ||
| ); | ||
| await queryRunner.query( | ||
| `ALTER TABLE "notification" ADD CONSTRAINT "FK_3ea5cd8a1de9cbf90c86dd0582c" FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
| ); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "notification" DROP CONSTRAINT "FK_3ea5cd8a1de9cbf90c86dd0582c"`, | ||
| ); | ||
| await queryRunner.query(`DROP TABLE "notification"`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { TypeOrmModule } from '@nestjs/typeorm'; | ||
| import { Notification } from './entities/notification.entity'; | ||
| import { NotificationController } from '../notification/controllers/notification.controller'; | ||
| import { NotificationService } from '../notification/services/notification.service'; | ||
| import { NotificationSubscriber } from '../notification/subscribers/notification.subscriber'; | ||
| import { AuthModule } from 'src/auth/auth.module'; | ||
|
|
||
| @Module({ | ||
| imports: [TypeOrmModule.forFeature([Notification]), AuthModule], | ||
| controllers: [NotificationController], | ||
| providers: [NotificationService, NotificationSubscriber], | ||
| }) | ||
| export class NotificationModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Injectable, ForbiddenException } from '@nestjs/common'; | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
| import { Repository } from 'typeorm'; | ||
| import { Notification } from '../entities/notification.entity'; | ||
|
|
||
| @Injectable() | ||
| export class NotificationService { | ||
| constructor( | ||
| @InjectRepository(Notification) | ||
| private readonly notificationRepository: Repository<Notification>, | ||
| ) {} | ||
|
|
||
| async getAllNotifications() { | ||
| return await this.notificationRepository.find({ relations: ['order'] }); | ||
| } | ||
| async getUserNotifications(userId: string) { | ||
| return await this.notificationRepository | ||
| .createQueryBuilder('notification') | ||
| .innerJoinAndSelect('notification.order', 'order') | ||
| .where('order.user.id = :userId', { userId }) | ||
| .orderBy('notification.createdAt', 'DESC') | ||
| .getMany(); | ||
| } | ||
|
|
||
| async markAsReadAsCustomer(orderId: string, userId: string): Promise<void> { | ||
| const notification = await this.notificationRepository.findOne({ | ||
| where: { order: { id: orderId, user: { id: userId } } }, | ||
| relations: ['order'], | ||
| }); | ||
| if (!notification) { | ||
| throw new ForbiddenException('Notification not found or no access'); | ||
| } | ||
| notification.isRead = true; | ||
| await this.notificationRepository.save(notification); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { | ||
| EntitySubscriberInterface, | ||
| EventSubscriber, | ||
| UpdateEvent, | ||
| } from 'typeorm'; | ||
| import { Order } from 'src/order/entities/order.entity'; | ||
| import { Notification } from '../entities/notification.entity'; | ||
|
|
||
| @EventSubscriber() | ||
| export class NotificationSubscriber | ||
| implements EntitySubscriberInterface<Order> | ||
| { | ||
| listenTo() { | ||
| return Order; | ||
| } | ||
|
|
||
| async afterUpdate(event: UpdateEvent<Order>) { | ||
| const updatedOrder = event.entity as Order | undefined; | ||
| console.log('Updated Order:', updatedOrder); | ||
| if (!updatedOrder) return; | ||
|
|
||
| const message = `The order ${updatedOrder.id} has been updated to ${updatedOrder.status}`; | ||
| const notificationRepository = event.manager.getRepository(Notification); | ||
|
|
||
| let notification = await notificationRepository.findOne({ | ||
| where: { order: { id: updatedOrder.id } }, | ||
| }); | ||
|
|
||
| if (notification) { | ||
| notification.message = message; | ||
| notification.isRead = false; | ||
| } else { | ||
| notification = notificationRepository.create({ | ||
| order: { id: updatedOrder.id } as Order, | ||
| message, | ||
| isRead: false, | ||
| }); | ||
| } | ||
|
|
||
| await notificationRepository.save(notification); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.