diff --git a/src/modules/shifts/helpers/shifts.helpers.ts b/src/modules/shifts/helpers/shifts.helpers.ts index c5da538..6038db7 100644 --- a/src/modules/shifts/helpers/shifts.helpers.ts +++ b/src/modules/shifts/helpers/shifts.helpers.ts @@ -18,16 +18,6 @@ export class ShiftsHelpersService { private readonly overtimeService: OvertimeService, ) { } - async findOrUpsertTimesheet(tx: Tx, employee_id: number, date_only: Date) { - const start_of_week = weekStartSunday(date_only); - return tx.timesheets.upsert({ - where: { employee_id_start_date: { employee_id, start_date: start_of_week } }, - update: {}, - create: { employee_id, start_date: start_of_week }, - select: { id: true }, - }); - } - async ensureTimesheet(tx: Tx, employee_id: number, date_only: Date) { const start_of_week = weekStartSunday(date_only); return tx.timesheets.findUnique({ diff --git a/src/modules/shifts/services/shifts-command.service.ts b/src/modules/shifts/services/shifts-command.service.ts index 91fe30d..a7e265a 100644 --- a/src/modules/shifts/services/shifts-command.service.ts +++ b/src/modules/shifts/services/shifts-command.service.ts @@ -84,18 +84,18 @@ export class ShiftsCommandService extends BaseApprovalService { ): Promise<{action: UpsertAction; day: DayShiftResponse[]}> { return this.prisma.$transaction(async (tx) => { const date_only = toDateOnly(date_iso); - const { id: timesheet_id } = await this.helpersService.findOrUpsertTimesheet(tx, employee_id, date_only); - + const timesheet = await this.helpersService.ensureTimesheet(tx, employee_id, date_only); + if(!timesheet) throw new NotFoundException('Timesheet not found') 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_only); + const day_shifts = await this.helpersService.getDayShifts(tx, timesheet.id, date_only); await this.helpersService.assertNoOverlap(day_shifts, new_norm_shift); await tx.shifts.create({ data: { - timesheet_id, + timesheet_id: timesheet.id, date: date_only, start_time: new_norm_shift.start_time, end_time: new_norm_shift.end_time, @@ -106,7 +106,7 @@ export class ShiftsCommandService extends BaseApprovalService { }, }); await this.helpersService.afterWriteOvertimeAndLog(tx, employee_id, date_only); - const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); + const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet.id, date_only); return { action: 'create', day: await this.helpersService.mapDay(fresh_shift)}; }); } @@ -121,20 +121,21 @@ export class ShiftsCommandService extends BaseApprovalService { ): Promise<{ action: UpsertAction; day: DayShiftResponse[];}>{ return this.prisma.$transaction(async (tx) => { const date_only = toDateOnly(date_iso); - const { id: timesheet_id } = await this.helpersService.findOrUpsertTimesheet(tx, employee_id, date_only); + const timesheet = await this.helpersService.ensureTimesheet(tx, employee_id, date_only); + if(!timesheet) throw new NotFoundException('Timesheet not found') const old_norm_shift = await this.helpersService.normalizeRequired(dto.old_shift, 'old_shift'); const new_norm_shift = await this.helpersService.normalizeRequired(dto.new_shift, 'new_shift'); - 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 old_bank_code = await this.typeResolver.findByType(old_norm_shift.type); + const new_bank_code = await this.typeResolver.findByType(new_norm_shift.type); - const day_shifts = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); + const day_shifts = await this.helpersService.getDayShifts(tx, timesheet.id, date_only); const existing = await this.helpersService.findExactOldShift(tx, { - timesheet_id, + timesheet_id: timesheet.id, date_only, norm: old_norm_shift, - bank_code_id: old_bank_code_id, + bank_code_id: old_bank_code.id, }); if(!existing) throw new NotFoundException('[SHIFT_STALE]- The shift was modified or deleted by someone else'); @@ -147,11 +148,11 @@ export class ShiftsCommandService extends BaseApprovalService { end_time: new_norm_shift.end_time, is_remote: new_norm_shift.is_remote, comment: new_norm_shift.comment ?? null, - bank_code_id: new_bank_code_id, + bank_code_id: new_bank_code.id, }, }); await this.helpersService.afterWriteOvertimeAndLog(tx, employee_id, date_only); - const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet_id, date_only); + const fresh_shift = await this.helpersService.getDayShifts(tx, timesheet.id, date_only); return { action: 'update', day: await this.helpersService.mapDay(fresh_shift)}; });