Spaces:
Running
Running
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Delete, | |
| Body, | |
| Param, | |
| Query, | |
| UseGuards, | |
| Request, | |
| HttpCode, | |
| HttpStatus, | |
| } from '@nestjs/common'; | |
| import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; | |
| import { NotificationsService } from './notifications.service'; | |
| import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; | |
| import { QueryNotificationsDto, MarkAsReadDto } from './dto'; | |
| interface RequestWithUser extends Request { | |
| user: { | |
| id: string; | |
| email: string; | |
| }; | |
| } | |
| ('Notifications') | |
| (JwtAuthGuard) | |
| () | |
| ('notifications') | |
| export class NotificationsController { | |
| constructor(private readonly notificationsService: NotificationsService) {} | |
| /** | |
| * GET /notifications | |
| * Get user's notifications | |
| */ | |
| () | |
| (HttpStatus.OK) | |
| async getNotifications( | |
| () query: QueryNotificationsDto, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| const result = await this.notificationsService.getNotifications(req.user.id, query); | |
| return { | |
| success: true, | |
| data: result.notifications, | |
| meta: { | |
| total: result.total, | |
| unreadCount: result.unreadCount, | |
| page: query.page || 1, | |
| limit: query.limit || 20, | |
| }, | |
| }; | |
| } | |
| /** | |
| * GET /notifications/unread-count | |
| * Get unread notification count | |
| */ | |
| ('unread-count') | |
| (HttpStatus.OK) | |
| async getUnreadCount(() req: RequestWithUser): Promise<any> { | |
| const count = await this.notificationsService.getUnreadCount(req.user.id); | |
| return { | |
| success: true, | |
| data: { unreadCount: count }, | |
| }; | |
| } | |
| /** | |
| * GET /notifications/:notificationId | |
| * Get single notification | |
| */ | |
| (':notificationId') | |
| (HttpStatus.OK) | |
| async getNotification( | |
| ('notificationId') notificationId: string, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| const notification = await this.notificationsService.getNotificationById( | |
| notificationId, | |
| req.user.id, | |
| ); | |
| return { | |
| success: true, | |
| data: notification, | |
| }; | |
| } | |
| /** | |
| * POST /notifications/read | |
| * Mark notifications as read | |
| */ | |
| ('read') | |
| (HttpStatus.OK) | |
| async markAsRead( | |
| () dto: MarkAsReadDto, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| const result = await this.notificationsService.markAsRead(req.user.id, dto); | |
| return { | |
| success: true, | |
| message: `${result.markedCount} notification(s) marked as read`, | |
| data: { markedCount: result.markedCount }, | |
| }; | |
| } | |
| /** | |
| * POST /notifications/:notificationId/read | |
| * Mark single notification as read | |
| */ | |
| (':notificationId/read') | |
| (HttpStatus.OK) | |
| async markOneAsRead( | |
| ('notificationId') notificationId: string, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| await this.notificationsService.markOneAsRead(req.user.id, notificationId); | |
| return { | |
| success: true, | |
| message: 'Notification marked as read', | |
| }; | |
| } | |
| /** | |
| * DELETE /notifications/:notificationId | |
| * Delete a notification | |
| */ | |
| (':notificationId') | |
| (HttpStatus.OK) | |
| async deleteNotification( | |
| ('notificationId') notificationId: string, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| await this.notificationsService.deleteNotification(req.user.id, notificationId); | |
| return { | |
| success: true, | |
| message: 'Notification deleted successfully', | |
| }; | |
| } | |
| /** | |
| * DELETE /notifications/read/all | |
| * Delete all read notifications | |
| */ | |
| ('read/all') | |
| (HttpStatus.OK) | |
| async deleteAllRead(() req: RequestWithUser): Promise<any> { | |
| const result = await this.notificationsService.deleteAllRead(req.user.id); | |
| return { | |
| success: true, | |
| message: `${result.deletedCount} read notification(s) deleted`, | |
| data: { deletedCount: result.deletedCount }, | |
| }; | |
| } | |
| } | |