Files
dmenu-api/src/modules/notifications/controllers/notifications.controller.ts
T
2025-12-15 09:21:55 +03:30

189 lines
7.6 KiB
TypeScript

import { Controller, Get, Body, Param, UseGuards, Query, Patch, Put } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery, ApiHeader } from '@nestjs/swagger';
import { NotificationService } from '../services/notification.service';
import { NotificationPreferenceService } from '../services/notification-preference.service';
import { AuthGuard } from '../../auth/guards/auth.guard';
import { UserId } from '../../../common/decorators/user-id.decorator';
import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
import { RestId } from '../../../common/decorators/rest-id.decorator';
import { UpdatePreferenceDto } from '../dto/update-preference.dto';
import { AdminId } from 'src/common/decorators/admin-id.decorator';
@ApiTags('notifications')
@Controller()
export class NotificationsController {
constructor(
private readonly notificationService: NotificationService,
private readonly preferenceService: NotificationPreferenceService,
) {}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/notifications')
@ApiOperation({ summary: 'Get user restaurant notifications' })
@ApiHeader({
name: 'X-Slug',
required: true,
schema: {
type: 'string',
default: 'zhivan',
},
})
@ApiQuery({ name: 'limit', required: false, type: Number })
async getUserNotifications(@UserId() userId: string, @RestId() restaurantId: string, @Query('limit') limit?: number) {
return await this.notificationService.findByUserAndRestaurant(
userId,
restaurantId,
limit ? parseInt(limit.toString(), 10) : 50,
);
}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Put('public/notifications/:id')
@ApiOperation({ summary: 'Read a notification ' })
@ApiParam({ name: 'id', description: 'Notification ID' })
async readNotificationUser(@RestId() restaurantId: string, @Param('id') id: string, @UserId() userId: string) {
await this.notificationService.readNotificationAsUser(id, userId, restaurantId);
return { message: 'Notification read successfully' };
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/notifications')
@ApiOperation({ summary: 'Get Admin restaurant notifications' })
@ApiQuery({ name: 'limit', required: false, type: Number })
async getAdminNotifications(@RestId() restaurantId: string, @Query('limit') limit?: number) {
return await this.notificationService.findByRestaurant(restaurantId, limit ? parseInt(limit.toString(), 30) : 50);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Put('admin/notifications/:id')
@ApiOperation({ summary: 'Read a notification ' })
@ApiParam({ name: 'id', description: 'Notification ID' })
async readNotificationAdmin(@RestId() restaurantId: string, @AdminId() adminId: string, @Param('id') id: string) {
await this.notificationService.readNotificationAdmin(id, adminId, restaurantId);
return { message: 'Notification read successfully' };
}
// @UseGuards(AuthGuard)
// @ApiBearerAuth()
// @Get('public/notifications/:id')
// @ApiOperation({ summary: 'Get a notification by ID' })
// @ApiParam({ name: 'id', description: 'Notification ID' })
// async getUserNotification(@Param('id') id: string) {
// return this.notificationService.findOne(id);
// }
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Post('admin/notifications/send')
// @ApiOperation({ summary: 'Send a notification (queued)' })
// async sendNotification(@Body() dto: SendNotificationDto) {
// return this.notificationService.sendNotification(dto);
// }
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Get('admin/notifications')
// @ApiOperation({ summary: 'Get notifications for a restaurant' })
// @ApiQuery({ name: 'limit', required: false, type: Number })
// @ApiQuery({ name: 'notificationType', required: false, type: String })
// async getRestaurantNotifications(
// @RestId() restaurantId: string,
// @Query('limit') limit?: number,
// @Query('notificationType') notificationType?: string,
// ) {
// if (notificationType) {
// return this.notificationService.findByRestaurantAndType(
// restaurantId,
// notificationType,
// limit ? parseInt(limit.toString(), 10) : 50,
// );
// }
// return this.notificationService.findByRestaurant(restaurantId, limit ? parseInt(limit.toString(), 10) : 50);
// }
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Get('admin/notifications/:id')
// @ApiOperation({ summary: 'Get a notification by ID' })
// @ApiParam({ name: 'id', description: 'Notification ID' })
// async getNotification(@Param('id') id: string) {
// return this.notificationService.findOne(id);
// }
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Post('admin/notifications/:id/retry')
// @ApiOperation({ summary: 'Retry a failed notification' })
// @ApiParam({ name: 'id', description: 'Notification ID' })
// async retryNotification(@Param('id') id: string) {
// await this.notificationService.retryFailedNotification(id);
// return { message: 'Notification queued for retry' };
// }
// Preference endpoints
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Post('admin/notification-preferences')
// @ApiOperation({ summary: 'Create notification preference' })
// createPreference(@RestId() restaurantId: string, @Body() dto: CreatePreferenceDto) {
// return this.preferenceService.createOrUpdate(restaurantId, dto);
// }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/notification-preferences')
@ApiOperation({ summary: 'Get all notification preferences for a restaurant' })
async getPreferences(@RestId() restaurantId: string) {
return this.preferenceService.findByRestaurant(restaurantId);
}
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Get('admin/notification-preferences/:type')
// @ApiOperation({ summary: 'Get a specific notification preference' })
// @ApiParam({ name: 'type', description: 'Notification type' })
// async getPreference(@RestId() restaurantId: string, @Param('type') notificationType: string) {
// return this.preferenceService.findByRestaurantAndType(restaurantId, notificationType);
// }
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Patch('admin/notification-preferences/:type/enabled')
// @ApiOperation({ summary: 'Enable or disable a notification preference' })
// @ApiParam({ name: 'type', description: 'Notification type' })
// async updatePreferenceEnabled(
// @RestId() restaurantId: string,
// @Param('type') notificationType: string,
// @Body('enabled') enabled: boolean,
// ) {
// return this.preferenceService.updateEnabled(restaurantId, notificationType, enabled);
// }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Patch('admin/notification-preferences/:id')
@ApiOperation({ summary: 'Update notification channels' })
@ApiParam({ name: 'id', description: 'Notification preference ID' })
async updatePreference(
@RestId() restaurantId: string,
@Param('id') preferenceId: string,
@Body() dto: UpdatePreferenceDto,
) {
return this.preferenceService.updatePreference(preferenceId, restaurantId, dto);
}
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Delete('admin/notification-preferences/:id')
// @ApiOperation({ summary: 'Delete a notification preference' })
// @ApiParam({ name: 'id', description: 'Notification preference ID' })
// async deletePreference(@RestId() restaurantId: string, @Param('id') preferenceId: string) {
// await this.preferenceService.delete(restaurantId, preferenceId);
// return { message: 'Preference deleted successfully' };
// }
}