import { Controller, Get, Post, Param, Req, Inject, HttpStatus, Res } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import type { Response } from 'express'; import { NotificationsService } from './NotificationsService'; type AuthenticatedRequest = { user?: { userId: string }; }; @ApiTags('notifications') @Controller('notifications') export class NotificationsController { constructor( @Inject(NotificationsService) private readonly notificationsService: NotificationsService, ) {} @Get('unread') @ApiOperation({ summary: 'Get unread notifications for current user' }) @ApiResponse({ status: 200, description: 'List of unread notifications' }) @ApiResponse({ status: 401, description: 'Unauthorized' }) async getUnreadNotifications( @Req() req: AuthenticatedRequest, @Res() res: Response, ): Promise { const userId = req.user?.userId; if (!userId) { res.status(HttpStatus.UNAUTHORIZED).json({ error: 'Unauthorized' }); return; } const notifications = await this.notificationsService.getUnreadNotifications(userId); res.status(HttpStatus.OK).json({ notifications }); } @Post('read/:notificationId') @ApiOperation({ summary: 'Mark notification as read' }) @ApiResponse({ status: 200, description: 'Notification marked as read' }) @ApiResponse({ status: 401, description: 'Unauthorized' }) async markAsRead( @Param('notificationId') notificationId: string, @Req() req: AuthenticatedRequest, @Res() res: Response, ): Promise { const userId = req.user?.userId; if (!userId) { res.status(HttpStatus.UNAUTHORIZED).json({ error: 'Unauthorized' }); return; } await this.notificationsService.markAsRead(notificationId, userId); res.status(HttpStatus.OK).json({ success: true }); } @Get('all') @ApiOperation({ summary: 'Get all notifications for current user' }) @ApiResponse({ status: 200, description: 'List of all notifications' }) @ApiResponse({ status: 401, description: 'Unauthorized' }) async getAllNotifications( @Req() req: AuthenticatedRequest, @Res() res: Response, ): Promise { const userId = req.user?.userId; if (!userId) { res.status(HttpStatus.UNAUTHORIZED).json({ error: 'Unauthorized' }); return; } const notifications = await this.notificationsService.getAllNotifications(userId); res.status(HttpStatus.OK).json({ notifications }); } }