All files / src/platform/subscriptions subscription.controller.ts

0% Statements 0/36
0% Branches 0/17
0% Functions 0/11
0% Lines 0/32

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                                                                                                                                                                                                                           
import { Controller, Get, Post, Param, Body, Req } from '@nestjs/common';
import { SubscriptionService } from './subscription.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/subscription')
@ApiTags('Subscription')
export class SubscriptionController {
  constructor(private readonly subscriptionService: SubscriptionService) {}
 
  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 subscription operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getSubscription(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.subscriptionService.getSubscription(ctx.tenantId);
  }
 
  @Post('upgrade')
  @ApiOperation({ summary: 'Upgrade operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async upgrade(@Body() body: { planKey: string }, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.subscriptionService.upgradePlan(
      ctx.tenantId,
      body.planKey,
      ctx.userId,
    );
  }
 
  @Post('cancel')
  @ApiOperation({ summary: 'Cancel operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async cancel(
    @Body() body: { reason: string; feedback: string },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.subscriptionService.cancelSubscription(
      ctx.tenantId,
      body.reason,
      body.feedback,
      ctx.userId,
    );
  }
 
  @Post('reactivate')
  @ApiOperation({ summary: 'Reactivate operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async reactivate(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.subscriptionService.reactivateSubscription(
      ctx.tenantId,
      ctx.userId,
    );
  }
}
 
@Controller('api/v1/admin/tenants')
@ApiTags('TenantAdminSubscription')
export class TenantAdminSubscriptionController {
  constructor(private readonly subscriptionService: SubscriptionService) {}
 
  private getUserId(req: any) {
    return req.user?.id || req.headers['x-user-id'] || 'system-user-id';
  }
 
  @Post(':tenantId/trial/extend')
  @ApiOperation({ summary: 'Extend trial operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async extendTrial(
    @Param('tenantId') tenantId: string,
    @Body() body: { days: number },
    @Req() req: any,
  ) {
    const userId = this.getUserId(req);
    return this.subscriptionService.extendTrial(tenantId, body.days, userId);
  }
 
  @Post(':tenantId/suspend')
  @ApiOperation({ summary: 'Suspend operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async suspend(
    @Param('tenantId') tenantId: string,
    @Body() body: { reason: string },
    @Req() req: any,
  ) {
    const userId = this.getUserId(req);
    return this.subscriptionService.suspendSubscription(
      tenantId,
      body.reason,
      userId,
    );
  }
 
  @Post(':tenantId/resume')
  @ApiOperation({ summary: 'Resume operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async resume(@Param('tenantId') tenantId: string, @Req() req: any) {
    const userId = this.getUserId(req);
    return this.subscriptionService.resumeSubscription(tenantId, userId);
  }
}