Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Put, | |
| Delete, | |
| Patch, | |
| Body, | |
| Param, | |
| Query, | |
| Req, | |
| UseGuards, | |
| ParseIntPipe, | |
| HttpCode, | |
| HttpStatus, | |
| } from '@nestjs/common'; | |
| import { | |
| ApiTags, | |
| ApiOperation, | |
| ApiParam, | |
| ApiBody, | |
| ApiResponse, | |
| ApiBearerAuth, | |
| } from '@nestjs/swagger'; | |
| import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; | |
| import { RolesGuard } from '../../auth/guards/roles.guard'; | |
| import { Roles } from '../../auth/roles.decorator'; | |
| import { RoleName } from '../../auth/entities/role.entity'; | |
| import { NotificationsService } from '../services/notifications.service'; | |
| import { | |
| SendNotificationDto, | |
| NotificationQueryDto, | |
| UpdatePreferencesDto, | |
| RegisterDeviceTokenDto, | |
| UnregisterDeviceTokenDto, | |
| } from '../dto'; | |
| ('Notifications') | |
| ('JWT-auth') | |
| ('api/notifications') | |
| (JwtAuthGuard, RolesGuard) | |
| export class NotificationsController { | |
| constructor(private readonly notificationsService: NotificationsService) {} | |
| // ============ NOTIFICATIONS ============ | |
| () | |
| ({ | |
| summary: 'List notifications', | |
| description: | |
| "List current user's notifications with optional filters. Supports pagination.", | |
| }) | |
| ({ status: 200, description: 'Notifications retrieved' }) | |
| async findAll(() query: NotificationQueryDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.findAll(userId, query); | |
| } | |
| ('unread-count') | |
| ({ | |
| summary: 'Get unread count', | |
| description: 'Get the count of unread notifications for the current user.', | |
| }) | |
| ({ | |
| status: 200, | |
| description: 'Unread count', | |
| schema: { example: { count: 5 } }, | |
| }) | |
| async getUnreadCount(() req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.getUnreadCount(userId); | |
| } | |
| ('read-all') | |
| ({ | |
| summary: 'Mark all as read', | |
| description: "Mark all of the current user's notifications as read.", | |
| }) | |
| ({ | |
| status: 200, | |
| description: 'All notifications marked as read', | |
| schema: { example: { affected: 10 } }, | |
| }) | |
| async markAllAsRead(() req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.markAllAsRead(userId); | |
| } | |
| ('clear-all') | |
| ({ | |
| summary: 'Clear all notifications', | |
| description: 'Delete all notifications for the current user.', | |
| }) | |
| ({ | |
| status: 200, | |
| description: 'All notifications cleared', | |
| schema: { example: { affected: 10 } }, | |
| }) | |
| async clearAll(() req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.clearAll(userId); | |
| } | |
| ('clear-read') | |
| ({ | |
| summary: 'Clear read notifications', | |
| description: 'Delete all read notifications for the current user.', | |
| }) | |
| ({ | |
| status: 200, | |
| description: 'Read notifications cleared', | |
| schema: { example: { affected: 10 } }, | |
| }) | |
| async clearRead(() req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.clearRead(userId); | |
| } | |
| ('preferences') | |
| ({ | |
| summary: 'Get notification preferences', | |
| description: | |
| "Get the current user's notification preferences. Creates default preferences if none exist.", | |
| }) | |
| ({ status: 200, description: 'Preferences retrieved' }) | |
| async getPreferences(() req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.getPreferences(userId); | |
| } | |
| ('preferences') | |
| ({ | |
| summary: 'Update notification preferences', | |
| description: | |
| "Update the current user's notification preferences (email, push, quiet hours, etc.).", | |
| }) | |
| ({ type: UpdatePreferencesDto }) | |
| ({ status: 200, description: 'Preferences updated' }) | |
| async updatePreferences(() dto: UpdatePreferencesDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.updatePreferences(userId, dto); | |
| } | |
| ('device-tokens') | |
| ({ | |
| summary: 'Register Android push token', | |
| description: | |
| "Register or refresh the current user's Firebase Cloud Messaging token. Existing web/socket notification behavior is unchanged.", | |
| }) | |
| ({ type: RegisterDeviceTokenDto }) | |
| ({ status: 201, description: 'Device token registered' }) | |
| async registerDeviceToken( | |
| () dto: RegisterDeviceTokenDto, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.registerDeviceToken(userId, dto); | |
| } | |
| ('device-tokens/unregister') | |
| (HttpStatus.OK) | |
| ({ | |
| summary: 'Unregister Android push token', | |
| description: | |
| 'Deactivate one Firebase Cloud Messaging token for the current user.', | |
| }) | |
| ({ type: UnregisterDeviceTokenDto }) | |
| ({ status: 200, description: 'Device token deactivated' }) | |
| async unregisterDeviceTokenWithPost( | |
| () dto: UnregisterDeviceTokenDto, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.unregisterDeviceToken(userId, dto); | |
| } | |
| ('device-tokens') | |
| ({ | |
| summary: 'Delete Android push token', | |
| description: | |
| 'Deactivate one Firebase Cloud Messaging token for the current user.', | |
| }) | |
| ({ type: UnregisterDeviceTokenDto }) | |
| ({ status: 200, description: 'Device token deactivated' }) | |
| async unregisterDeviceToken( | |
| () dto: UnregisterDeviceTokenDto, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.unregisterDeviceToken(userId, dto); | |
| } | |
| (':id/read') | |
| ({ | |
| summary: 'Mark notification as read', | |
| description: 'Mark a single notification as read.', | |
| }) | |
| ({ name: 'id', description: 'Notification ID', example: 1 }) | |
| ({ status: 200, description: 'Notification marked as read' }) | |
| ({ status: 404, description: 'Notification not found' }) | |
| async markAsRead(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.markAsRead(userId, id); | |
| } | |
| (':id') | |
| ({ | |
| summary: 'Delete notification', | |
| description: 'Delete a single notification.', | |
| }) | |
| ({ name: 'id', description: 'Notification ID', example: 1 }) | |
| ({ status: 200, description: 'Notification deleted' }) | |
| ({ status: 404, description: 'Notification not found' }) | |
| async delete(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.notificationsService.delete(userId, id); | |
| } | |
| // ============ SEND (Admin Only) ============ | |
| ('send') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| (HttpStatus.CREATED) | |
| ({ | |
| summary: 'Send notification (Admin)', | |
| description: | |
| 'Send a notification to one or more users. Requires ADMIN or IT_ADMIN role.', | |
| }) | |
| ({ type: SendNotificationDto }) | |
| ({ status: 201, description: 'Notifications sent' }) | |
| ({ | |
| status: 403, | |
| description: 'Forbidden - Admin access required', | |
| }) | |
| async send(() dto: SendNotificationDto) { | |
| return this.notificationsService.send(dto); | |
| } | |
| } | |