read notif

This commit is contained in:
2025-12-15 09:21:55 +03:30
parent 996cd0016c
commit 6bcfda19c9
2 changed files with 37 additions and 10 deletions
@@ -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)
@@ -119,12 +119,29 @@ export class NotificationService {
);
}
async removeNotification(id: string, restaurantId: string): Promise<void> {
const notification = await this.em.findOne(Notification, { id, restaurant: { id: restaurantId } });
async readNotificationAdmin(id: string, adminId: string, restaurantId: string): Promise<void> {
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<void> {
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<Notification[]> {