import { Injectable, NotFoundException } from "@nestjs/common"; import { PrismaService } from "src/prisma/prisma.service"; import { GetShiftDto } from "../dtos/get-shift.dto"; import { toStringFromDate, toStringFromHHmm } from "../helpers/shifts-date-time-helpers"; @Injectable() export class ShiftsGetService { constructor( private readonly prisma: PrismaService, ){} //fetch a shift using shift_id and return all that shift's info async getShiftByShiftId(shift_ids: number[]): Promise { if(!Array.isArray(shift_ids) || shift_ids.length === 0) return []; const rows = await this.prisma.shifts.findMany({ where: { id: { in: shift_ids } }, select: { id: true, timesheet_id: true, bank_code_id: true, date: true, start_time: true, end_time: true, is_remote: true, is_approved: true, comment: true, } }); if(rows.length !== shift_ids.length) { const found_ids = new Set(rows.map(row => row.id)); const missing_ids = shift_ids.filter(id => !found_ids.has(id)); throw new NotFoundException(`Shift(s) not found: ${ missing_ids.join(", ")}`); } const row_by_id = new Map(rows.map(row => [row.id, row])); return shift_ids.map((id) => { const shift = row_by_id.get(id)!; return { timesheet_id: shift.timesheet_id, bank_code_id: shift.bank_code_id, date: toStringFromDate(shift.date), start_time: toStringFromHHmm(shift.start_time), end_time: toStringFromHHmm(shift.end_time), is_remote: shift.is_remote, is_approved: shift.is_approved, comment: shift.comment ?? undefined, } satisfies GetShiftDto; }); } }