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

Update prisma to 5.1.1 #532

Merged
merged 2 commits into from
Aug 18, 2023
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
7 changes: 3 additions & 4 deletions apps/api/src/benefactor/benefactor.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Benefactor } from '@prisma/client'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime'
import { Prisma } from '@prisma/client'
import { Injectable, Logger, NotFoundException } from '@nestjs/common'

import { PrismaService } from '../prisma/prisma.service'
Expand All @@ -20,7 +20,7 @@ export class BenefactorService {

async findOne(id: string): Promise<Benefactor> {
try {
return await this.prisma.benefactor.findFirst({
return await this.prisma.benefactor.findFirstOrThrow({
where: { id },
include: {
person: {
Expand All @@ -30,7 +30,6 @@ export class BenefactorService {
},
},
},
rejectOnNotFound: true,
})
} catch (err) {
const msg = `No Document found with ID: ${id}`
Expand All @@ -48,7 +47,7 @@ export class BenefactorService {
})
return result
} catch (error) {
if (error instanceof PrismaClientKnownRequestError) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
Logger.warn('No record with id', +id)
throw new NotFoundException('No record with id' + id)
}
Expand Down
4 changes: 1 addition & 3 deletions apps/api/src/config/shutdown.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { INestApplication, ShutdownSignal } from '@nestjs/common'
import { PrismaService } from '../prisma/prisma.service'

Check warning on line 2 in apps/api/src/config/shutdown.config.ts

View workflow job for this annotation

GitHub Actions / Run API tests

'PrismaService' is defined but never used

export function setupShutdownHooks(app: INestApplication) {
const prismaService: PrismaService = app.get(PrismaService)
prismaService.enableShutdownHooks(app)

// https://www.prisma.io/docs/guides/upgrade-guides/upgrading-versions/upgrading-to-prisma-5
app.enableShutdownHooks([ShutdownSignal.SIGINT, ShutdownSignal.SIGTERM])
}
7 changes: 3 additions & 4 deletions apps/api/src/country/country.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,24 @@ describe('CountryController', () => {

it('should get 1 country', async () => {
const country = mockData[0]
prismaMock.country.findFirst.mockResolvedValue(country)
prismaMock.country.findFirstOrThrow.mockResolvedValue(country)

const result = await controller.findOne(country.id)
expect(result).toEqual(country)
expect(prismaMock.country.findFirst).toHaveBeenCalledWith({
expect(prismaMock.country.findFirstOrThrow).toHaveBeenCalledWith({
where: {
id: country.id,
},
include: {
cities: true,
},
rejectOnNotFound: true,
})
})

it('should throw error if trying to get a country that does not exist', async () => {
const notExistingId = '12345'

const prismaSpy = jest.spyOn(prismaMock.country, 'findFirst').mockImplementation(() => {
const prismaSpy = jest.spyOn(prismaMock.country, 'findFirstOrThrow').mockImplementation(() => {
const msg = 'No Country record with ID: ' + notExistingId
throw new NotFoundException(msg)
})
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/country/country.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ export class CountryService {

async getCountryById(id: string): Promise<Country> {
try {
const country = await this.prisma.country.findFirst({
const country = await this.prisma.country.findFirstOrThrow({
where: {
id: id,
},
include: {
cities: true,
},
rejectOnNotFound: true,
})
return country
} catch (err) {
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/document/document.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ export class DocumentService {

async findOne(id: string): Promise<Document> {
try {
return await this.prisma.document.findFirst({
return await this.prisma.document.findFirstOrThrow({
where: {
id,
},
rejectOnNotFound: true,
})
} catch (err) {
const msg = `No Document found with ID: ${id}`
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,8 @@ export class DonationsService {
*/
async getDonationById(id: string): Promise<Donation> {
try {
const donation = await this.prisma.donation.findFirst({
const donation = await this.prisma.donation.findFirstOrThrow({
where: { id },
rejectOnNotFound: true,
})
return donation
} catch (err) {
Expand Down
18 changes: 9 additions & 9 deletions apps/api/src/expenses/expenses.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ describe('ExpensesController', () => {

prismaMock.person.findFirst.mockResolvedValue(person)
prismaMock.campaign.findFirst.mockResolvedValue(campaign)
prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.expense.findFirst.mockResolvedValue(expense)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.expense.findFirstOrThrow.mockResolvedValue(expense)
prismaMock.vault.update.mockResolvedValue(vault)
prismaMock.expense.update.mockResolvedValue(expense)
prismaMock.$transaction.mockResolvedValue([expense, vault])
Expand Down Expand Up @@ -181,8 +181,8 @@ describe('ExpensesController', () => {
sub: '00000000-0000-0000-0000-000000000012',
}

prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.expense.findFirst.mockResolvedValue(expense)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.expense.findFirstOrThrow.mockResolvedValue(expense)
prismaMock.vault.update.mockResolvedValue(vault)
prismaMock.expense.update.mockResolvedValue(expense)
prismaMock.$transaction.mockResolvedValue([expense, vault])
Expand Down Expand Up @@ -222,9 +222,9 @@ describe('ExpensesController', () => {
sub: '00000000-0000-0000-0000-000000000012',
}

prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.expense.findFirst.mockResolvedValueOnce(approvedExpense)
prismaMock.expense.findFirst.mockResolvedValueOnce(cancelledExpense)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.expense.findFirstOrThrow.mockResolvedValueOnce(approvedExpense)
prismaMock.expense.findFirstOrThrow.mockResolvedValueOnce(cancelledExpense)

const updateDto: UpdateExpenseDto = {
...approvedExpense,
Expand Down Expand Up @@ -252,8 +252,8 @@ describe('ExpensesController', () => {
amount: 1000,
blockedAmount: 350,
}
prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.expense.findFirst.mockResolvedValueOnce(expense)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.expense.findFirstOrThrow.mockResolvedValueOnce(expense)

const updateDto: UpdateExpenseDto = {
...expense,
Expand Down
6 changes: 2 additions & 4 deletions apps/api/src/expenses/expenses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ export class ExpensesService {
* Updates an expense, where status changes to approved/canceled state will finilize the expense and perform vault transaction.
*/
async update(id: string, dto: UpdateExpenseDto) {
const expense = await this.prisma.expense.findFirst({
const expense = await this.prisma.expense.findFirstOrThrow({
where: { id: id },
rejectOnNotFound: true,
})
if (
[ExpenseStatus.approved.valueOf(), ExpenseStatus.canceled.valueOf()].includes(
Expand All @@ -77,11 +76,10 @@ export class ExpensesService {
throw new BadRequestException('Vault or amount cannot be changed.')
}

const vault = await this.prisma.vault.findFirst({
const vault = await this.prisma.vault.findFirstOrThrow({
where: {
id: expense.vaultId,
},
rejectOnNotFound: true,
})

// TODO: figure out how to initialize empty vault promise
Expand Down
6 changes: 0 additions & 6 deletions apps/api/src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrismaClient } from '@prisma/client'
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'

Check warning on line 2 in apps/api/src/prisma/prisma.service.ts

View workflow job for this annotation

GitHub Actions / Run API tests

'INestApplication' is defined but never used

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
Expand All @@ -16,10 +16,4 @@

await this.$connect()
}

async enableShutdownHooks(app: INestApplication) {
this.$on('beforeExit', async () => {
await app.close()
})
}
}
14 changes: 7 additions & 7 deletions apps/api/src/transfer/transfer.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('TransferController', () => {

prismaMock.transfer.create.mockResolvedValue(transfer)
prismaMock.vault.update.mockResolvedValue(vault)
prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.$transaction.mockResolvedValue([transfer, vault])

const createDto: CreateTransferDto = { ...transfer }
Expand Down Expand Up @@ -198,9 +198,9 @@ describe('TransferController', () => {
blockedAmount: 0,
}

prismaMock.transfer.findFirst.mockResolvedValue(transfer)
prismaMock.vault.findFirst.mockResolvedValueOnce(srcVault)
prismaMock.vault.findFirst.mockResolvedValueOnce(dstVault)
prismaMock.transfer.findFirstOrThrow.mockResolvedValue(transfer)
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(srcVault)
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(dstVault)
prismaMock.transfer.update.mockResolvedValue(transfer)
prismaMock.vault.update.mockResolvedValueOnce(srcVault)
prismaMock.vault.update.mockResolvedValueOnce(dstVault)
Expand Down Expand Up @@ -256,9 +256,9 @@ describe('TransferController', () => {
blockedAmount: 0,
}

prismaMock.transfer.findFirst.mockResolvedValue(transfer)
prismaMock.vault.findFirst.mockResolvedValueOnce(srcVault)
prismaMock.vault.findFirst.mockResolvedValueOnce(dstVault)
prismaMock.transfer.findFirstOrThrow.mockResolvedValue(transfer)
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(srcVault)
prismaMock.vault.findFirstOrThrow.mockResolvedValueOnce(dstVault)
prismaMock.transfer.update.mockResolvedValue(transfer)
prismaMock.vault.update.mockResolvedValueOnce(srcVault)
prismaMock.vault.update.mockResolvedValueOnce(dstVault)
Expand Down
18 changes: 9 additions & 9 deletions apps/api/src/transfer/transfer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ export class TransferService {
* Creates a transfer, while blocking the corresponding amount in the source vault.
*/
async create(createTransferDto: CreateTransferDto): Promise<Transfer | undefined> {
const sourceVault = await this.prisma.vault.findFirst({
const sourceVault = await this.prisma.vault.findFirstOrThrow({
where: {
id: createTransferDto.sourceVaultId,
},
rejectOnNotFound: true,
})

if (sourceVault.amount - sourceVault.blockedAmount - createTransferDto.amount < 0) {
Expand Down Expand Up @@ -70,13 +69,16 @@ export class TransferService {
* Updates a transfer, where status changes to completed/declined state will finilize the transfer and perform vault transaction between source and target.
*/
async update(id: string, updateTransferDto: UpdateTransferDto): Promise<Transfer | null> {
const transfer = await this.prisma.transfer.findFirst({
const transfer = await this.prisma.transfer.findFirstOrThrow({
where: { id: id },
rejectOnNotFound: true,
})

if (
[ TransferStatus.succeeded.valueOf(), TransferStatus.cancelled.valueOf(), TransferStatus.declined.valueOf()].includes(transfer.status.valueOf())
[
TransferStatus.succeeded.valueOf(),
TransferStatus.cancelled.valueOf(),
TransferStatus.declined.valueOf(),
].includes(transfer.status.valueOf())
) {
throw new BadRequestException('Transfer has already been finilized and cannot be updated')
}
Expand All @@ -91,17 +93,15 @@ export class TransferService {
}

// TODO: figure out how to initialize empty vault promise
const srcVault = await this.prisma.vault.findFirst({
const srcVault = await this.prisma.vault.findFirstOrThrow({
where: {
id: transfer.sourceVaultId,
},
rejectOnNotFound: true,
})
const targetVault = await this.prisma.vault.findFirst({
const targetVault = await this.prisma.vault.findFirstOrThrow({
where: {
id: transfer.targetVaultId,
},
rejectOnNotFound: true,
})

let writeSrcVault = this.prisma.vault.update({
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/vault/vault.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('VaultController', () => {
})

it('should call remove on empty vaults', async () => {
prismaMock.vault.findFirst.mockResolvedValue({
prismaMock.vault.findFirstOrThrow.mockResolvedValue({
id: vaultId,
name: 'vault1',
currency: 'BGN',
Expand Down
6 changes: 2 additions & 4 deletions apps/api/src/vault/vault.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ export class VaultService {
}
async findOne(id: string): Promise<Vault> {
try {
return await this.prisma.vault.findFirst({
return await this.prisma.vault.findFirstOrThrow({
where: {
id,
},
rejectOnNotFound: true,
})
} catch (err) {
const msg = `No Vault found with ID: ${id} Exception was: ${err.message}`
Expand Down Expand Up @@ -71,11 +70,10 @@ export class VaultService {
}

async remove(id: string): Promise<Vault> {
const vault = await this.prisma.vault.findFirst({
const vault = await this.prisma.vault.findFirstOrThrow({
where: {
id,
},
rejectOnNotFound: true,
})
if (vault.amount != 0 || vault.blockedAmount != 0) {
throw new BadRequestException('Cannot delete non-empty vaults!')
Expand Down
10 changes: 5 additions & 5 deletions apps/api/src/withdrawal/withdrawal.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('WithdrawalController', () => {
}
prismaMock.withdrawal.create.mockResolvedValue(withdrawal)
prismaMock.vault.update.mockResolvedValue(vault)
prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.$transaction.mockResolvedValue([withdrawal, vault])

const createDto: CreateWithdrawalDto = {
Expand Down Expand Up @@ -193,8 +193,8 @@ describe('WithdrawalController', () => {
amount: 1000,
blockedAmount: 350,
}
prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.withdrawal.findFirst.mockResolvedValue(withdrawal)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.withdrawal.findFirstOrThrow.mockResolvedValue(withdrawal)
prismaMock.vault.update.mockResolvedValue(vault)
prismaMock.withdrawal.update.mockResolvedValue(withdrawal)
prismaMock.$transaction.mockResolvedValue([withdrawal, vault])
Expand Down Expand Up @@ -242,8 +242,8 @@ describe('WithdrawalController', () => {
amount: 1000,
blockedAmount: 350,
}
prismaMock.vault.findFirst.mockResolvedValue(vault)
prismaMock.withdrawal.findFirst.mockResolvedValue(withdrawal)
prismaMock.vault.findFirstOrThrow.mockResolvedValue(vault)
prismaMock.withdrawal.findFirstOrThrow.mockResolvedValue(withdrawal)
prismaMock.vault.update.mockResolvedValue(vault)
prismaMock.withdrawal.update.mockResolvedValue(withdrawal)
prismaMock.$transaction.mockResolvedValue([withdrawal, vault])
Expand Down
16 changes: 8 additions & 8 deletions apps/api/src/withdrawal/withdrawal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ export class WithdrawalService {
* Creates a withdrawal, while blocking the corresponding amount in the source vault.
*/
async create(createWithdrawalDto: CreateWithdrawalDto): Promise<Withdrawal> {
const vault = await this.prisma.vault.findFirst({
const vault = await this.prisma.vault.findFirstOrThrow({
where: {
id: createWithdrawalDto.sourceVaultId,
},
rejectOnNotFound: true,
})
if (vault.amount - vault.blockedAmount - createWithdrawalDto.amount < 0) {
throw new BadRequestException('Insufficient amount in vault.')
Expand Down Expand Up @@ -59,14 +58,16 @@ export class WithdrawalService {
* Updates a withdrawal, where status changes to completed/declined state will finilize the withdrawal and perform vault transaction.
*/
async update(id: string, updateWithdrawalDto: UpdateWithdrawalDto): Promise<Withdrawal | null> {
const withdrawal = await this.prisma.withdrawal.findFirst({
const withdrawal = await this.prisma.withdrawal.findFirstOrThrow({
where: { id: id },
rejectOnNotFound: true,
})

if (
[ WithdrawStatus.succeeded.valueOf(), WithdrawStatus.cancelled.valueOf(), WithdrawStatus.declined.valueOf()]
.includes(withdrawal.status.valueOf())
[
WithdrawStatus.succeeded.valueOf(),
WithdrawStatus.cancelled.valueOf(),
WithdrawStatus.declined.valueOf(),
].includes(withdrawal.status.valueOf())
) {
throw new BadRequestException('Withdrawal has already been finilized and cannot be updated.')
}
Expand All @@ -79,11 +80,10 @@ export class WithdrawalService {
)
}

const vault = await this.prisma.vault.findFirst({
const vault = await this.prisma.vault.findFirstOrThrow({
where: {
id: withdrawal.sourceVaultId,
},
rejectOnNotFound: true,
})

// TODO: figure out how to initialize empty vault promise
Expand Down
Loading
Loading