Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve not being able to update payment/donation #629

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -11,7 +11,7 @@
PaymentProvider,
Person,
Vault,
Payment,

Check warning on line 14 in apps/api/src/donations/donations.controller.spec.ts

View workflow job for this annotation

GitHub Actions / Run API tests

'Payment' is defined but never used
} from '@prisma/client'
import { CampaignService } from '../campaign/campaign.service'
import { ExportService } from '../export/export.service'
Expand All @@ -26,6 +26,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@ -24,7 +24,7 @@
import { CreateSessionDto } from './dto/create-session.dto'
import { UpdatePaymentDto } from './dto/update-payment.dto'

import { DonationBaseDto, ListDonationsDto } from './dto/list-donations.dto'

Check warning on line 27 in apps/api/src/donations/donations.service.ts

View workflow job for this annotation

GitHub Actions / Run API tests

'DonationBaseDto' is defined but never used
import { donationWithPerson } from './queries/donation.validator'
import { CreateStripePaymentDto } from './dto/create-stripe-payment.dto'
import { ImportStatus } from '../bank-transactions-file/dto/bank-transactions-import-status.dto'
Expand Down Expand Up @@ -680,20 +680,20 @@
* @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 @@
}

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
}
Loading