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

Invalidate a successfull donation #597

Merged
merged 10 commits into from
Jan 25, 2024
24 changes: 24 additions & 0 deletions apps/api/src/donations/donations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,28 @@ describe('DonationsController', () => {
reason: 'requested_by_customer',
})
})

it('should invalidate a donation and update the vault if needed', async () => {
const existingDonation = { ...mockDonation, status: DonationStatus.succeeded }
jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock))

prismaMock.donation.findFirstOrThrow.mockResolvedValueOnce(existingDonation)

await controller.invalidate('123')

expect(prismaMock.donation.update).toHaveBeenCalledWith({
where: { id: '123' },
data: {
status: DonationStatus.invalid,
},
})
expect(prismaMock.vault.update).toHaveBeenCalledWith({
where: { id: existingDonation.targetVaultId },
data: {
amount: {
decrement: existingDonation.amount,
},
},
})
})
})
18 changes: 15 additions & 3 deletions apps/api/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { ApiQuery, ApiTags } from '@nestjs/swagger'
import { DonationStatus } from '@prisma/client'
import { AuthenticatedUser, Public, RoleMatchingMode, Roles } from 'nest-keycloak-connect'
import { RealmViewSupporters, ViewSupporters } from '@podkrepi-bg/podkrepi-types'
import { RealmViewSupporters, ViewSupporters, EditFinancialsRequests } from '@podkrepi-bg/podkrepi-types'

import { isAdmin, KeycloakTokenParsed } from '../auth/keycloak'
import { DonationsService } from './donations.service'
Expand Down Expand Up @@ -221,7 +221,7 @@ export class DonationsController {

@Post('/refund-stripe-payment/:id')
@Roles({
roles: [RealmViewSupporters.role, ViewSupporters.role],
roles: [EditFinancialsRequests.role],
mode: RoleMatchingMode.ANY,
})
refundStripePaymet(@Param('id') paymentIntentId: string) {
Expand All @@ -240,6 +240,16 @@ export class DonationsController {
return this.donationsService.createUpdateBankPayment(bankPaymentDto)
}

@Patch('/:id/invalidate')
@Roles({
roles: [EditFinancialsRequests.role],
mode: RoleMatchingMode.ANY,
})
invalidate(@Param('id') id: string) {
Logger.debug(`Invalidating donation with id ${id}`)
return this.donationsService.invalidate(id)
}

@Patch(':id')
@Roles({
roles: [RealmViewSupporters.role, ViewSupporters.role],
Expand All @@ -251,12 +261,14 @@ export class DonationsController {
@Body()
updatePaymentDto: UpdatePaymentDto,
) {
Logger.debug(`Updating donation with id ${id}`)

return this.donationsService.update(id, updatePaymentDto)
}

@Post('delete')
@Roles({
roles: [RealmViewSupporters.role, ViewSupporters.role],
roles: [EditFinancialsRequests.role],
mode: RoleMatchingMode.ANY,
})
delete(
Expand Down
25 changes: 25 additions & 0 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { CreateBankPaymentDto } from './dto/create-bank-payment.dto'
import { CreateSessionDto } from './dto/create-session.dto'
import { UpdatePaymentDto } from './dto/update-payment.dto'
import { Person } from '../person/entities/person.entity'

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

View workflow job for this annotation

GitHub Actions / Run API tests

'Person' is defined but never used
import { DonationBaseDto, ListDonationsDto } from './dto/list-donations.dto'
import { donationWithPerson, DonationWithPerson } from './queries/donation.validator'
import { CreateStripePaymentDto } from './dto/create-stripe-payment.dto'
Expand Down Expand Up @@ -712,6 +712,31 @@
}
}

async invalidate(id: string) {
try {
await this.prisma.$transaction(async (tx) => {
const donation = await this.getDonationById(id)

if (donation.status === DonationStatus.succeeded) {
await this.vaultService.decrementVaultAmount(donation.targetVaultId, donation.amount, tx)
}

await this.prisma.donation.update({
where: { id },
data: {
status: DonationStatus.invalid,
},
})
})
} catch (err) {
Logger.warn(err.message || err)
const msg = `Invalidation failed. No Donation found with given ID.`

Logger.warn(msg)
throw new NotFoundException(msg)
}
}

async getDonationsByUser(keycloakId: string, email?: string) {
const donations = await this.prisma.donation.findMany({
where: {
Expand Down
3 changes: 3 additions & 0 deletions libs/podkrepi-types/src/lib/roles/team/edit-financials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class EditFinancialsRequests {
static readonly role = 'realm:account-edit-financials-requests'
}
1 change: 1 addition & 0 deletions libs/podkrepi-types/src/lib/roles/team/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './view-supporters'
export * from './view-contact-requests'
export * from './edit-financials'
Loading