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

0% Statements 0/29
0% Branches 0/18
0% Functions 0/7
0% Lines 0/27

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                                                                                                                                                       
import { Controller, Get, Post, Param, Req } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { ModuleLicensingService } from './licensing.service';
import {
  ModuleDefinition,
  TenantModuleLicense,
} from '../auth/schemas/module.schema';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/modules')
@ApiTags('ModuleLicensing')
export class ModuleLicensingController {
  constructor(
    private readonly licensingService: ModuleLicensingService,
    @InjectModel(ModuleDefinition.name)
    private readonly moduleDefModel: Model<ModuleDefinition>,
    @InjectModel(TenantModuleLicense.name)
    private readonly licenseModel: Model<TenantModuleLicense>,
  ) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id';
    return { tenantId, userId };
  }
 
  @Get()
  @ApiOperation({ summary: 'Get all definitions operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getAllDefinitions() {
    return this.moduleDefModel.find({ isActive: true }).lean().exec();
  }
 
  @Get('tenant')
  @ApiOperation({ summary: 'Get tenant licenses operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getTenantLicenses(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.licenseModel.find({ tenantId: ctx.tenantId }).lean().exec();
  }
 
  @Post(':moduleKey/activate')
  @ApiOperation({ summary: 'Activate operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async activate(@Param('moduleKey') moduleKey: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.licensingService.activateModule(
      ctx.tenantId,
      moduleKey,
      ctx.userId,
    );
  }
 
  @Post(':moduleKey/deactivate')
  @ApiOperation({ summary: 'Deactivate operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async deactivate(@Param('moduleKey') moduleKey: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.licensingService.deactivateModule(
      ctx.tenantId,
      moduleKey,
      ctx.userId,
    );
  }
 
  @Get(':moduleKey/usage')
  @ApiOperation({ summary: 'Get usage operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getUsage(@Param('moduleKey') moduleKey: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.licensingService.getUsage(ctx.tenantId, moduleKey);
  }
}