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 GetShiftPatternByCodeInput { code: string; } /** * Function: getShiftPatternByCode * Resolves a ShiftPattern by its stable `code` rather than its id, with * segments included from the embedded `segments` array (ADR-022). Lookups * match only against `code`, never against the display `name`. */ export async function run(db: ReadonlyDB, input: GetShiftPatternByCodeInput) { const shiftPattern = await db .selectFrom("ShiftPattern") .selectAll() .where("code", "=", input.code) .executeTakeFirst(); if (!shiftPattern) { return err(new ShiftPatternNotFoundError(input.code)); } const segments = [...(shiftPattern.segments ?? [])].sort((a, b) => a.sequence - b.sequence); return ok({ shiftPattern, segments }); }