All files / src/platform/limit limit.controller.ts

0% Statements 0/23
0% Branches 0/11
0% Functions 0/7
0% Lines 0/19

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                                                                                                                         
import { Controller, Get, Put, Param, Body, Req } from '@nestjs/common';
import { UsageMeterService } from './limit.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/subscription')
@ApiTags('SubscriptionUsage')
export class SubscriptionUsageController {
  constructor(private readonly limitService: UsageMeterService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    return { tenantId };
  }
 
  @Get('usage')
  @ApiOperation({ summary: 'Get usage operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getUsage(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.limitService.getUsage(ctx.tenantId);
  }
 
  @Get('limits')
  @ApiOperation({ summary: 'Get limits operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getLimits(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.limitService.getLimits(ctx.tenantId);
  }
}
 
@Controller('api/v1/admin/tenants')
@ApiTags('TenantAdminLimit')
export class TenantAdminLimitController {
  constructor(private readonly limitService: UsageMeterService) {}
 
  @Get(':tenantId/usage')
  @ApiOperation({ summary: 'Get tenant usage operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getTenantUsage(@Param('tenantId') tenantId: string) {
    return this.limitService.getUsage(tenantId);
  }
 
  @Put(':tenantId/usage-overrides')
  @ApiOperation({ summary: 'Set override operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async setOverride(
    @Param('tenantId') tenantId: string,
    @Body()
    body: { metricKey: string; overrideValue: number; expiresHours?: number },
  ) {
    return this.limitService.setOverride(
      tenantId,
      body.metricKey,
      body.overrideValue,
      body.expiresHours,
    );
  }
}