Skip to content

Commit

Permalink
src/donations: Order public donations by updatedAt field (#520)
Browse files Browse the repository at this point in the history
* src/donations: Order public donations by updatedAt property
When automatically importing bank transfers they are all created at the same time. As we currently order public donations by createdAt field, and the query is ran on every page change, this may cause unexpected behavior such as repeating of some bank donations in different pages, or some donations not being shown.
Instead use the updatedAt property which represents the exact time, donation record was added to database, or the last time its status has been updated. This should give us good approximation time of the donation itself

Tests:
Call the endpoint responsible for listing donations:
API_URL/api/v1/donation/listPublic/?campaignId=campaignId where campaignId, is the id of the campaign in local enviroment.
Public donations should be ordered by their updatedAt property

* src/donations: Don't update updateAt on person/billing email changes
If we are to order the donation payments by their updateAt property, it should only be changed on payment status change.

Tests:
Go to admin panel->Donations, Click F12 go to Network, and find the API response for the listed donations. Expand the first record and see its updatedAt property
Change donor field of the first record, of listed donations.  The updatedAt property of the first record, should be the same as in the previous API response.
Repeat the same for the billingEmail field

* src/donations: Adjusting tests
  • Loading branch information
sashko9807 committed Jul 25, 2023
1 parent d718269 commit e61e990
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/donations/donations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ describe('DonationsController', () => {
data: {
status: existingDonation.status,
personId: '2',
updatedAt: existingDonation.updatedAt,
},
})
expect(vaultMock.incrementVaultAmount).toHaveBeenCalledTimes(0)
Expand Down Expand Up @@ -257,6 +258,7 @@ describe('DonationsController', () => {
status: DonationStatus.succeeded,
personId: updatePaymentDto.targetPersonId,
billingEmail: updatePaymentDto.billingEmail,
updatedAt: expectedUpdatedDonation.updatedAt,
},
})
expect(vaultMock.incrementVaultAmount).toHaveBeenCalledWith(
Expand Down
20 changes: 11 additions & 9 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class DonationsService {
): Promise<ListDonationsDto<DonationBaseDto>> {
const data = await this.prisma.donation.findMany({
where: { status, targetVault: { campaign: { id: campaignId } } },
orderBy: [{ createdAt: 'desc' }],
orderBy: [{ updatedAt: 'desc' }],
select: {
id: true,
type: true,
Expand Down Expand Up @@ -579,16 +579,13 @@ export class DonationsService {
let donorId = currentDonation.personId
let billingEmail = ''
if (
updatePaymentDto.targetPersonId &&
currentDonation.personId !== updatePaymentDto.targetPersonId ||
(updatePaymentDto.targetPersonId &&
currentDonation.personId !== updatePaymentDto.targetPersonId) ||
updatePaymentDto.billingEmail
) {
const targetDonor = await this.prisma.person.findFirst({
where: {
OR: [
{id: updatePaymentDto.targetPersonId },
{email: updatePaymentDto.billingEmail}
]
OR: [{ id: updatePaymentDto.targetPersonId }, { email: updatePaymentDto.billingEmail }],
},
})
if (!targetDonor) {
Expand All @@ -604,8 +601,13 @@ export class DonationsService {
where: { id },
data: {
status: status,
personId: updatePaymentDto.targetPersonId ? donorId : undefined,
billingEmail: updatePaymentDto.billingEmail ? billingEmail : undefined
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
? currentDonation.updatedAt
: undefined,
},
})

Expand Down

0 comments on commit e61e990

Please sign in to comment.