fix(shifts): fixed findTimesheet to findUnique instead of upsert

This commit is contained in:
Matthieu Haineault 2025-10-14 14:29:37 -04:00
parent 9ad4e63485
commit cefba7a2dd
2 changed files with 14 additions and 23 deletions

View File

@ -18,16 +18,6 @@ export class ShiftsHelpersService {
private readonly overtimeService: OvertimeService, 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) { async ensureTimesheet(tx: Tx, employee_id: number, date_only: Date) {
const start_of_week = weekStartSunday(date_only); const start_of_week = weekStartSunday(date_only);
return tx.timesheets.findUnique({ return tx.timesheets.findUnique({

View File

@ -84,18 +84,18 @@ export class ShiftsCommandService extends BaseApprovalService<Shifts> {
): Promise<{action: UpsertAction; day: DayShiftResponse[]}> { ): Promise<{action: UpsertAction; day: DayShiftResponse[]}> {
return this.prisma.$transaction(async (tx) => { return this.prisma.$transaction(async (tx) => {
const date_only = toDateOnly(date_iso); 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_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 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 this.helpersService.assertNoOverlap(day_shifts, new_norm_shift);
await tx.shifts.create({ await tx.shifts.create({
data: { data: {
timesheet_id, timesheet_id: timesheet.id,
date: date_only, date: date_only,
start_time: new_norm_shift.start_time, start_time: new_norm_shift.start_time,
end_time: new_norm_shift.end_time, end_time: new_norm_shift.end_time,
@ -106,7 +106,7 @@ export class ShiftsCommandService extends BaseApprovalService<Shifts> {
}, },
}); });
await this.helpersService.afterWriteOvertimeAndLog(tx, employee_id, date_only); 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)}; return { action: 'create', day: await this.helpersService.mapDay(fresh_shift)};
}); });
} }
@ -121,20 +121,21 @@ export class ShiftsCommandService extends BaseApprovalService<Shifts> {
): Promise<{ action: UpsertAction; day: DayShiftResponse[];}>{ ): Promise<{ action: UpsertAction; day: DayShiftResponse[];}>{
return this.prisma.$transaction(async (tx) => { return this.prisma.$transaction(async (tx) => {
const date_only = toDateOnly(date_iso); 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 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 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 old_bank_code = await this.typeResolver.findByType(old_norm_shift.type);
const new_bank_code_id = await this.helpersService.resolveBankIdRequired(tx, new_norm_shift.type, 'new_shift'); 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, { const existing = await this.helpersService.findExactOldShift(tx, {
timesheet_id, timesheet_id: timesheet.id,
date_only, date_only,
norm: old_norm_shift, 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'); 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<Shifts> {
end_time: new_norm_shift.end_time, end_time: new_norm_shift.end_time,
is_remote: new_norm_shift.is_remote, is_remote: new_norm_shift.is_remote,
comment: new_norm_shift.comment ?? null, 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); 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)}; return { action: 'update', day: await this.helpersService.mapDay(fresh_shift)};
}); });