All files / src/platform/feature-flags feature-flag.controller.ts

0% Statements 0/14
0% Branches 0/11
0% Functions 0/3
0% Lines 0/12

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                                                                                             
import {
  Controller,
  Post,
  Put,
  Param,
  Body,
  Req,
  UseGuards,
} from '@nestjs/common';
import { FeatureFlagService } from './feature-flag.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/feature-flags')
@ApiTags('FeatureFlags')
@UseGuards(JwtAuthGuard)
export class FeatureFlagController {
  constructor(private readonly featureFlagService: FeatureFlagService) {}
 
  @Post(':key/evaluate')
  @ApiOperation({ summary: 'Evaluate feature flag status' })
  @ApiResponse({ status: 200, description: 'Evaluation result payload' })
  async evaluateFlag(@Param('key') key: string, @Req() req: any) {
    const context = {
      tenantId: req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM',
      userId: req.user?.id || 'SYSTEM',
      userEmail: req.user?.email || 'SYSTEM',
    };
    const isEnabled = await this.featureFlagService.evaluate(key, context);
    return { isEnabled };
  }
 
  @Put(':key')
  @ApiOperation({ summary: 'Configure rules and status for a feature flag' })
  @ApiResponse({ status: 200, description: 'Updated feature flag detail' })
  async configureFlag(
    @Param('key') key: string,
    @Body() body: { isEnabled: boolean; rules?: any },
  ) {
    return this.featureFlagService.configureFlag(
      key,
      body.isEnabled,
      body.rules,
    );
  }
}