fix(timesheets): fix id.data

This commit is contained in:
Matthieu Haineault 2025-11-12 10:31:25 -05:00
parent 40072af4a6
commit 73a2a755e4
2 changed files with 16 additions and 17 deletions

View File

@ -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";

View File

@ -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<Result<Timesheets, string>> {
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 } },