All files / src/modules/notifications notifications.controller.ts

100% Statements 27/27
81.25% Branches 13/16
100% Functions 10/10
100% Lines 25/25

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 8313x                           13x 13x 13x 13x       13x 13x     13x             4x                 13x 2x       13x 3x         13x 2x       13x 2x       13x 3x       13x 8x       13x 2x       13x 2x        
import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Param,
  Body,
  Query,
  Req,
  UseGuards,
  ParseIntPipe,
  HttpCode,
} from '@nestjs/common';
import { NotificationsService } from './notifications.service';
import { JwtAuthGuard } from '@app/common/guards/jwt-auth.guard';
import { RegisterDeviceDto } from './dto/register-device.dto';
import { UpdatePreferencesDto } from './dto/update-preferences.dto';
 
@Controller('notifications')
@UseGuards(JwtAuthGuard)
export class NotificationsController {
  constructor(private readonly service: NotificationsService) {}
 
  @Get()
  async getNotifications(
    @Req() req: any,
    @Query('unreadOnly') unreadOnly?: string,
    @Query('type') type?: string,
    @Query('limit') limit?: string,
    @Query('offset') offset?: string,
  ) {
    return this.service.getNotifications(req.user.id, {
      unreadOnly: unreadOnly === 'true',
      type,
      limit: limit ? parseInt(limit) : undefined,
      offset: offset ? parseInt(offset) : undefined,
    });
  }
 
  @Get('unread-count')
  async getUnreadCount(@Req() req: any) {
    return this.service.getUnreadCount(req.user.id);
  }
 
  @Patch(':id/read')
  async markAsRead(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
    return this.service.markAsRead(id, req.user.id);
  }
 
  @Post('mark-all-read')
  @HttpCode(200)
  async markAllAsRead(@Req() req: any) {
    return this.service.markAllAsRead(req.user.id);
  }
 
  @Get('preferences')
  async getPreferences(@Req() req: any) {
    return this.service.getPreferences(req.user.id);
  }
 
  @Patch('preferences')
  async updatePreferences(@Req() req: any, @Body() dto: UpdatePreferencesDto) {
    return this.service.updatePreferences(req.user.id, dto);
  }
 
  @Post('devices')
  async registerDevice(@Req() req: any, @Body() dto: RegisterDeviceDto) {
    return this.service.registerDevice(req.user.id, dto);
  }
 
  @Get('devices')
  async getDevices(@Req() req: any) {
    return this.service.getUserDevices(req.user.id);
  }
 
  @Delete('devices')
  async unregisterDevice(@Req() req: any, @Body() body: { token: string }) {
    return this.service.unregisterDevice(req.user.id, body.token);
  }
}