# Code coverage report for modules/notifications/controllers/devices.controller.ts

# All files / modules/notifications/controllers devices.controller.ts

0% Statements 0/48
0% Branches 0/26
0% Functions 0/16
0% Lines 0/43

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
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175

     import {
      Controller,
      Get,
      Post,
      Patch,
      Delete,
      Body,
      Param,
      UseGuards,
      HttpCode,
      HttpStatus,
    } from '@nestjs/common';
    import {
      ApiTags,
      ApiOperation,
      ApiResponse,
      ApiBearerAuth,
    } from '@nestjs/swagger';
    import { DevicesService, PushNotificationService } from '../services';
    import { RegisterDeviceDto, UpdateDeviceDto } from '../dto';

    // These decorators would come from your auth module
    // Adjust imports based on your actual auth implementation
    interface User {
      id: string;
      tenant_id: string;
    }

    // Placeholder decorators - replace with your actual implementations
    const CurrentUser = () => (target: any, key: string, index: number) => {};
    const CurrentTenant = () => (target: any, key: string, index: number) => {};
    const JwtAuthGuard = class {};
    const TenantGuard = class {};
    const Public = () => (target: any, key: string, descriptor: PropertyDescriptor) => {};

    @ApiTags('Notification Devices')
    @Controller('notifications/devices')
    export class DevicesController {
      constructor(
        private readonly devicesService: DevicesService,
        private readonly pushService: PushNotificationService,
      ) {}

      @Get('vapid-key')
      @ApiOperation({ summary: 'Get VAPID public key for push subscription' })
      @ApiResponse({ status: 200, description: 'Returns VAPID public key' })
      getVapidKey() {
        const vapidPublicKey = this.pushService.getVapidPublicKey();

        return {
          vapidPublicKey,
          isEnabled: this.pushService.isEnabled(),
        };
      }

      @Get()
      @UseGuards(JwtAuthGuard, TenantGuard)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'List my registered devices' })
      @ApiResponse({ status: 200, description: 'Returns list of devices' })
      async getDevices(
        @CurrentUser() user: User,
        @CurrentTenant() tenantId: string,
      ) {
        // In actual implementation, get user and tenant from request
        const userId = (user as any)?.id || '';
        const tenant = tenantId || (user as any)?.tenant_id || '';

        return this.devicesService.findByUser(userId, tenant);
      }

      @Post()
      @UseGuards(JwtAuthGuard, TenantGuard)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'Register device for push notifications' })
      @ApiResponse({ status: 201, description: 'Device registered' })
      @ApiResponse({ status: 400, description: 'Invalid subscription' })
      async registerDevice(
        @CurrentUser() user: User,
        @CurrentTenant() tenantId: string,
        @Body() dto: RegisterDeviceDto,
      ) {
        const userId = (user as any)?.id || '';
        const tenant = tenantId || (user as any)?.tenant_id || '';

        // Validate subscription format
        Iif (!this.pushService.validateSubscription(dto.deviceToken)) {
          return {
            success: false,
            error: 'Invalid push subscription format',
          };
        }

        const device = await this.devicesService.register(userId, tenant, dto);

        return {
          success: true,
          device: {
            id: device.id,
            device_type: device.device_type,
            device_name: device.device_name,
            browser: device.browser,
            os: device.os,
            created_at: device.created_at,
          },
        };
      }

      @Patch(':id')
      @UseGuards(JwtAuthGuard, TenantGuard)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'Update device' })
      @ApiResponse({ status: 200, description: 'Device updated' })
      @ApiResponse({ status: 404, description: 'Device not found' })
      async updateDevice(
        @CurrentUser() user: User,
        @CurrentTenant() tenantId: string,
        @Param('id') deviceId: string,
        @Body() dto: UpdateDeviceDto,
      ) {
        const userId = (user as any)?.id || '';
        const tenant = tenantId || (user as any)?.tenant_id || '';

        return this.devicesService.update(deviceId, userId, tenant, dto);
      }

      @Delete(':id')
      @UseGuards(JwtAuthGuard, TenantGuard)
      @ApiBearerAuth()
      @HttpCode(HttpStatus.NO_CONTENT)
      @ApiOperation({ summary: 'Unregister device' })
      @ApiResponse({ status: 204, description: 'Device unregistered' })
      @ApiResponse({ status: 404, description: 'Device not found' })
      async unregisterDevice(
        @CurrentUser() user: User,
        @CurrentTenant() tenantId: string,
        @Param('id') deviceId: string,
      ) {
        const userId = (user as any)?.id || '';
        const tenant = tenantId || (user as any)?.tenant_id || '';

        await this.devicesService.unregister(deviceId, userId, tenant);
      }

      @Get('stats')
      @UseGuards(JwtAuthGuard, TenantGuard)
      @ApiBearerAuth()
      @ApiOperation({ summary: 'Get device stats for current user' })
      @ApiResponse({ status: 200, description: 'Returns device statistics' })
      async getStats(
        @CurrentUser() user: User,
        @CurrentTenant() tenantId: string,
      ) {
        const userId = (user as any)?.id || '';
        const tenant = tenantId || (user as any)?.tenant_id || '';

        const activeCount = await this.devicesService.countActiveDevices(
          userId,
          tenant,
        );
        const devices = await this.devicesService.findByUser(userId, tenant);

        return {
          total: devices.length,
          active: activeCount,
          inactive: devices.length - activeCount,
          byType: {
            web: devices.filter((d) => d.device_type === 'web').length,
            mobile: devices.filter((d) => d.device_type === 'mobile').length,
            desktop: devices.filter((d) => d.device_type === 'desktop').length,
          },
        };
      }
    }