# All files / modules/billing billing.controller.ts

100% Statements 43/43
100% Branches 0/0
100% Functions 16/16
100% Lines 41/41

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
    1921x

    1x

    1x
    1x

    1x
    1x
    1x

    1x
    15x

    1x
    1x

    1x
    1x

    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x
    1x

     import {
      Controller,
      Get,
      Post,
      Patch,
      Delete,
      Body,
      Param,
      Query,
      UseGuards,
    } from '@nestjs/common';
    import {
      ApiTags,
      ApiOperation,
      ApiBearerAuth,
      ApiQuery,
    } from '@nestjs/swagger';
    import { BillingService } from './services/billing.service';
    import {
      CreateSubscriptionDto,
      UpdateSubscriptionDto,
      CancelSubscriptionDto,
      CreatePaymentMethodDto,
    } from './dto';
    import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
    import { PermissionsGuard, RequirePermissions } from '../rbac/guards/permissions.guard';
    import { CurrentUser } from '../auth/decorators/current-user.decorator';
    import { RequestUser } from '../auth/strategies/jwt.strategy';

    @ApiTags('billing')
    @Controller('billing')
    @UseGuards(JwtAuthGuard)
    @ApiBearerAuth()
    export class BillingController {
      constructor(private readonly billingService: BillingService) {}

      // ==================== Subscription ====================

      @Get('subscription')
      @ApiOperation({ summary: 'Get current subscription' })
      async getSubscription(@CurrentUser() user: RequestUser) {
        return this.billingService.getSubscription(user.tenant_id);
      }

      @Get('subscription/status')
      @ApiOperation({ summary: 'Check subscription status' })
      async getSubscriptionStatus(@CurrentUser() user: RequestUser) {
        return this.billingService.checkSubscriptionStatus(user.tenant_id);
      }

      @Post('subscription')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Create subscription (admin)' })
      async createSubscription(
        @Body() dto: CreateSubscriptionDto,
        @CurrentUser() user: RequestUser,
      ) {
        // Override tenant_id with the current user's tenant
        dto.tenant_id = user.tenant_id;
        return this.billingService.createSubscription(dto);
      }

      @Patch('subscription')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Update subscription' })
      async updateSubscription(
        @Body() dto: UpdateSubscriptionDto,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.updateSubscription(user.tenant_id, dto);
      }

      @Post('subscription/cancel')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Cancel subscription' })
      async cancelSubscription(
        @Body() dto: CancelSubscriptionDto,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.cancelSubscription(user.tenant_id, dto);
      }

      @Post('subscription/change-plan/:planId')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Change subscription plan' })
      async changePlan(
        @Param('planId') planId: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.changePlan(user.tenant_id, planId);
      }

      // ==================== Invoices ====================

      @Get('invoices')
      @ApiOperation({ summary: 'Get invoices' })
      @ApiQuery({ name: 'page', required: false, type: Number })
      @ApiQuery({ name: 'limit', required: false, type: Number })
      async getInvoices(
        @CurrentUser() user: RequestUser,
        @Query('page') page?: number,
        @Query('limit') limit?: number,
      ) {
        return this.billingService.getInvoices(user.tenant_id, { page, limit });
      }

      @Get('invoices/:id')
      @ApiOperation({ summary: 'Get invoice by ID' })
      async getInvoice(
        @Param('id') id: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.getInvoice(id, user.tenant_id);
      }

      @Post('invoices/:id/mark-paid')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Mark invoice as paid (admin)' })
      async markInvoicePaid(
        @Param('id') id: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.markInvoicePaid(id, user.tenant_id);
      }

      @Post('invoices/:id/void')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Void invoice (admin)' })
      async voidInvoice(
        @Param('id') id: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.voidInvoice(id, user.tenant_id);
      }

      // ==================== Payment Methods ====================

      @Get('payment-methods')
      @ApiOperation({ summary: 'Get payment methods' })
      async getPaymentMethods(@CurrentUser() user: RequestUser) {
        return this.billingService.getPaymentMethods(user.tenant_id);
      }

      @Post('payment-methods')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Add payment method' })
      async addPaymentMethod(
        @Body() dto: CreatePaymentMethodDto,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.addPaymentMethod(user.tenant_id, dto);
      }

      @Post('payment-methods/:id/set-default')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Set default payment method' })
      async setDefaultPaymentMethod(
        @Param('id') id: string,
        @CurrentUser() user: RequestUser,
      ) {
        return this.billingService.setDefaultPaymentMethod(id, user.tenant_id);
      }

      @Delete('payment-methods/:id')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Remove payment method' })
      async removePaymentMethod(
        @Param('id') id: string,
        @CurrentUser() user: RequestUser,
      ) {
        await this.billingService.removePaymentMethod(id, user.tenant_id);
        return { message: 'Payment method removed' };
      }

      // ==================== Summary ====================

      @Get('summary')
      @ApiOperation({ summary: 'Get billing summary' })
      async getBillingSummary(@CurrentUser() user: RequestUser) {
        return this.billingService.getBillingSummary(user.tenant_id);
      }
    }