refactor(leave-requests): moved utils to shared file

This commit is contained in:
Matthieu Haineault 2025-10-09 16:48:45 -04:00
parent 71d86f7fed
commit 6a4fbeb2c4
5 changed files with 25 additions and 29 deletions

View File

@ -8,6 +8,8 @@ import { mapRowToView } from '../mappers/leave-requests.mapper';
import { leaveRequestsSelect } from '../utils/leave-requests.select'; import { leaveRequestsSelect } from '../utils/leave-requests.select';
import { LeaveRequestsUtils} from '../utils/leave-request.util'; import { LeaveRequestsUtils} from '../utils/leave-request.util';
import { normalizeDates, toDateOnly } from 'src/modules/shared/helpers/date-time.helpers'; import { normalizeDates, toDateOnly } from 'src/modules/shared/helpers/date-time.helpers';
import { BankCodesResolver } from 'src/modules/shared/utils/resolve-bank-type-id.utils';
import { EmployeeIdEmailResolver } from 'src/modules/shared/utils/resolve-email-id.utils';
@Injectable() @Injectable()
@ -16,12 +18,14 @@ export class HolidayLeaveRequestsService {
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly holidayService: HolidayService, private readonly holidayService: HolidayService,
private readonly leaveUtils: LeaveRequestsUtils, private readonly leaveUtils: LeaveRequestsUtils,
private readonly emailResolver: EmployeeIdEmailResolver,
private readonly typeResolver: BankCodesResolver,
) {} ) {}
async create(dto: UpsertLeaveRequestDto): Promise<UpsertResult> { async create(dto: UpsertLeaveRequestDto): Promise<UpsertResult> {
const email = dto.email.trim(); const email = dto.email.trim();
const employee_id = await this.leaveUtils.resolveEmployeeIdByEmail(email); const employee_id = await this.emailResolver.findIdByEmail(email);
const bank_code = await this.leaveUtils.resolveBankCodeByType(LeaveTypes.HOLIDAY); const bank_code = await this.typeResolver.findByType(LeaveTypes.HOLIDAY);
if(!bank_code) throw new NotFoundException(`bank_code not found`); if(!bank_code) throw new NotFoundException(`bank_code not found`);
const dates = normalizeDates(dto.dates); const dates = normalizeDates(dto.dates);
if (!dates.length) throw new BadRequestException('Dates array must not be empty'); if (!dates.length) throw new BadRequestException('Dates array must not be empty');

View File

@ -14,6 +14,8 @@ import { VacationService } from "src/modules/business-logics/services/vacation.s
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
import { LeaveRequestsUtils } from "../utils/leave-request.util"; import { LeaveRequestsUtils } from "../utils/leave-request.util";
import { normalizeDates, toDateOnly, toISODateKey } from "src/modules/shared/helpers/date-time.helpers"; import { normalizeDates, toDateOnly, toISODateKey } from "src/modules/shared/helpers/date-time.helpers";
import { EmployeeIdEmailResolver } from "src/modules/shared/utils/resolve-email-id.utils";
import { BankCodesResolver } from "src/modules/shared/utils/resolve-bank-type-id.utils";
@Injectable() @Injectable()
export class LeaveRequestsService { export class LeaveRequestsService {
@ -26,6 +28,8 @@ export class LeaveRequestsService {
private readonly vacationLeaveService: VacationLeaveRequestsService, private readonly vacationLeaveService: VacationLeaveRequestsService,
private readonly vacationLogic: VacationService, private readonly vacationLogic: VacationService,
private readonly leaveUtils: LeaveRequestsUtils, private readonly leaveUtils: LeaveRequestsUtils,
private readonly emailResolver: EmployeeIdEmailResolver,
private readonly typeResolver: BankCodesResolver,
) {} ) {}
//handle distribution to the right service according to the selected type and action //handle distribution to the right service according to the selected type and action
@ -63,7 +67,7 @@ export class LeaveRequestsService {
async delete(dto: UpsertLeaveRequestDto, type: LeaveTypes): Promise<UpsertResult> { async delete(dto: UpsertLeaveRequestDto, type: LeaveTypes): Promise<UpsertResult> {
const email = dto.email.trim(); const email = dto.email.trim();
const dates = normalizeDates(dto.dates); const dates = normalizeDates(dto.dates);
const employee_id = await this.leaveUtils.resolveEmployeeIdByEmail(email); const employee_id = await this.emailResolver.findIdByEmail(email);
if (!dates.length) throw new BadRequestException("Dates array must not be empty"); if (!dates.length) throw new BadRequestException("Dates array must not be empty");
const rows = await this.prisma.leaveRequests.findMany({ const rows = await this.prisma.leaveRequests.findMany({
@ -97,8 +101,8 @@ export class LeaveRequestsService {
async update(dto: UpsertLeaveRequestDto, type: LeaveTypes): Promise<UpsertResult> { async update(dto: UpsertLeaveRequestDto, type: LeaveTypes): Promise<UpsertResult> {
const email = dto.email.trim(); const email = dto.email.trim();
const employee_id = await this.leaveUtils.resolveEmployeeIdByEmail(email); const employee_id = await this.emailResolver.findIdByEmail(email);
const bank_code = await this.leaveUtils.resolveBankCodeByType(type); const bank_code = await this.typeResolver.findByType(type);
if(!bank_code) throw new NotFoundException(`bank_code not found`); if(!bank_code) throw new NotFoundException(`bank_code not found`);
const modifier = Number(bank_code.modifier ?? 1); const modifier = Number(bank_code.modifier ?? 1);
const dates = normalizeDates(dto.dates); const dates = normalizeDates(dto.dates);

View File

@ -9,6 +9,8 @@ import { SickLeaveService } from "src/modules/business-logics/services/sick-le
import { roundToQuarterHour } from "src/common/utils/date-utils"; import { roundToQuarterHour } from "src/common/utils/date-utils";
import { LeaveRequestsUtils } from "../utils/leave-request.util"; import { LeaveRequestsUtils } from "../utils/leave-request.util";
import { normalizeDates, toDateOnly } from "src/modules/shared/helpers/date-time.helpers"; import { normalizeDates, toDateOnly } from "src/modules/shared/helpers/date-time.helpers";
import { EmployeeIdEmailResolver } from "src/modules/shared/utils/resolve-email-id.utils";
import { BankCodesResolver } from "src/modules/shared/utils/resolve-bank-type-id.utils";
@Injectable() @Injectable()
export class SickLeaveRequestsService { export class SickLeaveRequestsService {
@ -16,12 +18,14 @@ export class SickLeaveRequestsService {
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly sickService: SickLeaveService, private readonly sickService: SickLeaveService,
private readonly leaveUtils: LeaveRequestsUtils, private readonly leaveUtils: LeaveRequestsUtils,
private readonly emailResolver: EmployeeIdEmailResolver,
private readonly typeResolver: BankCodesResolver,
) {} ) {}
async create(dto: UpsertLeaveRequestDto): Promise<UpsertResult> { async create(dto: UpsertLeaveRequestDto): Promise<UpsertResult> {
const email = dto.email.trim(); const email = dto.email.trim();
const employee_id = await this.leaveUtils.resolveEmployeeIdByEmail(email); const employee_id = await this.emailResolver.findIdByEmail(email);
const bank_code = await this.leaveUtils.resolveBankCodeByType(LeaveTypes.SICK); const bank_code = await this.typeResolver.findByType(LeaveTypes.SICK);
if(!bank_code) throw new NotFoundException(`bank_code not found`); if(!bank_code) throw new NotFoundException(`bank_code not found`);
const modifier = bank_code.modifier ?? 1; const modifier = bank_code.modifier ?? 1;

View File

@ -10,6 +10,8 @@ import { leaveRequestsSelect } from "../utils/leave-requests.select";
import { roundToQuarterHour } from "src/common/utils/date-utils"; import { roundToQuarterHour } from "src/common/utils/date-utils";
import { LeaveRequestsUtils } from "../utils/leave-request.util"; import { LeaveRequestsUtils } from "../utils/leave-request.util";
import { normalizeDates, toDateOnly } from "src/modules/shared/helpers/date-time.helpers"; import { normalizeDates, toDateOnly } from "src/modules/shared/helpers/date-time.helpers";
import { EmployeeIdEmailResolver } from "src/modules/shared/utils/resolve-email-id.utils";
import { BankCodesResolver } from "src/modules/shared/utils/resolve-bank-type-id.utils";
@Injectable() @Injectable()
export class VacationLeaveRequestsService { export class VacationLeaveRequestsService {
@ -17,12 +19,14 @@ export class VacationLeaveRequestsService {
private readonly prisma: PrismaService, private readonly prisma: PrismaService,
private readonly vacationService: VacationService, private readonly vacationService: VacationService,
private readonly leaveUtils: LeaveRequestsUtils, private readonly leaveUtils: LeaveRequestsUtils,
private readonly emailResolver: EmployeeIdEmailResolver,
private readonly typeResolver: BankCodesResolver,
) {} ) {}
async create(dto: UpsertLeaveRequestDto): Promise<UpsertResult> { async create(dto: UpsertLeaveRequestDto): Promise<UpsertResult> {
const email = dto.email.trim(); const email = dto.email.trim();
const employee_id = await this.leaveUtils.resolveEmployeeIdByEmail(email); const employee_id = await this.emailResolver.findIdByEmail(email);
const bank_code = await this.leaveUtils.resolveBankCodeByType(LeaveTypes.VACATION); const bank_code = await this.typeResolver.findByType(LeaveTypes.VACATION);
if(!bank_code) throw new NotFoundException(`bank_code not found`); if(!bank_code) throw new NotFoundException(`bank_code not found`);
const modifier = bank_code.modifier ?? 1; const modifier = bank_code.modifier ?? 1;

View File

@ -11,26 +11,6 @@ export class LeaveRequestsUtils {
private readonly shiftsCommand: ShiftsCommandService, private readonly shiftsCommand: ShiftsCommandService,
){} ){}
async resolveEmployeeIdByEmail(email: string): Promise<number> {
const employee = await this.prisma.employees.findFirst({
where: { user: { email } },
select: { id: true },
});
if (!employee) {
throw new NotFoundException(`Employee with email ${email} not found`);
}
return employee.id;
}
async resolveBankCodeByType(type: LeaveTypes) {
const bankCode = await this.prisma.bankCodes.findFirst({
where: { type },
select: { id: true, bank_code: true, modifier: true },
});
if (!bankCode) throw new BadRequestException(`Bank code type "${type}" not found`);
return bankCode;
}
async syncShift( async syncShift(
email: string, email: string,
employee_id: number, employee_id: number,