import { err, ok, type ReadonlyDB } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; import { ShiftPatternNotFoundError } from "../lib/errors.generated"; export interface GetShiftPatternInput { shiftPatternId: string; } /** * Function: getShiftPattern * Retrieves a single ShiftPattern by id, with its segments included from the * embedded `segments` array (ADR-022), ordered by sequence. */ export async function run(db: ReadonlyDB, input: GetShiftPatternInput) { const shiftPattern = await db .selectFrom("ShiftPattern") .selectAll() .where("id", "=", input.shiftPatternId) .executeTakeFirst(); if (!shiftPattern) { return err(new ShiftPatternNotFoundError(input.shiftPatternId)); } const segments = [...(shiftPattern.segments ?? [])].sort((a, b) => a.sequence - b.sequence); return ok({ shiftPattern, segments }); }