120 lines
3.4 KiB
TypeScript
120 lines
3.4 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Patch,
|
|
Delete,
|
|
Body,
|
|
Param,
|
|
Query,
|
|
UseGuards,
|
|
HttpCode,
|
|
HttpStatus,
|
|
} from '@nestjs/common';
|
|
import {
|
|
ApiTags,
|
|
ApiOperation,
|
|
ApiResponse,
|
|
ApiBearerAuth,
|
|
ApiQuery,
|
|
} 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
|
|
// ==========================================
|
|
|
|
@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;
|
|
}
|
|
}
|