import { err, ok, type ReadonlyDB } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; import { ValidationErrorError } from "../lib/errors.generated"; export interface GetCompanyHolidayByDateInput { calendarId: string; holidayDate: Date; } /** * Function: getCompanyHolidayByDate * Description: Resolves the holiday fact for a specific calendar date within a * given HolidayCalendar, by the stable business key (calendarId, holidayDate). * Returns null (not an error) when the date is not registered as a holiday in * that calendar — an ordinary working day. This is the lookup calculation uses * to determine holiday classification/premium, and scheduling uses to determine * scheduled working-day status. */ export async function run(db: ReadonlyDB, input: GetCompanyHolidayByDateInput) { if (!input.holidayDate || Number.isNaN(input.holidayDate.getTime())) { return err(new ValidationErrorError("holidayDate")); } const companyHoliday = await db .selectFrom("CompanyHoliday") .selectAll() .where("calendarId", "=", input.calendarId) .where("holidayDate", "=", input.holidayDate) .executeTakeFirst(); return ok({ companyHoliday: companyHoliday ?? null }); }