Files
backend/src/notifications/notifications.controller.ts

120 lines
3.4 KiB
TypeScript
Raw Normal View History

2026-02-24 22:36:42 +05:30
import {
Controller,
Get,
2026-02-24 22:36:42 +05:30
Post,
Patch,
2026-02-24 22:36:42 +05:30
Delete,
Body,
Param,
Query,
2026-02-24 22:36:42 +05:30
UseGuards,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiResponse,
ApiBearerAuth,
ApiQuery,
2026-02-24 22:36:42 +05:30
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards';
import { CurrentUser } from '../auth/decorators';
import { NotificationsService } from './notifications.service';
import { RegisterTokenDto, RemoveTokenDto } from './dto/register-token.dto';
@ApiTags('Notifications')
@Controller('notifications')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('JWT-auth')
export class NotificationsController {
constructor(
private readonly notificationsService: NotificationsService,
) {}
// ==========================================
// IN-APP NOTIFICATIONS
// ==========================================
@Get()
@ApiOperation({ summary: 'Get notifications list' })
@ApiQuery({ name: 'filter', required: false, enum: ['all', 'unread'] })
@ApiQuery({ name: 'page', required: false, type: Number })
@ApiQuery({ name: 'limit', required: false, type: Number })
@ApiResponse({ status: 200, description: 'Notifications list with pagination' })
async getNotifications(
@CurrentUser('id') userId: string,
@Query('filter') filter?: 'all' | 'unread',
@Query('page') page?: string,
@Query('limit') limit?: string,
) {
return this.notificationsService.getNotifications(
userId,
filter || 'all',
page ? parseInt(page, 10) : 1,
limit ? parseInt(limit, 10) : 20,
);
}
@Get('unread-count')
@ApiOperation({ summary: 'Get unread notification count' })
@ApiResponse({ status: 200, description: 'Unread count' })
async getUnreadCount(@CurrentUser('id') userId: string) {
const count = await this.notificationsService.getUnreadCount(userId);
return { unreadCount: count };
}
@Patch(':id/read')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Mark a notification as read' })
@ApiResponse({ status: 200, description: 'Notification marked as read' })
async markAsRead(
@CurrentUser('id') userId: string,
@Param('id') notificationId: string,
) {
await this.notificationsService.markAsRead(userId, notificationId);
return null;
}
@Patch('read-all')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Mark all notifications as read' })
@ApiResponse({ status: 200, description: 'All notifications marked as read' })
async markAllAsRead(@CurrentUser('id') userId: string) {
await this.notificationsService.markAllAsRead(userId);
return null;
}
// ==========================================
// FCM TOKEN MANAGEMENT
// ==========================================
2026-02-24 22:36:42 +05:30
@Post('fcm-token')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Register FCM token for push notifications' })
@ApiResponse({ status: 200, description: 'Token registered' })
async registerToken(
@CurrentUser('id') userId: string,
@Body() dto: RegisterTokenDto,
) {
await this.notificationsService.registerToken(
userId,
dto.token,
dto.device,
);
return null;
}
@Delete('fcm-token')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Remove FCM token' })
@ApiResponse({ status: 200, description: 'Token removed' })
async removeToken(
@CurrentUser('id') userId: string,
@Body() dto: RemoveTokenDto,
) {
await this.notificationsService.removeToken(userId, dto.token);
return null;
}
}