# All files / modules/superadmin superadmin.controller.ts

0% Statements 0/38
0% Branches 0/5
0% Functions 0/15
0% Lines 0/36

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

     import {
      Controller,
      Get,
      Post,
      Patch,
      Delete,
      Body,
      Param,
      Query,
      UseGuards,
      HttpCode,
      HttpStatus,
    } from '@nestjs/common';
    import {
      ApiTags,
      ApiOperation,
      ApiBearerAuth,
      ApiResponse,
    } from '@nestjs/swagger';
    import { SuperadminService } from './superadmin.service';
    import {
      CreateTenantDto,
      UpdateTenantDto,
      UpdateTenantStatusDto,
      ListTenantsQueryDto,
    } from './dto';
    import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
    import { PermissionsGuard, RequireRoles } from '../rbac/guards/permissions.guard';

    @ApiTags('superadmin')
    @Controller('superadmin')
    @UseGuards(JwtAuthGuard, PermissionsGuard)
    @RequireRoles('superadmin')
    @ApiBearerAuth()
    export class SuperadminController {
      constructor(private readonly superadminService: SuperadminService) {}

      // ==================== Dashboard ====================

      @Get('dashboard/stats')
      @ApiOperation({ summary: 'Get dashboard statistics' })
      @ApiResponse({ status: 200, description: 'Dashboard statistics' })
      async getDashboardStats() {
        return this.superadminService.getDashboardStats();
      }

      // ==================== Tenants ====================

      @Get('tenants')
      @ApiOperation({ summary: 'List all tenants with pagination' })
      @ApiResponse({ status: 200, description: 'Paginated list of tenants' })
      async listTenants(@Query() query: ListTenantsQueryDto) {
        return this.superadminService.listTenants(query);
      }

      @Get('tenants/:id')
      @ApiOperation({ summary: 'Get tenant by ID' })
      @ApiResponse({ status: 200, description: 'Tenant details with stats' })
      @ApiResponse({ status: 404, description: 'Tenant not found' })
      async getTenant(@Param('id') id: string) {
        return this.superadminService.getTenant(id);
      }

      @Post('tenants')
      @ApiOperation({ summary: 'Create a new tenant' })
      @ApiResponse({ status: 201, description: 'Tenant created' })
      @ApiResponse({ status: 409, description: 'Slug already exists' })
      async createTenant(@Body() dto: CreateTenantDto) {
        return this.superadminService.createTenant(dto);
      }

      @Patch('tenants/:id')
      @ApiOperation({ summary: 'Update tenant details' })
      @ApiResponse({ status: 200, description: 'Tenant updated' })
      @ApiResponse({ status: 404, description: 'Tenant not found' })
      async updateTenant(@Param('id') id: string, @Body() dto: UpdateTenantDto) {
        return this.superadminService.updateTenant(id, dto);
      }

      @Patch('tenants/:id/status')
      @ApiOperation({ summary: 'Update tenant status (suspend/activate)' })
      @ApiResponse({ status: 200, description: 'Status updated' })
      @ApiResponse({ status: 404, description: 'Tenant not found' })
      async updateTenantStatus(
        @Param('id') id: string,
        @Body() dto: UpdateTenantStatusDto,
      ) {
        return this.superadminService.updateTenantStatus(id, dto);
      }

      @Delete('tenants/:id')
      @HttpCode(HttpStatus.NO_CONTENT)
      @ApiOperation({ summary: 'Delete a tenant' })
      @ApiResponse({ status: 204, description: 'Tenant deleted' })
      @ApiResponse({ status: 400, description: 'Cannot delete tenant with users' })
      @ApiResponse({ status: 404, description: 'Tenant not found' })
      async deleteTenant(@Param('id') id: string) {
        return this.superadminService.deleteTenant(id);
      }

      // ==================== Tenant Users ====================

      @Get('tenants/:id/users')
      @ApiOperation({ summary: 'Get users for a specific tenant' })
      @ApiResponse({ status: 200, description: 'Paginated list of tenant users' })
      @ApiResponse({ status: 404, description: 'Tenant not found' })
      async getTenantUsers(
        @Param('id') id: string,
        @Query('page') page = 1,
        @Query('limit') limit = 10,
      ) {
        return this.superadminService.getTenantUsers(id, page, limit);
      }

      // ==================== Metrics ====================

      @Get('metrics')
      @ApiOperation({ summary: 'Get all metrics summary' })
      @ApiResponse({ status: 200, description: 'Complete metrics summary' })
      async getMetricsSummary() {
        return this.superadminService.getMetricsSummary();
      }

      @Get('metrics/tenant-growth')
      @ApiOperation({ summary: 'Get tenant growth over time' })
      @ApiResponse({ status: 200, description: 'Tenant growth by month' })
      async getTenantGrowth(@Query('months') months = 12) {
        return this.superadminService.getTenantGrowth(months);
      }

      @Get('metrics/user-growth')
      @ApiOperation({ summary: 'Get user growth over time' })
      @ApiResponse({ status: 200, description: 'User growth by month' })
      async getUserGrowth(@Query('months') months = 12) {
        return this.superadminService.getUserGrowth(months);
      }

      @Get('metrics/plan-distribution')
      @ApiOperation({ summary: 'Get plan distribution' })
      @ApiResponse({ status: 200, description: 'Distribution of plans' })
      async getPlanDistribution() {
        return this.superadminService.getPlanDistribution();
      }

      @Get('metrics/status-distribution')
      @ApiOperation({ summary: 'Get tenant status distribution' })
      @ApiResponse({ status: 200, description: 'Distribution of tenant statuses' })
      async getStatusDistribution() {
        return this.superadminService.getStatusDistribution();
      }

      @Get('metrics/top-tenants')
      @ApiOperation({ summary: 'Get top tenants by user count' })
      @ApiResponse({ status: 200, description: 'Top tenants list' })
      async getTopTenants(@Query('limit') limit = 10) {
        return this.superadminService.getTopTenants(limit);
      }
    }