103 lines
3.6 KiB
TypeScript
103 lines
3.6 KiB
TypeScript
import { hhmmFromLocal, toDateOnly, toStringFromDate } from "src/modules/shared/helpers/date-time.helpers";
|
|
import { BadRequestException, Injectable } from "@nestjs/common";
|
|
import { ShiftsCommandService } from "src/modules/shifts/services/shifts-command.service";
|
|
import { PrismaService } from "src/prisma/prisma.service";
|
|
import { LeaveTypes } from "@prisma/client";
|
|
|
|
@Injectable()
|
|
export class LeaveRequestsUtils {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly shiftsCommand: ShiftsCommandService,
|
|
){}
|
|
|
|
async syncShift(
|
|
email: string,
|
|
employee_id: number,
|
|
date: string,
|
|
hours: number,
|
|
type: LeaveTypes,
|
|
comment?: string,
|
|
) {
|
|
if (hours <= 0) return;
|
|
|
|
const duration_minutes = Math.round(hours * 60);
|
|
if (duration_minutes > 8 * 60) {
|
|
throw new BadRequestException("Amount of hours cannot exceed 8 hours per day.");
|
|
}
|
|
const date_only = toDateOnly(date);
|
|
const yyyy_mm_dd = toStringFromDate(date_only);
|
|
|
|
|
|
|
|
const start_minutes = 8 * 60;
|
|
const end_minutes = start_minutes + duration_minutes;
|
|
const toHHmm = (total: number) =>
|
|
`${String(Math.floor(total / 60)).padStart(2, "0")}:${String(total % 60).padStart(2, "0")}`;
|
|
|
|
const existing = await this.prisma.shifts.findFirst({
|
|
where: {
|
|
date: date_only,
|
|
bank_code: { type },
|
|
timesheet: { employee_id: employee_id },
|
|
},
|
|
include: { bank_code: true },
|
|
});
|
|
|
|
await this.shiftsCommand.upsertShiftsByDate(email, {
|
|
old_shift: existing
|
|
? {
|
|
date: yyyy_mm_dd,
|
|
start_time: existing.start_time.toISOString().slice(11, 16),
|
|
end_time: existing.end_time.toISOString().slice(11, 16),
|
|
type: existing.bank_code?.type ?? type,
|
|
is_remote: existing.is_remote,
|
|
is_approved:existing.is_approved,
|
|
comment: existing.comment ?? undefined,
|
|
}
|
|
: undefined,
|
|
new_shift: {
|
|
date: yyyy_mm_dd,
|
|
start_time: toHHmm(start_minutes),
|
|
end_time: toHHmm(end_minutes),
|
|
is_remote: existing?.is_remote ?? false,
|
|
is_approved:existing?.is_approved ?? false,
|
|
comment: comment ?? existing?.comment ?? "",
|
|
type: type,
|
|
},
|
|
});
|
|
}
|
|
|
|
async removeShift(
|
|
email: string,
|
|
employee_id: number,
|
|
iso_date: string,
|
|
type: LeaveTypes,
|
|
) {
|
|
const date_only = toDateOnly(iso_date);
|
|
const yyyy_mm_dd = toStringFromDate(date_only);
|
|
const existing = await this.prisma.shifts.findFirst({
|
|
where: {
|
|
date: date_only,
|
|
bank_code: { type },
|
|
timesheet: { employee_id: employee_id },
|
|
},
|
|
include: { bank_code: true },
|
|
});
|
|
if (!existing) return;
|
|
|
|
await this.shiftsCommand.upsertShiftsByDate(email, {
|
|
old_shift: {
|
|
date: yyyy_mm_dd,
|
|
start_time: hhmmFromLocal(existing.start_time),
|
|
end_time: hhmmFromLocal(existing.end_time),
|
|
type: existing.bank_code?.type ?? type,
|
|
is_remote: existing.is_remote,
|
|
is_approved:existing.is_approved,
|
|
comment: existing.comment ?? undefined,
|
|
},
|
|
});
|
|
}
|
|
|
|
}
|