From 73a2a755e4e803547a1dc6a77eeb5503d2070f63 Mon Sep 17 00:00:00 2001 From: Matthieu Haineault Date: Wed, 12 Nov 2025 10:31:25 -0500 Subject: [PATCH] fix(timesheets): fix id.data --- .../controllers/timesheet.controller.ts | 2 +- .../timesheet-get-overview.service.ts | 31 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/time-and-attendance/time-tracker/timesheets/controllers/timesheet.controller.ts b/src/time-and-attendance/time-tracker/timesheets/controllers/timesheet.controller.ts index 25dc752..886bb4a 100644 --- a/src/time-and-attendance/time-tracker/timesheets/controllers/timesheet.controller.ts +++ b/src/time-and-attendance/time-tracker/timesheets/controllers/timesheet.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, ParseBoolPipe, ParseIntPipe, Patch, Query, Req, UnauthorizedException } from "@nestjs/common"; +import { Body, Controller, Get, ParseBoolPipe, ParseIntPipe, Patch, Req, UnauthorizedException } from "@nestjs/common"; import { RolesAllowed } from "src/common/decorators/roles.decorators"; import { GetTimesheetsOverviewService } from "src/time-and-attendance/time-tracker/timesheets/services/timesheet-get-overview.service"; import { TimesheetApprovalService } from "src/time-and-attendance/time-tracker/timesheets/services/timesheet-approval.service"; diff --git a/src/time-and-attendance/time-tracker/timesheets/services/timesheet-get-overview.service.ts b/src/time-and-attendance/time-tracker/timesheets/services/timesheet-get-overview.service.ts index b54ea1e..abe99c5 100644 --- a/src/time-and-attendance/time-tracker/timesheets/services/timesheet-get-overview.service.ts +++ b/src/time-and-attendance/time-tracker/timesheets/services/timesheet-get-overview.service.ts @@ -34,19 +34,19 @@ export class GetTimesheetsOverviewService { // GET TIMESHEETS FOR A SELECTED EMPLOYEE //----------------------------------------------------------------------------------- async getTimesheetsForEmployeeByPeriod(email: string, pay_year: number, pay_period_no: number): Promise> { - try { //find period using year and period_no + try { + //find period using year and period_no const period = await this.prisma.payPeriods.findFirst({ where: { pay_year, pay_period_no } }); - if (!period) return { success: false, error: `Pay period ${pay_year}-${pay_period_no} not found`}; + if (!period) return { success: false, error: `Pay period ${pay_year}-${pay_period_no} not found` }; //fetch the employee_id using the email const employee_id = await this.emailResolver.findIdByEmail(email); - if (!employee_id.success) return { success: false, error: employee_id.error } + if (!employee_id.success) return { success: false, error: `employee with email: ${email} not found` + employee_id.error } //loads the timesheets related to the fetched pay-period - const timesheet_range = { employee_id, start_date: { gte: period.period_start, lte: period.period_end } }; - let rows = await this.loadTimesheets(timesheet_range); + let rows = await this.loadTimesheets(employee_id.data, period.period_start, period.period_end); - //Normalized dates from pay-period + //Normalized dates from pay-period strings const normalized_start = toDateFromString(period.period_start); const normalized_end = toDateFromString(period.period_end); @@ -57,20 +57,19 @@ export class GetTimesheetsOverviewService { if (week_start.getTime() > normalized_end.getTime()) break; - const exists = rows.some( + const has_existing_timesheets = rows.some( (row) => toDateFromString(row.start_date).getTime() === week_start.getTime() ); - if (!exists) await this.ensureTimesheet(employee_id.data, week_start); + if (!has_existing_timesheets) this.ensureTimesheet(employee_id.data, week_start); } - rows = await this.loadTimesheets(timesheet_range); - + rows = await this.loadTimesheets(employee_id.data, period.period_start, period.period_end); //find user infos using the employee_id const employee = await this.prisma.employees.findUnique({ where: { id: employee_id.data }, include: { user: true }, }); - if (!employee) return { success: false, error:`Employee #${employee_id} not found`} + if (!employee) return { success: false, error: `Employee #${employee_id} not found` } //builds employee full name const user = employee.user; @@ -79,20 +78,20 @@ export class GetTimesheetsOverviewService { //maps all timesheet's infos const timesheets = await Promise.all(rows.map((timesheet) => this.mapOneTimesheet(timesheet))); - return { success: true, data:{ employee_fullname, timesheets} }; + return { success: true, data: { employee_fullname, timesheets } }; } catch (error) { - return { success: false, error} + return { success: false, error: 'timesheet failed to load: ' + pay_year + pay_period_no } } } //----------------------------------------------------------------------------------- // MAPPERS & HELPERS //----------------------------------------------------------------------------------- - +// const timesheet_range = { employee_id: employee_id.data, start_date: { gte: period.period_start, lte: period.period_end } }; //fetch timesheet's infos - private async loadTimesheets(where: any) { + private async loadTimesheets(employee_id: number, period_start: Date, period_end: Date) { return this.prisma.timesheets.findMany({ - where, + where: { employee_id , start_date: { gte: period_start, lte: period_end } }, include: { employee: { include: { user: true } }, shift: { include: { bank_code: true } },