import type { CommandContext } from "@tailor-platform/erp-kit/core"; import type { Transaction } from "../generated/kysely-tailordb"; // The polymorphic target of an EligibilityRule / WorkRuleAssignment. Each variant is a workforce // entity referenced by id. export type EligibilityTargetType = | "WORKER" | "POSITION" | "JOB_PROFILE" | "EMPLOYMENT_TYPE" | "WORK_REGIME"; // The erp-kit Result union, narrowed to what the target guard reads. type Result = { ok: true; value: T } | { ok: false; error: { code: string } }; // One workforce lookup per targetType, resolving an id to an entity (ok) or reporting it missing. // Declared as minimal structural signatures (the `_approvalDeps` precedent) rather than derived from // WorkforceModule["queries"], so time-tracking stays free of the workforce module types — the app // composition adapts the real queries onto these seams. type TargetLookupFn = ( db: Transaction, input: { id: string }, ctx: CommandContext, ) => Promise>; export interface EligibilityTargetQueries { getWorker: TargetLookupFn; getPosition: TargetLookupFn; getJobProfile: TargetLookupFn; getEmploymentType: TargetLookupFn; getWorkRegime: TargetLookupFn; } /** * Function: eligibilityTargetExists * Description: Resolves a polymorphic target against workforce — dispatches on `targetType` to the * matching workforce lookup and reports whether `targetId` resolves to an existing entity. Shared by * createEligibilityRule / updateEligibilityRule / assignWorkRule so all three reject a dangling * cross-module target the same way (M17). */ export async function eligibilityTargetExists( db: Transaction, queries: EligibilityTargetQueries, targetType: EligibilityTargetType, targetId: string, ctx: CommandContext, ): Promise { switch (targetType) { case "WORKER": return (await queries.getWorker(db, { id: targetId }, ctx)).ok; case "POSITION": return (await queries.getPosition(db, { id: targetId }, ctx)).ok; case "JOB_PROFILE": return (await queries.getJobProfile(db, { id: targetId }, ctx)).ok; case "EMPLOYMENT_TYPE": return (await queries.getEmploymentType(db, { id: targetId }, ctx)).ok; case "WORK_REGIME": return (await queries.getWorkRegime(db, { id: targetId }, ctx)).ok; } }