-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/update donation person (#348)
* #958 added update of a donation's donor * #958 changed keycloakid to personid * code-review fixes
- Loading branch information
Showing
3 changed files
with
170 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,15 @@ import { STRIPE_CLIENT_TOKEN } from '@golevelup/nestjs-stripe' | |
import { NotAcceptableException } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { Campaign, CampaignState } from '@prisma/client' | ||
import { | ||
Campaign, | ||
CampaignState, | ||
Currency, | ||
DonationStatus, | ||
DonationType, | ||
PaymentProvider, | ||
Prisma, | ||
} from '@prisma/client' | ||
import { CampaignService } from '../campaign/campaign.service' | ||
import { PersonService } from '../person/person.service' | ||
import { MockPrismaService, prismaMock } from '../prisma/prisma-client.mock' | ||
|
@@ -26,6 +34,32 @@ describe('DonationsController', () => { | |
cancelUrl: 'http://test.com', | ||
isAnonymous: true, | ||
} as CreateSessionDto | ||
const vaultMock = { | ||
incrementVaultAmount: jest.fn(), | ||
} | ||
|
||
const mockDonation = { | ||
id: '123', | ||
provider: PaymentProvider.bank, | ||
currency: Currency.BGN, | ||
type: DonationType.donation, | ||
status: DonationStatus.succeeded, | ||
amount: 10, | ||
extCustomerId: 'gosho', | ||
extPaymentIntentId: 'pm1', | ||
extPaymentMethodId: 'bank', | ||
billingEmail: '[email protected]', | ||
billingName: 'gosho1', | ||
targetVaultId: '1000', | ||
chargedAmount: 10.5, | ||
createdAt: new Date('2022-01-01'), | ||
updatedAt: new Date('2022-01-02'), | ||
personId: '1', | ||
person: { | ||
id: '1', | ||
keycloakId: '00000000-0000-0000-0000-000000000015', | ||
}, | ||
} | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
|
@@ -39,7 +73,10 @@ describe('DonationsController', () => { | |
}, | ||
CampaignService, | ||
DonationsService, | ||
VaultService, | ||
{ | ||
provide: VaultService, | ||
useValue: vaultMock, | ||
}, | ||
MockPrismaService, | ||
{ | ||
provide: STRIPE_CLIENT_TOKEN, | ||
|
@@ -109,4 +146,78 @@ describe('DonationsController', () => { | |
expect(prismaMock.campaign.findFirst).toHaveBeenCalled() | ||
expect(stripeMock.checkout.sessions.create).toHaveBeenCalled() | ||
}) | ||
|
||
it('should update a donations donor, when it is changed', async () => { | ||
const updatePaymentDto = { | ||
type: DonationType.donation, | ||
amount: 10, | ||
targetPersonId: '2', | ||
} | ||
|
||
const existingDonation = { ...mockDonation } | ||
const existingTargetPerson = { | ||
id: '2', | ||
firstName: 'string', | ||
lastName: 'string', | ||
email: 'string', | ||
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', | ||
} | ||
|
||
prismaMock.donation.findFirst.mockResolvedValueOnce(existingDonation) | ||
prismaMock.person.findFirst.mockResolvedValueOnce(existingTargetPerson) | ||
|
||
// act | ||
await controller.update('123', updatePaymentDto) | ||
|
||
// assert | ||
expect(prismaMock.donation.update).toHaveBeenCalledWith({ | ||
where: { id: '123' }, | ||
data: { | ||
status: existingDonation.status, | ||
personId: '2', | ||
}, | ||
}) | ||
expect(vaultMock.incrementVaultAmount).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('should update a donation status, when it is changed', async () => { | ||
const updatePaymentDto = { | ||
type: DonationType.donation, | ||
amount: 10, | ||
status: DonationStatus.succeeded, | ||
} | ||
|
||
const existingDonation = { ...mockDonation, status: DonationStatus.initial } | ||
const expectedUpdatedDonation = {...existingDonation, status: DonationStatus.succeeded } | ||
|
||
prismaMock.donation.findFirst.mockResolvedValueOnce(existingDonation) | ||
prismaMock.donation.update.mockResolvedValueOnce(expectedUpdatedDonation) | ||
|
||
// act | ||
await controller.update('123', updatePaymentDto) | ||
|
||
// assert | ||
expect(prismaMock.donation.update).toHaveBeenCalledWith({ | ||
where: { id: '123' }, | ||
data: { | ||
status: DonationStatus.succeeded, | ||
personId: '1', | ||
}, | ||
}) | ||
expect(vaultMock.incrementVaultAmount).toHaveBeenCalledWith( | ||
existingDonation.targetVaultId, | ||
existingDonation.amount, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
import { PartialType } from '@nestjs/mapped-types' | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { Expose } from 'class-transformer' | ||
import { IsOptional, IsUUID } from 'class-validator' | ||
import { CreatePaymentDto } from './create-payment.dto' | ||
|
||
export class UpdatePaymentDto extends PartialType(CreatePaymentDto) {} | ||
export class UpdatePaymentDto extends PartialType(CreatePaymentDto) { | ||
@Expose() | ||
@ApiProperty() | ||
@IsOptional() | ||
@IsUUID() | ||
targetPersonId?: string | ||
} |
78c3c95
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report
Test suite run success
180 tests passing in 64 suites.
Report generated by 🧪jest coverage report action from 78c3c95