targo-backend/src/time-and-attendance/modules/time-tracker/schedule-presets/services/schedule-presets-query.service.ts

51 lines
2.1 KiB
TypeScript

import { Injectable, NotFoundException } from "@nestjs/common";
import { EmailToIdResolver } from "src/time-and-attendance/modules/shared/utils/resolve-email-id.utils";
import { PrismaService } from "src/prisma/prisma.service";
import { Prisma } from "@prisma/client";
import { PresetResponse, ShiftResponse } from "src/time-and-attendance/utils/type.utils";
@Injectable()
export class SchedulePresetsQueryService {
constructor(
private readonly prisma: PrismaService,
private readonly emailResolver: EmailToIdResolver,
){}
async findSchedulePresetsByEmail(email:string): Promise<PresetResponse[]> {
const employee_id = await this.emailResolver.findIdByEmail(email);
if(!employee_id) throw new NotFoundException(`Employee with email: ${email} not found`);
try {
const presets = await this.prisma.schedulePresets.findMany({
where: { employee_id },
orderBy: [{is_default: 'desc' }, { name: 'asc' }],
include: {
shifts: {
orderBy: [{week_day:'asc'}, { sort_order: 'asc'}],
include: { bank_code: { select: { type: true } } },
},
},
});
const hhmm = (date: Date) => date.toISOString().slice(11,16);
const response: PresetResponse[] = presets.map((preset) => ({
id: preset.id,
name: preset.name,
is_default: preset.is_default,
shifts: preset.shifts.map<ShiftResponse>((shift)=> ({
week_day: shift.week_day,
sort_order: shift.sort_order,
start_time: hhmm(shift.start_time),
end_time: hhmm(shift.end_time),
is_remote: shift.is_remote,
type: shift.bank_code?.type,
})),
}));
return response;
} catch ( error: unknown) {
if(error instanceof Prisma.PrismaClientKnownRequestError) {}
throw error;
}
}
}