import { err, ok, type ReadonlyDB } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; import { AccrualPlanNotFoundError } from "../lib/errors.generated"; export interface GetAccrualPlanInput { id: string; } /** * Function: getAccrualPlan * Description: Resolves a single AccrualPlan generation by its surrogate id, * returning the generation regardless of whether it is the current * (effectiveEnd IS NULL) generation or a closed historical one. */ export async function run(db: ReadonlyDB, input: GetAccrualPlanInput) { const accrualPlan = await db .selectFrom("AccrualPlan") .selectAll() .where("id", "=", input.id) .executeTakeFirst(); if (!accrualPlan) { return err(new AccrualPlanNotFoundError(input.id)); } return ok({ accrualPlan }); }