From 6bcfda19c9f0f79d0d7f9c5807d8b2c935a92ae7 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Mon, 15 Dec 2025 09:21:55 +0330 Subject: [PATCH] read notif --- .../controllers/notifications.controller.ts | 24 +++++++++++++------ .../services/notification.service.ts | 23 +++++++++++++++--- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/modules/notifications/controllers/notifications.controller.ts b/src/modules/notifications/controllers/notifications.controller.ts index fe12957..e7a9ca5 100644 --- a/src/modules/notifications/controllers/notifications.controller.ts +++ b/src/modules/notifications/controllers/notifications.controller.ts @@ -1,13 +1,13 @@ -import { Controller, Post, Get, Body, Param, UseGuards, Query, Patch, Delete } from '@nestjs/common'; +import { Controller, Get, Body, Param, UseGuards, Query, Patch, Put } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery, ApiHeader } from '@nestjs/swagger'; import { NotificationService } from '../services/notification.service'; import { NotificationPreferenceService } from '../services/notification-preference.service'; -import { CreatePreferenceDto } from '../dto/create-preference.dto'; import { AuthGuard } from '../../auth/guards/auth.guard'; import { UserId } from '../../../common/decorators/user-id.decorator'; import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard'; import { RestId } from '../../../common/decorators/rest-id.decorator'; import { UpdatePreferenceDto } from '../dto/update-preference.dto'; +import { AdminId } from 'src/common/decorators/admin-id.decorator'; @ApiTags('notifications') @Controller() @@ -38,6 +38,16 @@ export class NotificationsController { ); } + @UseGuards(AuthGuard) + @ApiBearerAuth() + @Put('public/notifications/:id') + @ApiOperation({ summary: 'Read a notification ' }) + @ApiParam({ name: 'id', description: 'Notification ID' }) + async readNotificationUser(@RestId() restaurantId: string, @Param('id') id: string, @UserId() userId: string) { + await this.notificationService.readNotificationAsUser(id, userId, restaurantId); + return { message: 'Notification read successfully' }; + } + @UseGuards(AdminAuthGuard) @ApiBearerAuth() @Get('admin/notifications') @@ -49,12 +59,12 @@ export class NotificationsController { @UseGuards(AdminAuthGuard) @ApiBearerAuth() - @Delete('admin/notification/:id') - @ApiOperation({ summary: 'Delete a notification ' }) + @Put('admin/notifications/:id') + @ApiOperation({ summary: 'Read a notification ' }) @ApiParam({ name: 'id', description: 'Notification ID' }) - async deleteNotification(@RestId() restaurantId: string, @Param('id') id: string) { - await this.notificationService.removeNotification(id, restaurantId); - return { message: 'Notification deleted successfully' }; + async readNotificationAdmin(@RestId() restaurantId: string, @AdminId() adminId: string, @Param('id') id: string) { + await this.notificationService.readNotificationAdmin(id, adminId, restaurantId); + return { message: 'Notification read successfully' }; } // @UseGuards(AuthGuard) diff --git a/src/modules/notifications/services/notification.service.ts b/src/modules/notifications/services/notification.service.ts index f49bbfc..653589d 100644 --- a/src/modules/notifications/services/notification.service.ts +++ b/src/modules/notifications/services/notification.service.ts @@ -119,12 +119,29 @@ export class NotificationService { ); } - async removeNotification(id: string, restaurantId: string): Promise { - const notification = await this.em.findOne(Notification, { id, restaurant: { id: restaurantId } }); + async readNotificationAdmin(id: string, adminId: string, restaurantId: string): Promise { + const notification = await this.em.findOne(Notification, { + id, + admin: { id: adminId }, + restaurant: { id: restaurantId }, + }); if (!notification) { throw new NotFoundException('Notification not found'); } - await this.em.removeAndFlush(notification as any); + notification.sentAt = new Date(); + await this.em.persistAndFlush(notification); + } + async readNotificationAsUser(id: string, userId: string, restaurantId: string): Promise { + const notification = await this.em.findOne(Notification, { + id, + user: { id: userId }, + restaurant: { id: restaurantId }, + }); + if (!notification) { + throw new NotFoundException('Notification not found'); + } + notification.sentAt = new Date(); + await this.em.persistAndFlush(notification); } async findByRestaurantAndType(restaurantId: string, title: NotifTitleEnum, limit = 50): Promise {