import { NotFoundException } from "@nestjs/common"; import { Prisma } from "@prisma/client"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"; import { PrismaService } from "src/prisma/prisma.service"; type UpdatableDelegate = { update(args: { where: { id: number }, data: { is_approved: boolean }, }): Promise; }; //abstract class for approving or rejecting a shift, expense, timesheet or pay-period export abstract class BaseApprovalService { protected constructor(protected readonly prisma: PrismaService) {} //returns the corresponding Prisma delegate protected abstract get delegate(): UpdatableDelegate; protected abstract delegateFor(transaction: Prisma.TransactionClient): UpdatableDelegate; //standard update Aproval async updateApproval(id: number, isApproved: boolean): Promise { try{ return await this.delegate.update({ where: { id }, data: { is_approved: isApproved }, }); }catch (error: any) { if (error instanceof PrismaClientKnownRequestError && error.code === "P2025") { throw new NotFoundException(`Entity #${id} not found`); } throw error; } } //approval with transaction to avoid many requests async updateApprovalWithTx(transaction: Prisma.TransactionClient, id: number, isApproved: boolean): Promise { try { return await this.delegateFor(transaction).update({ where: { id }, data: { is_approved: isApproved }, }); } catch (error: any ){ if(error instanceof PrismaClientKnownRequestError && error.code === 'P2025') { throw new NotFoundException(`Entity #${id} not found`); } throw error; } } }