import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core"; import type { Transaction } from "../generated/kysely-tailordb"; import { JobProfileInUseError, JobProfileNotFoundError } from "../lib/errors.generated"; export interface DeleteJobProfileInput { id: string; } /** * Function: deleteJobProfile * Description: Removes a JobProfile role template that is not referenced by * any Position — a role template is only safe to remove once no seat depends * on it. Since JobProfile is not effective-dated, deletion is a hard delete. */ export async function run(db: Transaction, input: DeleteJobProfileInput, _ctx: CommandContext) { const jobProfile = await db .selectFrom("JobProfile") .selectAll() .where("id", "=", input.id) .forUpdate() .executeTakeFirst(); if (!jobProfile) { return err(new JobProfileNotFoundError(input.id)); } const referencingPosition = await db .selectFrom("Position") .selectAll() .where("jobProfileId", "=", input.id) .executeTakeFirst(); if (referencingPosition) { return err(new JobProfileInUseError(input.id)); } await db.deleteFrom("JobProfile").where("id", "=", input.id).execute(); return ok({}); }