diff --git a/apps/api/src/stripe/stripe.controller.spec.ts b/apps/api/src/stripe/stripe.controller.spec.ts index 64838171..27c1dede 100644 --- a/apps/api/src/stripe/stripe.controller.spec.ts +++ b/apps/api/src/stripe/stripe.controller.spec.ts @@ -30,7 +30,10 @@ import { VaultService } from '../vault/vault.service' import { ExportService } from '../export/export.service' import { NotificationModule } from '../sockets/notifications/notification.module' import { CACHE_MANAGER } from '@nestjs/cache-manager' - +import { CreateSubscriptionPaymentDto } from './dto/create-subscription-payment.dto' +import { mockDeep } from 'jest-mock-extended' +import { Grant } from 'keycloak-connect' +import { KeycloakTokenParsed } from '../auth/keycloak' describe('StripeController', () => { let controller: StripeController @@ -182,4 +185,45 @@ describe('StripeController', () => { reason: 'requested_by_customer', }) }) + it(`should not call setupintents.update if campaign can't accept donations`, async () => { + prismaMock.campaign.findFirst.mockResolvedValue({ + id: 'complete-campaign', + allowDonationOnComplete: false, + state: CampaignState.complete, + } as Campaign) + + const payload = { + metadata: { + campaignId: 'complete-campaign', + }, + } + + await expect(controller.updateSetupIntent('123', payload)).rejects.toThrow( + new NotAcceptableException('Campaign cannot accept donations in state: complete'), + ) + }) + it(`should not call subscription.create if campaign can't accept donations`, async () => { + prismaMock.campaign.findFirst.mockResolvedValue({ + id: 'complete-campaign', + allowDonationOnComplete: false, + state: CampaignState.complete, + } as Campaign) + + const user: KeycloakTokenParsed = { + sub: '00000000-0000-0000-0000-000000000013', + 'allowed-origins': [], + email: 'test@podkrepi.bg', + } + const subscribtionObj: CreateSubscriptionPaymentDto = { + type: 'donation', + email: 'test@podkrepi.bg', + campaignId: 'complete-campaign', + amount: 200, + currency: 'BGN', + } + + await expect(controller.createSubscription(user, subscribtionObj)).rejects.toThrow( + new NotAcceptableException('Campaign cannot accept donations in state: complete'), + ) + }) }) diff --git a/apps/api/src/stripe/stripe.controller.ts b/apps/api/src/stripe/stripe.controller.ts index 798cde0b..b2a274c4 100644 --- a/apps/api/src/stripe/stripe.controller.ts +++ b/apps/api/src/stripe/stripe.controller.ts @@ -81,7 +81,6 @@ export class StripeController { @Body() updateSetupIntentDto: UpdateSetupIntentDto, ) { - console.log(updateSetupIntentDto) return this.stripeService.updateSetupIntent(id, updateSetupIntentDto) } diff --git a/apps/api/src/stripe/stripe.service.ts b/apps/api/src/stripe/stripe.service.ts index 99ce4785..dd711ed2 100644 --- a/apps/api/src/stripe/stripe.service.ts +++ b/apps/api/src/stripe/stripe.service.ts @@ -34,6 +34,11 @@ export class StripeService { id: string, inputDto: UpdateSetupIntentDto, ): Promise> { + if (!inputDto.metadata.campaignId) + throw new BadRequestException('campaignId is missing from metadata') + const campaign = await this.campaignService.validateCampaignId( + inputDto.metadata.campaignId as string, + ) return await this.stripeClient.setupIntents.update(id, inputDto) } /** @@ -178,6 +183,9 @@ export class StripeService { user: KeycloakTokenParsed, subscriptionPaymentDto: CreateSubscriptionPaymentDto, ): Promise { + const campaign = await this.campaignService.validateCampaignId( + subscriptionPaymentDto.campaignId, + ) const customer = await this.createCustomer(user, subscriptionPaymentDto) const product = await this.createProduct(subscriptionPaymentDto) const subscription = await this.stripeClient.subscriptions.create({