import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core"; import type { Transaction } from "../generated/kysely-tailordb"; import { WorkRegimeNotFoundError, InvalidWorkRegimeStatusError } from "../lib/errors.generated"; export interface DeactivateWorkRegimeInput { id: string; } /** * Function: deactivateWorkRegime * Description: Marks an ACTIVE work-regime catalog entry INACTIVE so it is * no longer selectable for new employments; historical references keep working. * Rejected when the entry is already INACTIVE. */ export async function run(db: Transaction, input: DeactivateWorkRegimeInput, _ctx: CommandContext) { const workRegime = await db .selectFrom("WorkRegime") .selectAll() .where("id", "=", input.id) .forUpdate() .executeTakeFirst(); if (!workRegime) { return err(new WorkRegimeNotFoundError(input.id)); } if (workRegime.status !== "ACTIVE") { return err(new InvalidWorkRegimeStatusError(`${input.id}:${workRegime.status}`)); } const updated = await db .updateTable("WorkRegime") .set({ status: "INACTIVE" }) .where("id", "=", input.id) .returningAll() .executeTakeFirstOrThrow(); return ok({ workRegime: updated }); }