All files / modules/notifications notifications.controller.ts

97.22% Statements 35/36
100% Branches 6/6
91.66% Functions 11/12
97.05% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1561x                     1x           1x 1x         1x 1x 1x             1x 11x                 1x           2x                 1x 1x       1x         1x       1x         1x 1x       1x         1x       1x 1x             1x 1x         1x       1x                         1x       1x             1x                     1x 1x             1x 1x      
import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { NotificationsService } from './services/notifications.service';
import {
  CreateNotificationDto,
  SendTemplateNotificationDto,
  UpdatePreferencesDto,
} from './dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard, RequirePermissions } from '../rbac/guards/permissions.guard';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { RequestUser } from '../auth/strategies/jwt.strategy';
 
@ApiTags('notifications')
@Controller('notifications')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class NotificationsController {
  constructor(private readonly notificationsService: NotificationsService) {}
 
  // ==================== User Notifications ====================
 
  @Get()
  @ApiOperation({ summary: 'Get my notifications' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'unreadOnly', required: false, type: Boolean })
  async getMyNotifications(
    @CurrentUser() user: RequestUser,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
    @Query('unreadOnly') unreadOnly?: boolean,
  ) {
    return this.notificationsService.findAllForUser(user.id, user.tenant_id, {
      page: page || 1,
      limit: limit || 20,
      unreadOnly: unreadOnly || false,
    });
  }
 
  @Get('unread-count')
  @ApiOperation({ summary: 'Get unread notifications count' })
  async getUnreadCount(@CurrentUser() user: RequestUser) {
    const count = await this.notificationsService.getUnreadCount(
      user.id,
      user.tenant_id,
    );
    return { count };
  }
 
  @Patch(':id/read')
  @ApiOperation({ summary: 'Mark notification as read' })
  async markAsRead(
    @Param('id') id: string,
    @CurrentUser() user: RequestUser,
  ) {
    return this.notificationsService.markAsRead(id, user.id, user.tenant_id);
  }
 
  @Post('read-all')
  @ApiOperation({ summary: 'Mark all notifications as read' })
  async markAllAsRead(@CurrentUser() user: RequestUser) {
    const count = await this.notificationsService.markAllAsRead(
      user.id,
      user.tenant_id,
    );
    return { message: `${count} notificaciones marcadas como leĆ­das` };
  }
 
  @Delete(':id')
  @ApiOperation({ summary: 'Delete notification' })
  async delete(
    @Param('id') id: string,
    @CurrentUser() user: RequestUser,
  ) {
    await this.notificationsService.delete(id, user.id, user.tenant_id);
    return { message: 'Notificación eliminada' };
  }
 
  // ==================== Preferences ====================
 
  @Get('preferences')
  @ApiOperation({ summary: 'Get my notification preferences' })
  async getPreferences(@CurrentUser() user: RequestUser) {
    return this.notificationsService.getPreferences(user.id, user.tenant_id);
  }
 
  @Patch('preferences')
  @ApiOperation({ summary: 'Update my notification preferences' })
  async updatePreferences(
    @CurrentUser() user: RequestUser,
    @Body() dto: UpdatePreferencesDto,
  ) {
    return this.notificationsService.updatePreferences(
      user.id,
      user.tenant_id,
      dto,
    );
  }
 
  // ==================== Admin Operations ====================
 
  @Post()
  @UseGuards(PermissionsGuard)
  @RequirePermissions('notifications:manage')
  @ApiOperation({ summary: 'Send notification to user (admin)' })
  async sendNotification(
    @Body() dto: CreateNotificationDto,
    @CurrentUser() user: RequestUser,
  ) {
    return this.notificationsService.create(dto, user.tenant_id);
  }
 
  @Post('template')
  @UseGuards(PermissionsGuard)
  @RequirePermissions('notifications:manage')
  @ApiOperation({ summary: 'Send notification from template (admin)' })
  async sendFromTemplate(
    @Body() dto: SendTemplateNotificationDto,
    @CurrentUser() user: RequestUser,
  ) {
    return this.notificationsService.sendFromTemplate(dto, user.tenant_id);
  }
 
  @Get('templates')
  @UseGuards(PermissionsGuard)
  @RequirePermissions('notifications:manage')
  @ApiOperation({ summary: 'List notification templates (admin)' })
  async getTemplates() {
    return this.notificationsService.findAllTemplates();
  }
 
  @Get('templates/:code')
  @UseGuards(PermissionsGuard)
  @RequirePermissions('notifications:manage')
  @ApiOperation({ summary: 'Get notification template by code (admin)' })
  async getTemplate(@Param('code') code: string) {
    return this.notificationsService.findTemplateByCode(code);
  }
}