From a23a6299ddc36a7468b15347a26d9e62b57d5c96 Mon Sep 17 00:00:00 2001 From: Matthieu Haineault Date: Tue, 19 Aug 2025 08:44:36 -0400 Subject: [PATCH] refactor(module): refactored expenses and shifts approval to be made in batch iva timesheets --- .../services/expenses-command.service.ts | 27 ++++++++++++++++++- .../shifts/services/shifts-command.service.ts | 25 ++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/modules/expenses/services/expenses-command.service.ts b/src/modules/expenses/services/expenses-command.service.ts index f5b8283..5c254ed 100644 --- a/src/modules/expenses/services/expenses-command.service.ts +++ b/src/modules/expenses/services/expenses-command.service.ts @@ -1,5 +1,7 @@ import { Injectable } from "@nestjs/common"; -import { Expenses } from "@prisma/client"; +import { Expenses, Prisma } from "@prisma/client"; +import { Decimal } from "@prisma/client/runtime/library"; +import { transcode } from "buffer"; import { BaseApprovalService } from "src/common/shared/base-approval.service"; import { PrismaService } from "src/prisma/prisma.service"; @@ -10,4 +12,27 @@ export class ExpensesCommandService extends BaseApprovalService { protected get delegate() { return this.prisma.expenses; } + + protected delegateFor(transaction: Prisma.TransactionClient){ + return transaction.expenses; + } + + async updateApproval(id: number, isApproved: boolean): Promise { + return this.prisma.$transaction((transaction) => + this.updateApprovalWithTx(transaction, id, isApproved), + ); + } + + // deprecated since batch transaction are made with timesheets + // async updateManyWithTx( + // tx: Prisma.TransactionClient, + // ids: number[], + // isApproved: boolean, + // ): Promise { + // const { count } = await tx.expenses.updateMany({ + // where: { id: { in: ids } }, + // data: { is_approved: isApproved }, + // }); + // return count; + // } } \ No newline at end of file diff --git a/src/modules/shifts/services/shifts-command.service.ts b/src/modules/shifts/services/shifts-command.service.ts index e222490..8d75e0e 100644 --- a/src/modules/shifts/services/shifts-command.service.ts +++ b/src/modules/shifts/services/shifts-command.service.ts @@ -1,5 +1,5 @@ import { Injectable } from "@nestjs/common"; -import { Shifts } from "@prisma/client"; +import { Prisma, Shifts } from "@prisma/client"; import { BaseApprovalService } from "src/common/shared/base-approval.service"; import { PrismaService } from "src/prisma/prisma.service"; @@ -10,4 +10,27 @@ export class ShiftsCommandService extends BaseApprovalService { protected get delegate() { return this.prisma.shifts; } + + protected delegateFor(transaction: Prisma.TransactionClient) { + return transaction.shifts; + } + + async updateApproval(id: number, isApproved: boolean): Promise { + return this.prisma.$transaction((transaction) => + this.updateApprovalWithTx(transaction, id, isApproved), + ); + } + + // deprecated since batch transaction are made with timesheets + // async updateManyWithTx( + // tx: Prisma.TransactionClient, + // ids: number[], + // isApproved: boolean, + // ): Promise { + // const { count } = await tx.shifts.updateMany({ + // where: { id: { in: ids } }, + // data: { is_approved: isApproved }, + // }); + // return count; + // } } \ No newline at end of file