fix(timesheet): minor fix to apply preset logic, less iteration

This commit is contained in:
Nicolas Drolet 2025-12-17 10:50:49 -05:00
parent 08679b7db3
commit 21dfe60548
4 changed files with 31 additions and 34 deletions

View File

@ -290,16 +290,6 @@
"post": {
"operationId": "SchedulePresetsController_applyPresetToTimesheet",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "number"
}
}
}
},
"responses": {
"201": {
"description": ""

View File

@ -50,7 +50,7 @@ export class SchedulePresetsController {
@Post('apply-preset')
@ModuleAccessAllowed(ModulesEnum.timesheets)
async applyPresetToTimesheet(
@Access('email') email: string, @Body() timesheet_id: number,
@Access('email') email: string, @Body('timesheet_id') timesheet_id: number,
) {
return await this.applyService.ApplyPresetToTimesheet(email, timesheet_id);
}

View File

@ -1,7 +1,7 @@
import { Injectable } from "@nestjs/common";
import { PrismaService } from "src/prisma/prisma.service";
import { is_same_week_day, sevenDaysFrom, toStringFromDate } from "src/common/utils/date-utils";
import { is_same_week_day, sevenDaysFrom, toStringFromDate, toStringFromHHmm } from "src/common/utils/date-utils";
import { BankCodesResolver } from "src/common/mappers/bank-type-id.mapper";
import { EmailToIdResolver } from "src/common/mappers/email-id.mapper";
import { Result } from "src/common/errors/result-error.factory";
@ -9,6 +9,7 @@ import { Result } from "src/common/errors/result-error.factory";
import { ShiftsCreateService } from "src/time-and-attendance/shifts/services/shifts-create.service";
import { timesheet_select } from "src/time-and-attendance/utils/selects.utils";
import { ShiftDto } from "src/time-and-attendance/shifts/shift.dto";
import { WEEKDAY_MAP } from "src/time-and-attendance/schedule-presets/schedule-presets.dto";
@Injectable()
@ -61,27 +62,33 @@ export class SchedulePresetsApplyService {
const dated_map = await sevenDaysFrom(timesheet.start_date);
for (const date of dated_map) {
for (const preset_shift of default_preset_shifts) {
if (!is_same_week_day(date, preset_shift.week_day)) continue;
let created_shifts: ShiftDto[] = [];
const type = await this.typeResolver.findTypeByBankCodeId(preset_shift.bank_code_id);
if (!type.success) return { success: false, error: 'INVALID_PRESET_SHIFT' };
for (const preset_shift of default_preset_shifts) {
const type = await this.typeResolver.findTypeByBankCodeId(preset_shift.bank_code_id);
const date = dated_map.find(date => date.getUTCDay() === WEEKDAY_MAP[preset_shift.week_day])
if (!date) return {success: false, error: 'INVALIDE_PRESET_DATE'};
if (!type.success) return { success: false, error: 'INVALID_PRESET_SHIFT' };
const shift: ShiftDto = {
timesheet_id: timesheet.id,
type: type.data,
date: toStringFromDate(date),
start_time: toStringFromDate(preset_shift.start_time),
end_time: toStringFromDate(preset_shift.end_time),
is_approved: false,
is_remote: preset_shift.is_remote,
};
await this.shiftService.createShift(employee_id.data, shift);
}
const shift: ShiftDto = {
timesheet_id: timesheet.id,
type: type.data,
date: toStringFromDate(date),
start_time: toStringFromHHmm(preset_shift.start_time),
end_time: toStringFromHHmm(preset_shift.end_time),
is_approved: false,
is_remote: preset_shift.is_remote,
};
created_shifts.push(shift);
// await this.shiftService.createShift(employee_id.data, shift);
}
return { success: true, data: true };
const response = await this.shiftService.createOneOrManyShifts(email, created_shifts);
if (response.success)
return { success: true, data: true };
else
return { success: false, error: 'There was an error applying presets for this week'};
}
}

View File

@ -99,8 +99,8 @@ export class GetTimesheetsOverviewService {
where: { employee_id, start_date: { gte: period_start, lte: period_end } },
include: {
employee: { include: { user: true } },
shift: { include: { bank_code: true }, orderBy: { start_time: 'asc'} },
expense: { include: { bank_code: true, attachment_record: true } },
shift: { include: { bank_code: true }, orderBy: { start_time: 'asc' } },
expense: { include: { bank_code: true, attachment_record: true }, orderBy: [{ date: 'asc' }, {bank_code_id: 'desc'}] },
},
orderBy: { start_date: 'asc' },
});
@ -109,7 +109,7 @@ export class GetTimesheetsOverviewService {
private async mapOneTimesheet(timesheet: Prisma.TimesheetsGetPayload<{
include: {
employee: { include: { user } },
shift: { include: { bank_code }, orderBy: { start_time: 'asc'} },
shift: { include: { bank_code }, orderBy: { start_time: 'asc' } },
expense: { include: { bank_code, attachment_record } },
}
}>): Promise<Timesheet> {
@ -118,7 +118,7 @@ export class GetTimesheetsOverviewService {
const day_dates = sevenDaysFrom(start);
//map of shifts by days
const shifts_by_date = new Map<string, Prisma.ShiftsGetPayload<{ include: { bank_code }, orderBy: { start_time: 'asc'} }>[]>();
const shifts_by_date = new Map<string, Prisma.ShiftsGetPayload<{ include: { bank_code }, orderBy: { start_time: 'asc' } }>[]>();
for (const shift of timesheet.shift) {
const date_string = toStringFromDate(shift.date);
const arr = shifts_by_date.get(date_string) ?? [];