diff --git a/src/modules/shifts/helpers/shifts.helpers.ts b/src/modules/shifts/helpers/shifts.helpers.ts index 62da22d..2770457 100644 --- a/src/modules/shifts/helpers/shifts.helpers.ts +++ b/src/modules/shifts/helpers/shifts.helpers.ts @@ -49,9 +49,9 @@ export class ShiftsHelpersService { return id; } - async getDayShifts(tx: Tx, timesheet_id: number, dateIso: string) { + async getDayShifts(tx: Tx, timesheet_id: number, date_only: Date) { return tx.shifts.findMany({ - where: { timesheet_id, date: dateIso }, + where: { timesheet_id, date: date_only }, include: { bank_code: true }, orderBy: { start_time: 'asc' }, }); diff --git a/src/modules/shifts/services/shifts-command.service.ts b/src/modules/shifts/services/shifts-command.service.ts index 1592233..dc098cd 100644 --- a/src/modules/shifts/services/shifts-command.service.ts +++ b/src/modules/shifts/services/shifts-command.service.ts @@ -89,7 +89,7 @@ export class ShiftsCommandService extends BaseApprovalService { const new_norm_shift = await this.helpersService.normalizeRequired(dto.new_shift); const new_bank_code_id = await this.helpersService.resolveBankIdRequired(tx, new_norm_shift.type, 'new_shift'); - const day_shifts = await this.helpersService.getDayShifts(tx, timesheet_id, date_iso); + const day_shifts = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); await this.helpersService.assertNoOverlap(day_shifts, new_norm_shift); @@ -106,7 +106,7 @@ export class ShiftsCommandService extends BaseApprovalService { }, }); await this.helpersService.afterWriteOvertimeAndLog(tx, employee_id, date_only,'create'); - const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_iso); + const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); return { action: 'create', day: await this.helpersService.mapDay(fresh_shift)}; }); } @@ -129,7 +129,7 @@ export class ShiftsCommandService extends BaseApprovalService { const old_bank_code_id = await this.helpersService.resolveBankIdRequired(tx, old_norm_shift.type, 'old_shift'); const new_bank_code_id = await this.helpersService.resolveBankIdRequired(tx, new_norm_shift.type, 'new_shift'); - const day_shifts = await this.helpersService.getDayShifts(tx, timesheet_id, date_iso); + const day_shifts = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); const existing = await this.helpersService.findExactOldShift(tx, { timesheet_id, date_only, @@ -151,7 +151,7 @@ export class ShiftsCommandService extends BaseApprovalService { }, }); await this.helpersService.afterWriteOvertimeAndLog(tx, employee_id, date_only, 'update'); - const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_iso); + const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); return { action: 'update', day: await this.helpersService.mapDay(fresh_shift)}; }); @@ -168,23 +168,23 @@ export class ShiftsCommandService extends BaseApprovalService { return this.prisma.$transaction(async (tx) => { const date_only = toDateOnly(date_iso); const employee_id = await this.emailResolver.findIdByEmail(email); + const { id: timesheet_id } = await this.helpersService.ensureTimesheet(tx, employee_id, date_only); - - const old_norm_shift = await this.helpersService.normalizeRequired(dto.old_shift, 'old_shift'); - const old_bank_code_id = await this.typeResolver.findByType(old_norm_shift.type); + const norm_shift = await this.helpersService.normalizeRequired(dto.old_shift, 'old_shift'); + const bank_code_id = await this.typeResolver.findByType(norm_shift.type); const existing = await this.helpersService.findExactOldShift(tx, { timesheet_id, date_only, - norm: old_norm_shift, - bank_code_id: old_bank_code_id.id, + norm: norm_shift, + bank_code_id: bank_code_id.id, }); if(!existing) throw new NotFoundException('[SHIFT_STALE]- The shift was modified or deleted by someone else'); await tx.shifts.delete({ where: { id: existing.id } }); await this.helpersService.afterWriteOvertimeAndLog(tx, employee_id, date_only, 'delete'); - const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_iso); + const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); return { day: await this.helpersService.mapDay(fresh_shift)}; }); } diff --git a/src/modules/shifts/utils/shifts.utils.ts b/src/modules/shifts/utils/shifts.utils.ts index b2bd6e8..60bc11f 100644 --- a/src/modules/shifts/utils/shifts.utils.ts +++ b/src/modules/shifts/utils/shifts.utils.ts @@ -49,7 +49,7 @@ export function normalizeShiftPayload(payload: { const type = (payload.type || '').trim().toUpperCase(); const is_remote = payload.is_remote === true; - const is_approved = payload.is_approved === false; + const is_approved = payload.is_approved; //normalize comment const trimmed = typeof payload.comment === 'string' ? payload.comment.trim() : null; const comment = trimmed && trimmed.length > 0 ? trimmed : null;