All files / src/platform/notifications notification.controller.ts

0% Statements 0/38
0% Branches 0/25
0% Functions 0/11
0% Lines 0/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                                                                                                                                                                                                                         
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Query,
  Req,
} from '@nestjs/common';
import { NotificationService } from './notification.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/notifications')
@ApiTags('Notification')
export class NotificationController {
  constructor(private readonly notificationService: NotificationService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id';
    return { tenantId, userId };
  }
 
  @Get()
  @ApiOperation({ summary: 'Get inbox operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getInbox(
    @Query('page') page: string,
    @Query('limit') limit: string,
    @Req() req: any,
  ): Promise<any> {
    const ctx = this.getContext(req);
    return this.notificationService.getInbox(
      ctx.tenantId,
      ctx.userId,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 20,
    );
  }
 
  @Get('unread-count')
  @ApiOperation({ summary: 'Get unread count operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getUnreadCount(@Req() req: any) {
    const ctx = this.getContext(req);
    return {
      count: await this.notificationService.getUnreadCount(
        ctx.tenantId,
        ctx.userId,
      ),
    };
  }
 
  @Post(':id/read')
  @ApiOperation({ summary: 'Mark as read operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async markAsRead(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.notificationService.markAsRead(id, ctx.tenantId, ctx.userId);
  }
 
  @Post('read-all')
  @ApiOperation({ summary: 'Mark all as read operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async markAllAsRead(@Req() req: any) {
    const ctx = this.getContext(req);
    await this.notificationService.markAllAsRead(ctx.tenantId, ctx.userId);
    return { success: true };
  }
 
  @Delete(':id')
  @ApiOperation({ summary: 'Delete notification operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async deleteNotification(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    await this.notificationService.delete(id, ctx.tenantId, ctx.userId);
    return { success: true };
  }
}
 
@Controller('api/v1/notification-preferences')
@ApiTags('NotificationPreference')
export class NotificationPreferenceController {
  constructor(private readonly notificationService: NotificationService) {}
 
  private getUserId(req: any): string {
    return req.user?.id || req.headers['x-user-id'] || 'system-user-id';
  }
 
  @Get()
  @ApiOperation({ summary: 'Get preferences operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getPreferences(@Req() req: any) {
    const userId = this.getUserId(req);
    return this.notificationService.getPreferences(userId);
  }
 
  @Put()
  @ApiOperation({ summary: 'Update preferences operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async updatePreferences(@Body() body: any, @Req() req: any) {
    const userId = this.getUserId(req);
    return this.notificationService.updatePreferences(userId, body);
  }
}