# All files / modules/feature-flags feature-flags.controller.ts

100% Statements 49/49
100% Branches 0/0
100% Functions 16/16
100% Lines 47/47

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
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    2031x

    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    17x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x
    1x

    1x

    2x

    1x
    1x

    1x

    1x

    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x

    1x

    1x

    1x
    1x

    1x

    1x

    2x

    2x
    2x

     import {
      Controller,
      Get,
      Post,
      Put,
      Delete,
      Body,
      Param,
      Query,
      UseGuards,
    } from '@nestjs/common';
    import {
      ApiTags,
      ApiOperation,
      ApiResponse,
      ApiBearerAuth,
      ApiParam,
      ApiQuery,
    } from '@nestjs/swagger';
    import { FeatureFlagsService, EvaluationContext } from './services/feature-flags.service';
    import { CreateFlagDto } from './dto/create-flag.dto';
    import { UpdateFlagDto } from './dto/update-flag.dto';
    import { SetTenantFlagDto, SetUserFlagDto } from './dto/set-tenant-flag.dto';
    import { JwtAuthGuard } from '../auth/guards';
    import { CurrentUser } from '../auth/decorators';
    import { RequestUser } from '../auth/strategies/jwt.strategy';

    @ApiTags('Feature Flags')
    @ApiBearerAuth()
    @UseGuards(JwtAuthGuard)
    @Controller('feature-flags')
    export class FeatureFlagsController {
      constructor(private readonly featureFlagsService: FeatureFlagsService) {}

      // ==================== FLAG MANAGEMENT (Admin) ====================

      @Post()
      @ApiOperation({ summary: 'Create a new feature flag' })
      @ApiResponse({ status: 201, description: 'Flag created successfully' })
      @ApiResponse({ status: 409, description: 'Flag with this key already exists' })
      async createFlag(@Body() dto: CreateFlagDto) {
        return this.featureFlagsService.createFlag(dto);
      }

      @Get()
      @ApiOperation({ summary: 'Get all feature flags' })
      @ApiResponse({ status: 200, description: 'List of all flags' })
      async getAllFlags() {
        return this.featureFlagsService.getAllFlags();
      }

      @Get(':id')
      @ApiOperation({ summary: 'Get flag by ID' })
      @ApiParam({ name: 'id', description: 'Flag ID' })
      @ApiResponse({ status: 200, description: 'Flag details' })
      @ApiResponse({ status: 404, description: 'Flag not found' })
      async getFlagById(@Param('id') id: string) {
        return this.featureFlagsService.getFlagById(id);
      }

      @Put(':id')
      @ApiOperation({ summary: 'Update a feature flag' })
      @ApiParam({ name: 'id', description: 'Flag ID' })
      @ApiResponse({ status: 200, description: 'Flag updated successfully' })
      @ApiResponse({ status: 404, description: 'Flag not found' })
      async updateFlag(@Param('id') id: string, @Body() dto: UpdateFlagDto) {
        return this.featureFlagsService.updateFlag(id, dto);
      }

      @Delete(':id')
      @ApiOperation({ summary: 'Delete a feature flag' })
      @ApiParam({ name: 'id', description: 'Flag ID' })
      @ApiResponse({ status: 200, description: 'Flag deleted successfully' })
      @ApiResponse({ status: 404, description: 'Flag not found' })
      async deleteFlag(@Param('id') id: string) {
        await this.featureFlagsService.deleteFlag(id);
        return { success: true };
      }

      @Post(':id/toggle')
      @ApiOperation({ summary: 'Toggle flag enabled state' })
      @ApiParam({ name: 'id', description: 'Flag ID' })
      @ApiQuery({ name: 'enabled', type: 'boolean', required: true })
      @ApiResponse({ status: 200, description: 'Flag toggled successfully' })
      async toggleFlag(
        @Param('id') id: string,
        @Query('enabled') enabled: string,
      ) {
        return this.featureFlagsService.toggleFlag(id, enabled === 'true');
      }

      // ==================== TENANT FLAGS ====================

      @Get('tenant/overrides')
      @ApiOperation({ summary: 'Get all flag overrides for current tenant' })
      @ApiResponse({ status: 200, description: 'List of tenant flag overrides' })
      async getTenantFlags(@CurrentUser() user: RequestUser) {
        return this.featureFlagsService.getTenantFlags(user.tenant_id);
      }

      @Post('tenant/override')
      @ApiOperation({ summary: 'Set flag override for current tenant' })
      @ApiResponse({ status: 201, description: 'Tenant flag set successfully' })
      async setTenantFlag(
        @CurrentUser() user: RequestUser,
        @Body() dto: SetTenantFlagDto,
      ) {
        return this.featureFlagsService.setTenantFlag(user.tenant_id, dto);
      }

      @Delete('tenant/override/:flagId')
      @ApiOperation({ summary: 'Remove flag override for current tenant' })
      @ApiParam({ name: 'flagId', description: 'Flag ID' })
      @ApiResponse({ status: 200, description: 'Tenant flag removed successfully' })
      async removeTenantFlag(
        @CurrentUser() user: RequestUser,
        @Param('flagId') flagId: string,
      ) {
        await this.featureFlagsService.removeTenantFlag(user.tenant_id, flagId);
        return { success: true };
      }

      // ==================== USER FLAGS ====================

      @Get('user/:userId/overrides')
      @ApiOperation({ summary: 'Get all flag overrides for a user' })
      @ApiParam({ name: 'userId', description: 'User ID' })
      @ApiResponse({ status: 200, description: 'List of user flag overrides' })
      async getUserFlags(
        @CurrentUser() user: RequestUser,
        @Param('userId') userId: string,
      ) {
        return this.featureFlagsService.getUserFlags(user.tenant_id, userId);
      }

      @Post('user/override')
      @ApiOperation({ summary: 'Set flag override for a user' })
      @ApiResponse({ status: 201, description: 'User flag set successfully' })
      async setUserFlag(
        @CurrentUser() user: RequestUser,
        @Body() dto: SetUserFlagDto,
      ) {
        return this.featureFlagsService.setUserFlag(user.tenant_id, dto);
      }

      @Delete('user/:userId/override/:flagId')
      @ApiOperation({ summary: 'Remove flag override for a user' })
      @ApiParam({ name: 'userId', description: 'User ID' })
      @ApiParam({ name: 'flagId', description: 'Flag ID' })
      @ApiResponse({ status: 200, description: 'User flag removed successfully' })
      async removeUserFlag(
        @Param('userId') userId: string,
        @Param('flagId') flagId: string,
      ) {
        await this.featureFlagsService.removeUserFlag(userId, flagId);
        return { success: true };
      }

      // ==================== EVALUATION ====================

      @Get('evaluate/:key')
      @ApiOperation({ summary: 'Evaluate a single flag for current context' })
      @ApiParam({ name: 'key', description: 'Flag key' })
      @ApiResponse({ status: 200, description: 'Flag evaluation result' })
      async evaluateFlag(
        @CurrentUser() user: RequestUser,
        @Param('key') key: string,
      ) {
        const context: EvaluationContext = {
          tenantId: user.tenant_id,
          userId: user.id,
        };
        return this.featureFlagsService.evaluateFlag(key, context);
      }

      @Get('evaluate')
      @ApiOperation({ summary: 'Evaluate all flags for current context' })
      @ApiResponse({ status: 200, description: 'All flag evaluations' })
      async evaluateAllFlags(@CurrentUser() user: RequestUser) {
        const context: EvaluationContext = {
          tenantId: user.tenant_id,
          userId: user.id,
        };
        return this.featureFlagsService.evaluateAllFlags(context);
      }

      @Get('check/:key')
      @ApiOperation({ summary: 'Quick check if flag is enabled' })
      @ApiParam({ name: 'key', description: 'Flag key' })
      @ApiResponse({ status: 200, description: 'Boolean enabled status' })
      async isEnabled(
        @CurrentUser() user: RequestUser,
        @Param('key') key: string,
      ) {
        const context: EvaluationContext = {
          tenantId: user.tenant_id,
          userId: user.id,
        };
        const enabled = await this.featureFlagsService.isEnabled(key, context);
        return { key, enabled };
      }
    }