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