Skip to content

Commit

Permalink
fix: tests and errors after e80f63f
Browse files Browse the repository at this point in the history
  • Loading branch information
sashko9807 committed Jun 25, 2024
1 parent d581aad commit b00abf6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
3 changes: 2 additions & 1 deletion apps/api/src/campaign/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export class CampaignService {
return campaign
}

async getCampaignBySlug(slug: string): Promise<Campaign> {
async getCampaignBySlug(slug: string) {
const includeFilter = {
campaignType: {
select: { name: true, slug: true, category: true },
Expand Down Expand Up @@ -409,6 +409,7 @@ export class CampaignService {
},
},
},
vaults: true,
campaignFiles: true,
}

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 18 additions & 16 deletions apps/api/src/donations/events/stripe-payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })

Expand Down Expand Up @@ -133,12 +129,10 @@ describe('StripePaymentService', () => {
ConfigService,
StripePaymentService,
CampaignService,
DonationsService,
MockPrismaService,
VaultService,
PersonService,
RecurringDonationService,
ExportService,
{
provide: HttpService,
useValue: mockDeep<HttpService>(),
Expand All @@ -153,8 +147,6 @@ describe('StripePaymentService', () => {
await app.init()

stripePaymentService = app.get<StripePaymentService>(StripePaymentService)
campaignService = app.get<CampaignService>(CampaignService)
donationService = app.get<DonationsService>(DonationsService)

//this intercepts the request raw body and removes the exact signature check
const stripePayloadService = app.get<StripePayloadService>(StripePayloadService)
Expand All @@ -178,14 +170,15 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const mockedCampaignById = jest
.spyOn(campaignService, 'getCampaignById')
.mockImplementation(() => Promise.resolve(mockedCampaign))

const paymentData = getPaymentData(mockPaymentEventCreated.data.object as Stripe.PaymentIntent)

const mockedUpdateDonationPayment = jest
.spyOn(donationService, 'updateDonationPayment')
.spyOn(campaignService, 'updateDonationPayment')
.mockImplementation(() => Promise.resolve(''))
.mockName('updateDonationPayment')

Expand Down Expand Up @@ -218,6 +211,7 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const mockedCampaignById = jest
.spyOn(campaignService, 'getCampaignById')
.mockImplementation(() => Promise.resolve(mockedCampaign))
Expand All @@ -227,7 +221,7 @@ describe('StripePaymentService', () => {
)

const mockedUpdateDonationPayment = jest
.spyOn(donationService, 'updateDonationPayment')
.spyOn(campaignService, 'updateDonationPayment')
.mockImplementation(() => Promise.resolve(''))
.mockName('updateDonationPayment')

Expand Down Expand Up @@ -255,14 +249,15 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const mockedCampaignById = jest
.spyOn(campaignService, 'getCampaignById')
.mockImplementation(() => Promise.resolve(mockedCampaign))

const paymentData = getPaymentData(mockPaymentEventFailed.data.object as Stripe.PaymentIntent)

const mockedUpdateDonationPayment = jest
.spyOn(donationService, 'updateDonationPayment')
.spyOn(campaignService, 'updateDonationPayment')
.mockImplementation(() => Promise.resolve(''))
.mockName('updateDonationPayment')

Expand Down Expand Up @@ -293,6 +288,7 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const vaultService = app.get<VaultService>(VaultService)

const mockedCampaignById = jest
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -374,6 +370,7 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const vaultService = app.get<VaultService>(VaultService)

const mockedCampaignById = jest
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -431,7 +428,9 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const vaultService = app.get<VaultService>(VaultService)

const mockedCampaignById = jest
.spyOn(campaignService, 'getCampaignById')
.mockImplementation(() => Promise.resolve(mockedCampaign))
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -503,6 +502,7 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const recurring = app.get<RecurringDonationService>(RecurringDonationService)
const mockedCampaignById = jest
.spyOn(campaignService, 'getCampaignVault')
Expand Down Expand Up @@ -561,6 +561,7 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const vaultService = app.get<VaultService>(VaultService)

const mockedCampaignById = jest
Expand All @@ -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({
Expand Down Expand Up @@ -613,6 +614,7 @@ describe('StripePaymentService', () => {
secret: stripeSecret,
})

const campaignService = app.get<CampaignService>(CampaignService)
const recurring = app.get<RecurringDonationService>(RecurringDonationService)

const mockCancelSubscription = jest
Expand All @@ -624,7 +626,7 @@ describe('StripePaymentService', () => {
.mockImplementation(() => Promise.resolve(mockedCampaignCompeleted))

const mockedUpdateDonationPayment = jest
.spyOn(donationService, 'updateDonationPayment')
.spyOn(campaignService, 'updateDonationPayment')
.mockImplementation(() => Promise.resolve(''))
.mockName('updateDonationPayment')

Expand Down
11 changes: 5 additions & 6 deletions apps/api/src/donations/events/stripe-payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand Down Expand Up @@ -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] }
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b00abf6

Please sign in to comment.