From e4914900ffff6ef5d5e8ffe4441bf816f11bfc3a Mon Sep 17 00:00:00 2001 From: Alexander Petkov Date: Wed, 24 Apr 2024 15:59:57 +0300 Subject: [PATCH 1/2] fix: Resolve not being able to update payment/donation --- apps/api/src/donations/donations.service.ts | 17 ++++++----------- .../api/src/donations/dto/update-payment.dto.ts | 7 ++++++- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/apps/api/src/donations/donations.service.ts b/apps/api/src/donations/donations.service.ts index 407755bbf..6acd6590f 100644 --- a/apps/api/src/donations/donations.service.ts +++ b/apps/api/src/donations/donations.service.ts @@ -680,12 +680,12 @@ export class DonationsService { * @param updatePaymentDto * @returns */ - async update(id: string, updatePaymentDto: UpdatePaymentDto): Promise { + async update(paymentId: string, updatePaymentDto: UpdatePaymentDto): Promise { try { // execute the below in prisma transaction return await this.prisma.$transaction(async (tx) => { const currentPayment = await tx.payment.findFirst({ - where: { id }, + where: { id: paymentId }, include: { donations: { select: { personId: true, targetVaultId: true }, @@ -693,7 +693,7 @@ export class DonationsService { }, }) if (!currentPayment) { - throw new NotFoundException(`Update failed. No donation found with ID: ${id}`) + throw new NotFoundException(`Update failed. No payment found with ID: ${paymentId}`) } if ( @@ -730,23 +730,18 @@ export class DonationsService { } const donation = await tx.payment.update({ - where: { id }, + where: { id: paymentId }, data: { status: status, donations: { - updateMany: { - where: { paymentId: id }, + update: { + where: { id: updatePaymentDto.donationId ?? '' }, data: { personId: updatePaymentDto.targetPersonId ? donorId : undefined, }, }, }, billingEmail: updatePaymentDto.billingEmail ? billingEmail : undefined, - //In case of personId or billingEmail change, take the last updatedAt property to prevent any changes to updatedAt property - updatedAt: - updatePaymentDto.targetPersonId || updatePaymentDto.billingEmail - ? currentPayment.updatedAt - : undefined, }, }) diff --git a/apps/api/src/donations/dto/update-payment.dto.ts b/apps/api/src/donations/dto/update-payment.dto.ts index 2835ca608..8ad7f1522 100644 --- a/apps/api/src/donations/dto/update-payment.dto.ts +++ b/apps/api/src/donations/dto/update-payment.dto.ts @@ -14,5 +14,10 @@ export class UpdatePaymentDto extends PartialType(CreatePaymentDto) { @Expose() @ApiProperty() @IsOptional() - billingEmail?: string + billingEmail?: string + + @Expose() + @ApiProperty() + @IsOptional() + donationId?: string } From 8c03c472560bea3fc5f9445ef6cf6645282c6290 Mon Sep 17 00:00:00 2001 From: Alexander Petkov Date: Wed, 24 Apr 2024 16:25:14 +0300 Subject: [PATCH 2/2] test: Fix tests --- .../donations/donations.controller.spec.ts | 70 +++++-------------- 1 file changed, 18 insertions(+), 52 deletions(-) diff --git a/apps/api/src/donations/donations.controller.spec.ts b/apps/api/src/donations/donations.controller.spec.ts index a1e894e35..43d06895d 100644 --- a/apps/api/src/donations/donations.controller.spec.ts +++ b/apps/api/src/donations/donations.controller.spec.ts @@ -26,6 +26,7 @@ import { UpdatePaymentDto } from './dto/update-payment.dto' import { CACHE_MANAGER } from '@nestjs/cache-manager' import { MarketingNotificationsModule } from '../notifications/notifications.module' import type { PaymentWithDonation } from './types/donation' +import { personMock } from '../person/__mock__/personMock' describe('DonationsController', () => { let controller: DonationsController @@ -59,26 +60,26 @@ describe('DonationsController', () => { targetVaultId: '1000', createdAt: new Date('2022-01-01'), updatedAt: new Date('2022-01-02'), - personId: '1', + personId: personMock.id, person: { - id: '1', + id: personMock.id, keycloakId: '00000000-0000-0000-0000-000000000015', }, } const mockPayment: PaymentWithDonation = { - id: '123', + id: personMock.id, provider: PaymentProvider.bank, currency: Currency.BGN, type: 'single', status: PaymentStatus.succeeded, amount: 10, affiliateId: null, - extCustomerId: 'gosho', + extCustomerId: '', extPaymentIntentId: 'pm1', extPaymentMethodId: 'bank', - billingEmail: 'gosho1@abv.bg', - billingName: 'gosho1', + billingEmail: personMock.email, + billingName: 'Admin Dev', chargedAmount: 10.5, createdAt: new Date('2022-01-01'), updatedAt: new Date('2022-01-02'), @@ -191,28 +192,11 @@ describe('DonationsController', () => { const updatePaymentDto = { amount: 10, targetPersonId: '2', + donationId: '123', } const existingPayment = { ...mockPayment } - const existingTargetPerson: Person = { - id: '2', - firstName: 'string', - lastName: 'string', - email: 'string', - phone: 'string', - companyId: 'string', - createdAt: new Date('2022-01-01'), - updatedAt: new Date('2022-01-01'), - newsletter: false, - address: 'string', - birthday: new Date('2002-07-07'), - emailConfirmed: true, - personalNumber: 'string', - keycloakId: '00000000-0000-0000-0000-000000000012', - stripeCustomerId: 'string', - picture: 'string', - profileEnabled: true, - } + const existingTargetPerson: Person = personMock jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock)) const mockedIncrementVaultAmount = jest @@ -229,13 +213,13 @@ describe('DonationsController', () => { expect(prismaMock.payment.update).toHaveBeenCalledWith({ where: { id: '123' }, data: { - status: existingPayment.status, - updatedAt: existingPayment.updatedAt, + status: PaymentStatus.succeeded, + billingEmail: undefined, donations: { - updateMany: { - where: { paymentId: existingPayment.id }, + update: { + where: { id: updatePaymentDto.donationId }, data: { - personId: '2', + personId: existingPayment.donations[0].personId, }, }, }, @@ -251,27 +235,10 @@ describe('DonationsController', () => { status: PaymentStatus.succeeded, targetPersonId: mockDonation.personId, billingEmail: mockPayment.billingEmail as string, + donationId: '123', } - const existingTargetPerson: Person = { - id: mockDonation.personId, - firstName: 'string', - lastName: 'string', - email: mockPayment.billingEmail, - phone: 'string', - companyId: 'string', - createdAt: new Date('2022-01-01'), - updatedAt: new Date('2022-01-01'), - newsletter: false, - address: 'string', - birthday: new Date('2002-07-07'), - emailConfirmed: true, - personalNumber: 'string', - keycloakId: '00000000-0000-0000-0000-000000000012', - stripeCustomerId: 'string', - picture: 'string', - profileEnabled: true, - } + const existingTargetPerson: Person = personMock const existingPayment = { ...mockPayment, status: PaymentStatus.initial } const expectedUpdatedPayment = { ...existingPayment, status: PaymentStatus.succeeded } @@ -292,10 +259,9 @@ describe('DonationsController', () => { data: { status: PaymentStatus.succeeded, billingEmail: updatePaymentDto.billingEmail, - updatedAt: expectedUpdatedPayment.updatedAt, donations: { - updateMany: { - where: { paymentId: existingPayment.id }, + update: { + where: { id: updatePaymentDto.donationId }, data: { personId: existingPayment.donations[0].personId, },