diff --git a/src/modules/notifications/controllers/notifications.controller.ts b/src/modules/notifications/controllers/notifications.controller.ts index dee91cd..6d3eda8 100644 --- a/src/modules/notifications/controllers/notifications.controller.ts +++ b/src/modules/notifications/controllers/notifications.controller.ts @@ -47,6 +47,16 @@ export class NotificationsController { ); } + @UseGuards(AuthGuard) + @ApiBearerAuth() + @Get('public/notifications/unseen-count') + @ApiOperation({ summary: 'Get unseen notifications count for user' }) + @ApiHeader(API_HEADER_SLUG) + async getUserUnseenCount(@UserId() userId: string, @RestId() restaurantId: string) { + const count = await this.notificationService.countUnseenByUserAndRestaurant(userId, restaurantId); + return { count }; + } + @UseGuards(AuthGuard) @ApiBearerAuth() @Put('public/notifications/:id') @@ -79,7 +89,16 @@ export class NotificationsController { @Query('status') status?: 'seen' | 'unseen', ) { const parsedLimit = limit ? parseInt(limit.toString(), 10) : 50; - return await this.notificationService.findByRestaurant(restaurantId, parsedLimit, cursor, status); + return await this.notificationService.findByRestaurant(restaurantId, adminId, parsedLimit, cursor, status); + } + + @UseGuards(AdminAuthGuard) + @ApiBearerAuth() + @Get('admin/notifications/unseen-count') + @ApiOperation({ summary: 'Get unseen notifications count for admin' }) + async getAdminUnseenCount(@AdminId() adminId: string, @RestId() restaurantId: string) { + const count = await this.notificationService.countUnseenByRestaurant(adminId, restaurantId); + return { count }; } @UseGuards(AdminAuthGuard) diff --git a/src/modules/notifications/services/notification.service.ts b/src/modules/notifications/services/notification.service.ts index 2aa74f5..527ca87 100644 --- a/src/modules/notifications/services/notification.service.ts +++ b/src/modules/notifications/services/notification.service.ts @@ -98,12 +98,14 @@ export class NotificationService { async findByRestaurant( restaurantId: string, + adminId: string, limit = 50, cursor?: string, status?: 'seen' | 'unseen', ): Promise<{ data: Notification[]; nextCursor: string | null }> { const where: FilterQuery = { restaurant: { id: restaurantId }, + admin: { id: adminId }, }; // Filter by status (seen/unseen) @@ -224,4 +226,22 @@ export class NotificationService { }, ); } + + async countUnseenByUserAndRestaurant(userId: string, restaurantId: string): Promise { + const where: FilterQuery = { + user: { id: userId }, + restaurant: { id: restaurantId }, + seenAt: null, + }; + return this.em.count(Notification, where); + } + + async countUnseenByRestaurant(adminId: string, restaurantId: string): Promise { + const where: FilterQuery = { + admin: { id: adminId }, + restaurant: { id: restaurantId }, + seenAt: null, + }; + return this.em.count(Notification, where); + } }