From b00abf65a3c8d1c93e57af27ed40b77115acd237 Mon Sep 17 00:00:00 2001 From: Alexander Petkov Date: Tue, 25 Jun 2024 18:51:55 +0300 Subject: [PATCH] fix: tests and errors after e80f63f --- apps/api/src/campaign/campaign.service.ts | 3 +- apps/api/src/donations/donations.service.ts | 2 +- .../events/stripe-payment.service.spec.ts | 34 ++++++++++--------- .../events/stripe-payment.service.ts | 11 +++--- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/apps/api/src/campaign/campaign.service.ts b/apps/api/src/campaign/campaign.service.ts index d487e519..874826d2 100644 --- a/apps/api/src/campaign/campaign.service.ts +++ b/apps/api/src/campaign/campaign.service.ts @@ -375,7 +375,7 @@ export class CampaignService { return campaign } - async getCampaignBySlug(slug: string): Promise { + async getCampaignBySlug(slug: string) { const includeFilter = { campaignType: { select: { name: true, slug: true, category: true }, @@ -409,6 +409,7 @@ export class CampaignService { }, }, }, + vaults: true, campaignFiles: true, } diff --git a/apps/api/src/donations/donations.service.ts b/apps/api/src/donations/donations.service.ts index d2d92b9a..22d7ee1d 100644 --- a/apps/api/src/donations/donations.service.ts +++ b/apps/api/src/donations/donations.service.ts @@ -978,7 +978,7 @@ export class DonationsService { const campaignId = stripeChargeDto.metadata?.campaignId const campaign = await this.campaignService.getCampaignById(campaignId) const newStatus = mapStripeStatusToInternal(stripeChargeDto) - this.updateDonationPayment(campaign, paymentData, newStatus) + this.campaignService.updateDonationPayment(campaign, paymentData, newStatus) } async createFromBenevity(benevityDto: CreateBenevityPaymentDto) { diff --git a/apps/api/src/donations/events/stripe-payment.service.spec.ts b/apps/api/src/donations/events/stripe-payment.service.spec.ts index 254a9116..499f82bc 100644 --- a/apps/api/src/donations/events/stripe-payment.service.spec.ts +++ b/apps/api/src/donations/events/stripe-payment.service.spec.ts @@ -52,16 +52,12 @@ import { MarketingNotificationsService } from '../../notifications/notifications import { EmailService } from '../../email/email.service' import { TemplateService } from '../../email/template.service' import type { PaymentWithDonation } from '../types/donation' -import { DonationsService } from '../donations.service' -import { ExportService } from '../../export/export.service' const defaultStripeWebhookEndpoint = '/stripe/webhook' const stripeSecret = 'wh_123' describe('StripePaymentService', () => { let stripePaymentService: StripePaymentService - let campaignService: CampaignService - let donationService: DonationsService let app: INestApplication const stripe = new Stripe(stripeSecret, { apiVersion: '2022-11-15' }) @@ -133,12 +129,10 @@ describe('StripePaymentService', () => { ConfigService, StripePaymentService, CampaignService, - DonationsService, MockPrismaService, VaultService, PersonService, RecurringDonationService, - ExportService, { provide: HttpService, useValue: mockDeep(), @@ -153,8 +147,6 @@ describe('StripePaymentService', () => { await app.init() stripePaymentService = app.get(StripePaymentService) - campaignService = app.get(CampaignService) - donationService = app.get(DonationsService) //this intercepts the request raw body and removes the exact signature check const stripePayloadService = app.get(StripePayloadService) @@ -178,6 +170,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const mockedCampaignById = jest .spyOn(campaignService, 'getCampaignById') .mockImplementation(() => Promise.resolve(mockedCampaign)) @@ -185,7 +178,7 @@ describe('StripePaymentService', () => { const paymentData = getPaymentData(mockPaymentEventCreated.data.object as Stripe.PaymentIntent) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockImplementation(() => Promise.resolve('')) .mockName('updateDonationPayment') @@ -218,6 +211,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const mockedCampaignById = jest .spyOn(campaignService, 'getCampaignById') .mockImplementation(() => Promise.resolve(mockedCampaign)) @@ -227,7 +221,7 @@ describe('StripePaymentService', () => { ) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockImplementation(() => Promise.resolve('')) .mockName('updateDonationPayment') @@ -255,6 +249,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const mockedCampaignById = jest .spyOn(campaignService, 'getCampaignById') .mockImplementation(() => Promise.resolve(mockedCampaign)) @@ -262,7 +257,7 @@ describe('StripePaymentService', () => { const paymentData = getPaymentData(mockPaymentEventFailed.data.object as Stripe.PaymentIntent) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockImplementation(() => Promise.resolve('')) .mockName('updateDonationPayment') @@ -293,6 +288,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const vaultService = app.get(VaultService) const mockedCampaignById = jest @@ -327,7 +323,7 @@ describe('StripePaymentService', () => { jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock)) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockName('updateDonationPayment') const mockedIncrementVaultAmount = jest.spyOn(vaultService, 'incrementVaultAmount') @@ -374,6 +370,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const vaultService = app.get(VaultService) const mockedCampaignById = jest @@ -401,7 +398,7 @@ describe('StripePaymentService', () => { jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock)) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockName('updateDonationPayment') const mockedIncrementVaultAmount = jest.spyOn(vaultService, 'incrementVaultAmount') @@ -431,7 +428,9 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const vaultService = app.get(VaultService) + const mockedCampaignById = jest .spyOn(campaignService, 'getCampaignById') .mockImplementation(() => Promise.resolve(mockedCampaign)) @@ -459,7 +458,7 @@ describe('StripePaymentService', () => { jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock)) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockName('updateDonationPayment') const mockDecremementVaultAmount = jest.spyOn(vaultService, 'decrementVaultAmount') @@ -503,6 +502,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const recurring = app.get(RecurringDonationService) const mockedCampaignById = jest .spyOn(campaignService, 'getCampaignVault') @@ -561,6 +561,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const vaultService = app.get(VaultService) const mockedCampaignById = jest @@ -570,7 +571,7 @@ describe('StripePaymentService', () => { jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock)) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockName('updateDonationPayment') prismaMock.payment.findFirst.mockResolvedValue({ @@ -613,6 +614,7 @@ describe('StripePaymentService', () => { secret: stripeSecret, }) + const campaignService = app.get(CampaignService) const recurring = app.get(RecurringDonationService) const mockCancelSubscription = jest @@ -624,7 +626,7 @@ describe('StripePaymentService', () => { .mockImplementation(() => Promise.resolve(mockedCampaignCompeleted)) const mockedUpdateDonationPayment = jest - .spyOn(donationService, 'updateDonationPayment') + .spyOn(campaignService, 'updateDonationPayment') .mockImplementation(() => Promise.resolve('')) .mockName('updateDonationPayment') diff --git a/apps/api/src/donations/events/stripe-payment.service.ts b/apps/api/src/donations/events/stripe-payment.service.ts index 86692ef1..e7e4eef8 100644 --- a/apps/api/src/donations/events/stripe-payment.service.ts +++ b/apps/api/src/donations/events/stripe-payment.service.ts @@ -27,7 +27,6 @@ import { DonationsService } from '../donations.service' @Injectable() export class StripePaymentService { constructor( - private donationService: DonationsService, private campaignService: CampaignService, private recurringDonationService: RecurringDonationService, private sendEmail: EmailService, @@ -64,7 +63,7 @@ export class StripePaymentService { /* * Handle the create event */ - await this.donationService.updateDonationPayment(campaign, paymentData, PaymentStatus.waiting) + await this.campaignService.updateDonationPayment(campaign, paymentData, PaymentStatus.waiting) } @StripeWebhookHandler('payment_intent.canceled') @@ -110,7 +109,7 @@ export class StripePaymentService { const campaign = await this.campaignService.getCampaignById(metadata.campaignId) - await this.donationService.updateDonationPayment(campaign, billingData, PaymentStatus) + await this.campaignService.updateDonationPayment(campaign, billingData, PaymentStatus) } @StripeWebhookHandler('charge.succeeded') @@ -137,7 +136,7 @@ export class StripePaymentService { const billingData = getPaymentDataFromCharge(charge) - const donationId = await this.donationService.updateDonationPayment( + const donationId = await this.campaignService.updateDonationPayment( campaign, billingData, PaymentStatus.succeeded, @@ -172,7 +171,7 @@ export class StripePaymentService { const campaign = await this.campaignService.getCampaignById(metadata.campaignId) - await this.donationService.updateDonationPayment(campaign, billingData, PaymentStatus.refund) + await this.campaignService.updateDonationPayment(campaign, billingData, PaymentStatus.refund) if (billingData.billingEmail !== undefined) { const recepient = { to: [billingData.billingEmail] } @@ -365,7 +364,7 @@ export class StripePaymentService { const paymentData = getInvoiceData(invoice) - await this.donationService.updateDonationPayment(campaign, paymentData, PaymentStatus.succeeded) + await this.campaignService.updateDonationPayment(campaign, paymentData, PaymentStatus.succeeded) //updateDonationPayment will mark the campaign as completed if amount is reached await this.cancelSubscriptionsIfCompletedCampaign(metadata.campaignId)