Skip to content

Commit

Permalink
src/donations: Allow billingEmail to be edited (#510)
Browse files Browse the repository at this point in the history
* src/donations: Allow billingEmail to be edited

* donations.controller.spec: Fix donation update test
  • Loading branch information
sashko9807 authored Jul 3, 2023
1 parent 2c518b2 commit 2976011
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
28 changes: 26 additions & 2 deletions apps/api/src/donations/donations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { VaultService } from '../vault/vault.service'
import { DonationsController } from './donations.controller'
import { DonationsService } from './donations.service'
import { CreateSessionDto } from './dto/create-session.dto'
import { UpdatePaymentDto } from './dto/update-payment.dto'

describe('DonationsController', () => {
let controller: DonationsController
Expand Down Expand Up @@ -210,16 +211,38 @@ describe('DonationsController', () => {
})

it('should update a donation status, when it is changed', async () => {
const updatePaymentDto = {
const updatePaymentDto: UpdatePaymentDto = {
type: DonationType.donation,
amount: 10,
status: DonationStatus.succeeded,
targetPersonId: mockDonation.personId,
billingEmail: mockDonation.billingEmail,
}

const existingTargetPerson = {
id: mockDonation.personId,
firstName: 'string',
lastName: 'string',
email: mockDonation.billingEmail,
phone: 'string',
company: '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',
}

const existingDonation = { ...mockDonation, status: DonationStatus.initial }
const expectedUpdatedDonation = { ...existingDonation, status: DonationStatus.succeeded }

prismaMock.donation.findFirst.mockResolvedValueOnce(existingDonation)
prismaMock.person.findFirst.mockResolvedValueOnce(existingTargetPerson)
prismaMock.donation.update.mockResolvedValueOnce(expectedUpdatedDonation)

// act
Expand All @@ -230,7 +253,8 @@ describe('DonationsController', () => {
where: { id: '123' },
data: {
status: DonationStatus.succeeded,
personId: '1',
personId: updatePaymentDto.targetPersonId,
billingEmail: updatePaymentDto.billingEmail,
},
})
expect(vaultMock.incrementVaultAmount).toHaveBeenCalledWith(
Expand Down
15 changes: 12 additions & 3 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,26 +577,35 @@ export class DonationsService {

const status = updatePaymentDto.status || currentDonation.status
let donorId = currentDonation.personId
let billingEmail = ''
if (
updatePaymentDto.targetPersonId &&
currentDonation.personId !== updatePaymentDto.targetPersonId
currentDonation.personId !== updatePaymentDto.targetPersonId ||
updatePaymentDto.billingEmail
) {
const targetDonor = await this.prisma.person.findFirst({
where: { id: updatePaymentDto.targetPersonId },
where: {
OR: [
{id: updatePaymentDto.targetPersonId },
{email: updatePaymentDto.billingEmail}
]
},
})
if (!targetDonor) {
throw new NotFoundException(
`Update failed. No person found with ID: ${updatePaymentDto.targetPersonId}`,
)
}
donorId = targetDonor.id
billingEmail = targetDonor.email
}

const donation = await this.prisma.donation.update({
where: { id },
data: {
status: status,
personId: donorId,
personId: updatePaymentDto.targetPersonId ? donorId : undefined,
billingEmail: updatePaymentDto.billingEmail ? billingEmail : undefined
},
})

Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/donations/dto/update-payment.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ export class UpdatePaymentDto extends PartialType(CreatePaymentDto) {
@IsOptional()
@IsUUID()
targetPersonId?: string

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

0 comments on commit 2976011

Please sign in to comment.