import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core"; import type { Transaction } from "../generated/kysely-tailordb"; import { DuplicateCalendarKeyError } from "../lib/errors.generated"; export interface CreateHolidayCalendarInput { key: string; name: string; } /** * Function: createHolidayCalendar * Description: Registers a new HolidayCalendar — a stable key and a display * name — that CompanyHoliday entries and WorkRules bind to. The key is unique * across calendars; registering a second calendar with an existing key is * rejected. */ export async function run( db: Transaction, input: CreateHolidayCalendarInput, _ctx: CommandContext, ) { const existing = await db .selectFrom("HolidayCalendar") .selectAll() .where("key", "=", input.key) .forUpdate() .executeTakeFirst(); if (existing) { return err(new DuplicateCalendarKeyError(input.key)); } const holidayCalendar = await db .insertInto("HolidayCalendar") .values({ key: input.key, name: input.name }) .returningAll() .executeTakeFirstOrThrow(); return ok({ holidayCalendar }); }