# Code coverage report for modules/billing/controllers/stripe.controller.ts

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

0% Statements 0/47
0% Branches 0/8
0% Functions 0/10
0% Lines 0/44

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

     import {
      Controller,
      Get,
      Post,
      Body,
      UseGuards,
      Query,
    } from '@nestjs/common';
    import {
      ApiTags,
      ApiOperation,
      ApiBearerAuth,
      ApiQuery,
    } from '@nestjs/swagger';
    import { StripeService } from '../services/stripe.service';
    import {
      CreateCheckoutSessionDto,
      CreateBillingPortalSessionDto,
    } from '../dto/stripe-webhook.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('stripe')
    @Controller('stripe')
    @UseGuards(JwtAuthGuard)
    @ApiBearerAuth()
    export class StripeController {
      constructor(private readonly stripeService: StripeService) {}

      @Post('checkout-session')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Create Stripe checkout session' })
      async createCheckoutSession(
        @Body() dto: CreateCheckoutSessionDto,
        @CurrentUser() user: RequestUser,
      ) {
        dto.tenant_id = user.tenant_id;
        const session = await this.stripeService.createCheckoutSession(dto);
        return {
          session_id: session.id,
          url: session.url,
        };
      }

      @Post('billing-portal')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Create Stripe billing portal session' })
      async createBillingPortalSession(
        @Body() dto: CreateBillingPortalSessionDto,
        @CurrentUser() user: RequestUser,
      ) {
        dto.tenant_id = user.tenant_id;
        const session = await this.stripeService.createBillingPortalSession(dto);
        return {
          url: session.url,
        };
      }

      @Post('setup-intent')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Create setup intent for adding payment method' })
      async createSetupIntent(@CurrentUser() user: RequestUser) {
        const customer = await this.stripeService.findCustomerByTenantId(user.tenant_id);

        Iif (!customer) {
          return {
            error: 'Stripe customer not found',
            client_secret: null,
          };
        }

        const setupIntent = await this.stripeService.createSetupIntent(customer.id);
        return {
          client_secret: setupIntent.client_secret,
        };
      }

      @Get('prices')
      @ApiOperation({ summary: 'List available Stripe prices/plans' })
      @ApiQuery({ name: 'product_id', required: false })
      async listPrices(@Query('product_id') productId?: string) {
        const prices = await this.stripeService.listPrices(productId);
        return prices.map((price) => ({
          id: price.id,
          product: typeof price.product === 'string' ? price.product : (price.product as any)?.name,
          currency: price.currency,
          unit_amount: price.unit_amount,
          interval: price.recurring?.interval,
          interval_count: price.recurring?.interval_count,
        }));
      }

      @Get('customer')
      @ApiOperation({ summary: 'Get Stripe customer for current tenant' })
      async getCustomer(@CurrentUser() user: RequestUser) {
        const customer = await this.stripeService.findCustomerByTenantId(user.tenant_id);

        Iif (!customer) {
          return {
            exists: false,
            customer: null,
          };
        }

        return {
          exists: true,
          customer: {
            id: customer.id,
            email: customer.email,
            name: customer.name,
            created: customer.created,
          },
        };
      }

      @Post('customer')
      @UseGuards(PermissionsGuard)
      @RequirePermissions('billing:manage')
      @ApiOperation({ summary: 'Create Stripe customer for tenant' })
      async createCustomer(
        @CurrentUser() user: RequestUser,
        @Body() body: { email: string; name?: string },
      ) {
        const existingCustomer = await this.stripeService.findCustomerByTenantId(user.tenant_id);

        Iif (existingCustomer) {
          return {
            created: false,
            customer: {
              id: existingCustomer.id,
              email: existingCustomer.email,
              name: existingCustomer.name,
            },
            message: 'Customer already exists',
          };
        }

        const customer = await this.stripeService.createCustomer({
          tenant_id: user.tenant_id,
          email: body.email,
          name: body.name,
        });

        return {
          created: true,
          customer: {
            id: customer.id,
            email: customer.email,
            name: customer.name,
          },
        };
      }

      @Get('payment-methods')
      @ApiOperation({ summary: 'List Stripe payment methods' })
      async listPaymentMethods(@CurrentUser() user: RequestUser) {
        const customer = await this.stripeService.findCustomerByTenantId(user.tenant_id);

        Iif (!customer) {
          return { payment_methods: [] };
        }

        const paymentMethods = await this.stripeService.listPaymentMethods(customer.id);

        return {
          payment_methods: paymentMethods.map((pm) => ({
            id: pm.id,
            type: pm.type,
            card: pm.card
              ? {
                  brand: pm.card.brand,
                  last4: pm.card.last4,
                  exp_month: pm.card.exp_month,
                  exp_year: pm.card.exp_year,
                }
              : null,
            created: pm.created,
          })),
        };
      }
    }