From 984810f81234a8bbda14270daedd4b816aa56e75 Mon Sep 17 00:00:00 2001 From: "dimitar.nizamov" Date: Thu, 16 Mar 2023 16:22:37 +0100 Subject: [PATCH 1/2] fix - check for an existing person and connect before updating donation --- apps/api/src/campaign/campaign.service.ts | 49 +++++++---------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/apps/api/src/campaign/campaign.service.ts b/apps/api/src/campaign/campaign.service.ts index bb309667d..314746b61 100644 --- a/apps/api/src/campaign/campaign.service.ts +++ b/apps/api/src/campaign/campaign.service.ts @@ -460,7 +460,12 @@ export class CampaignService { Logger.debug('Donation found by subscription: ', donation) } - + let person + if (paymentData.billingEmail) { + person = await this.prisma.person.findFirst({ + where: { email: paymentData.billingEmail }, + }) + } //if missing create the donation with the incoming status if (!donation) { Logger.debug( @@ -485,7 +490,7 @@ export class CampaignService { extPaymentMethodId: paymentData.paymentMethodId ?? '', billingName: paymentData.billingName, billingEmail: paymentData.billingEmail, - person: paymentData.personId ? { connect: { id: paymentData.personId } } : {}, + person: person ? { connect: { id: person.id } } : undefined, }, select: donationNotificationSelect, }) @@ -497,11 +502,10 @@ export class CampaignService { ) throw new InternalServerErrorException(error) } - return } //donation exists, so check if it is safe to update it - else if (shouldAllowStatusChange(donation.status, newDonationStatus)) { + if (shouldAllowStatusChange(donation.status, newDonationStatus)) { try { const updatedDonation = await this.prisma.donation.update({ where: { @@ -510,11 +514,13 @@ export class CampaignService { data: { status: newDonationStatus, amount: paymentData.netAmount, + chargedAmount: paymentData.chargedAmount, extCustomerId: paymentData.stripeCustomerId, extPaymentMethodId: paymentData.paymentMethodId, extPaymentIntentId: paymentData.paymentIntentId, billingName: paymentData.billingName, billingEmail: paymentData.billingEmail, + person: person ? { connect: { id: person.id } } : undefined, }, select: donationNotificationSelect, }) @@ -529,38 +535,13 @@ export class CampaignService { ) throw new InternalServerErrorException(error) } - } - //donation exists but we need to skip because previous status is from later event than the incoming - else { - Logger.warn( - `Skipping update of donation with paymentIntentId: ${paymentData.paymentIntentId} - and status: ${newDonationStatus} because the event comes after existing donation with status: ${donation.status}`, - ) + return } - //For successful donations we will also need to link them to user and add donation wish: - if (newDonationStatus === DonationStatus.succeeded) { - Logger.debug('metadata?.isAnonymous = ' + metadata?.isAnonymous) - - if (metadata?.isAnonymous != 'true') { - await this.prisma.donation.update({ - where: { id: donation.id }, - data: { - person: { - connect: { - email: paymentData.billingEmail, - }, - }, - }, - }) - } - - Logger.debug('Saving donation wish ' + metadata?.wish) - - if (metadata?.wish) { - await this.createDonationWish(metadata.wish, donation.id, campaign.id) - } - } + Logger.warn( + `Skipping update of donation with paymentIntentId: ${paymentData.paymentIntentId} + and status: ${newDonationStatus} because the event comes after existing donation with status: ${donation.status}`, + ) } async createDonationWish(message: string, donationId: string, campaignId: string) { From ef2679221bfe83ac2dd603aed2a41d461c970061 Mon Sep 17 00:00:00 2001 From: "dimitar.nizamov" Date: Thu, 16 Mar 2023 16:35:31 +0100 Subject: [PATCH 2/2] check for `isAnonymous` prop before updating the donation --- apps/api/src/campaign/campaign.service.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/api/src/campaign/campaign.service.ts b/apps/api/src/campaign/campaign.service.ts index 314746b61..4e68db864 100644 --- a/apps/api/src/campaign/campaign.service.ts +++ b/apps/api/src/campaign/campaign.service.ts @@ -490,7 +490,10 @@ export class CampaignService { extPaymentMethodId: paymentData.paymentMethodId ?? '', billingName: paymentData.billingName, billingEmail: paymentData.billingEmail, - person: person ? { connect: { id: person.id } } : undefined, + person: + metadata?.isAnonymous !== 'true' && person + ? { connect: { id: person.id } } + : undefined, }, select: donationNotificationSelect, }) @@ -520,7 +523,10 @@ export class CampaignService { extPaymentIntentId: paymentData.paymentIntentId, billingName: paymentData.billingName, billingEmail: paymentData.billingEmail, - person: person ? { connect: { id: person.id } } : undefined, + person: + metadata?.isAnonymous !== 'true' && person + ? { connect: { id: person.id } } + : undefined, }, select: donationNotificationSelect, })