All files / src/interfaces/web-api/company-admin communications.controller.ts

0% Statements 0/25
0% Branches 0/12
0% Functions 0/6
0% Lines 0/23

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                                                                                                                                                                                       
import {
  Controller,
  Post,
  Body,
  Req,
  UseGuards,
  Get,
  Delete,
  Param,
} from '@nestjs/common';
import { CommunicationHubService } from '../../../platform/communications/communication-hub.service';
import { PushNotificationService } from '../../../platform/communications/push/push.service';
import { WebPushService } from '../../../platform/communications/web-push/web-push.service';
import { JwtAuthGuard } from '../../../platform/auth/jwt-auth.guard';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
class RegisterPushTokenDto {
  token: string;
  platform: 'ios' | 'android' | 'flutter';
  deviceId?: string;
  deviceName?: string;
}
 
class RegisterWebPushDto {
  endpoint: string;
  keys: {
    p256dh: string;
    auth: string;
  };
}
 
@Controller('api/v1/communications')
@UseGuards(JwtAuthGuard)
@ApiTags('Communications')
export class CommunicationsController {
  constructor(
    private readonly hubService: CommunicationHubService,
    private readonly pushService: PushNotificationService,
    private readonly webPushService: WebPushService,
  ) {}
 
  @Post('push/register')
  @ApiOperation({ summary: 'Register push token operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async registerPushToken(@Body() body: RegisterPushTokenDto, @Req() req: any) {
    return this.pushService.registerToken(
      req.user.userId,
      req.user.tenantId,
      body.token,
      body.platform,
      body.deviceId,
      body.deviceName,
    );
  }
 
  @Delete('push/deregister')
  @ApiOperation({ summary: 'Deregister push token operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async deregisterPushToken(@Body('token') token: string) {
    await this.pushService.deregisterToken(token);
    return { success: true };
  }
 
  @Post('web-push/subscribe')
  @ApiOperation({ summary: 'Subscribe web push operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async subscribeWebPush(@Body() body: RegisterWebPushDto, @Req() req: any) {
    return this.webPushService.subscribe(
      req.user.tenantId,
      req.user.userId,
      body,
      req.headers['user-agent'],
    );
  }
 
  @Post('web-push/unsubscribe')
  @ApiOperation({ summary: 'Unsubscribe web push operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async unsubscribeWebPush(@Body('endpoint') endpoint: string) {
    await this.webPushService.unsubscribe(endpoint);
    return { success: true };
  }
 
  @Get('web-push/vapid-key')
  @ApiOperation({ summary: 'Get vapid key operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getVapidKey(@Req() req: any) {
    const keys = await this.webPushService.getOrCreateKeys(req.user.tenantId);
    return { publicKey: keys.publicKey };
  }
}