import { ok, err, type ReadonlyDB } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; import { TimeEntryCodeNotFoundError, ValidationErrorError } from "../lib/errors.generated"; export interface GetTimeEntryCodeByKeyInput { key: string; } export async function run(db: ReadonlyDB, input: GetTimeEntryCodeByKeyInput) { if (typeof input.key !== "string" || input.key.trim().length === 0) { return err(new ValidationErrorError(String(input.key))); } const timeEntryCode = await db .selectFrom("TimeEntryCode") .selectAll() .where("key", "=", input.key) .executeTakeFirst(); if (!timeEntryCode) { return err(new TimeEntryCodeNotFoundError(input.key)); } return ok({ timeEntryCode }); }