targo-backend/src/time-and-attendance/modules/leave-requests/utils/leave-request.util.ts

108 lines
4.0 KiB
TypeScript

import { BadRequestException, Injectable } from "@nestjs/common";
import { PrismaService } from "src/prisma/prisma.service";
import { LeaveTypes } from "@prisma/client";
// import { toDateOnly, toStringFromDate } from "src/time-and-attendance/modules/shared/helpers/date-time.helpers";
import { UpsertAction } from "src/time-and-attendance/modules/shared/types/upsert-actions.types";
import { toDateFromString, toStringFromDate } from "src/time-and-attendance/utils/date-time.utils";
// import { ShiftsUpsertService } from "src/time-and-attendance/modules/time-tracker/shifts/services/shifts-upsert.service";
@Injectable()
export class LeaveRequestsUtils {
constructor(
private readonly prisma: PrismaService,
// private readonly shiftsService: ShiftsUpsertService,
){}
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 = toDateFromString(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 },
});
const action: UpsertAction = existing ? 'update' : 'create';
// await this.shiftsService.upsertShifts(email, action, {
// 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 = toDateFromString(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.shiftsService.upsertShifts(email, 'delete', {
// 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,
// },
// });
}
}