import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core"; import type { Transaction } from "../generated/kysely-tailordb"; import { DuplicateKeyError } from "../lib/errors.generated"; export interface CreateTimeEntryCodeInput { key: string; /** Normalized category key defined by the calculation strategy (not a fixed enum). */ category: string; displayName: string; payMapKey: string; } /** * Function: createTimeEntryCode * Description: Registers a new entry in the semantic time-type catalog: a * stable key, a normalized category, a UI-only displayName, and a payMapKey * that seams the code to a future payroll earnings code. */ export async function run(db: Transaction, input: CreateTimeEntryCodeInput, _ctx: CommandContext) { const existing = await db .selectFrom("TimeEntryCode") .selectAll() .where("key", "=", input.key) .forUpdate() .executeTakeFirst(); if (existing) { return err(new DuplicateKeyError(input.key)); } const timeEntryCode = await db .insertInto("TimeEntryCode") .values({ key: input.key, category: input.category, displayName: input.displayName, payMapKey: input.payMapKey, }) .returningAll() .executeTakeFirstOrThrow(); return ok({ timeEntryCode }); }