Skip to content

Commit

Permalink
fix: Resolve not being able to update payment/donation (#629)
Browse files Browse the repository at this point in the history
* fix: Resolve not being able to update payment/donation

* test: Fix tests
  • Loading branch information
sashko9807 authored Apr 24, 2024
1 parent 6414873 commit daa7b74
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 64 deletions.
70 changes: 18 additions & 52 deletions apps/api/src/donations/donations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: '[email protected]',
billingName: 'gosho1',
billingEmail: personMock.email,
billingName: 'Admin Dev',
chargedAmount: 10.5,
createdAt: new Date('2022-01-01'),
updatedAt: new Date('2022-01-02'),
Expand Down Expand Up @@ -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
Expand All @@ -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,
},
},
},
Expand All @@ -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 }
Expand All @@ -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,
},
Expand Down
17 changes: 6 additions & 11 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,20 +680,20 @@ export class DonationsService {
* @param updatePaymentDto
* @returns
*/
async update(id: string, updatePaymentDto: UpdatePaymentDto): Promise<Payment> {
async update(paymentId: string, updatePaymentDto: UpdatePaymentDto): Promise<Payment> {
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 },
},
},
})
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 (
Expand Down Expand Up @@ -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,
},
})

Expand Down
7 changes: 6 additions & 1 deletion apps/api/src/donations/dto/update-payment.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ export class UpdatePaymentDto extends PartialType(CreatePaymentDto) {
@Expose()
@ApiProperty()
@IsOptional()
billingEmail?: string
billingEmail?: string

@Expose()
@ApiProperty()
@IsOptional()
donationId?: string
}

0 comments on commit daa7b74

Please sign in to comment.