fix(timesheet): minor fix to apply preset logic, less iteration
This commit is contained in:
parent
08679b7db3
commit
21dfe60548
|
|
@ -290,16 +290,6 @@
|
|||
"post": {
|
||||
"operationId": "SchedulePresetsController_applyPresetToTimesheet",
|
||||
"parameters": [],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"201": {
|
||||
"description": ""
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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[] = [];
|
||||
|
||||
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),
|
||||
start_time: toStringFromHHmm(preset_shift.start_time),
|
||||
end_time: toStringFromHHmm(preset_shift.end_time),
|
||||
is_approved: false,
|
||||
is_remote: preset_shift.is_remote,
|
||||
};
|
||||
await this.shiftService.createShift(employee_id.data, shift);
|
||||
}
|
||||
|
||||
created_shifts.push(shift);
|
||||
// await this.shiftService.createShift(employee_id.data, shift);
|
||||
}
|
||||
|
||||
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'};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -100,7 +100,7 @@ export class GetTimesheetsOverviewService {
|
|||
include: {
|
||||
employee: { include: { user: true } },
|
||||
shift: { include: { bank_code: true }, orderBy: { start_time: 'asc' } },
|
||||
expense: { include: { bank_code: true, attachment_record: true } },
|
||||
expense: { include: { bank_code: true, attachment_record: true }, orderBy: [{ date: 'asc' }, {bank_code_id: 'desc'}] },
|
||||
},
|
||||
orderBy: { start_date: 'asc' },
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user