import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField } from "@tailor-platform/sdk"; const builtins = { // Polymorphic target (who the rule applies to): a workforce entity referenced by id. targetType // is a discriminator; targetId is the referenced id for every type (Worker / Position / // JobProfile / EmploymentType catalog / WorkRegime catalog). No single fixed foreign type exists // across the union, so this is a discriminator + plain uuid rather than a `.relation()`. targetType: db .enum(["WORKER", "POSITION", "JOB_PROFILE", "EMPLOYMENT_TYPE", "WORK_REGIME"] as const) .description( "Whether the target is a Worker, Position, JobProfile, EmploymentType, or WorkRegime", ), targetId: db .uuid() .description( "Id of the target this rule applies to (cross-module): a Worker, Position, JobProfile, EmploymentType, or WorkRegime, per targetType", ), // Grant (what's permitted): currently a WorkRule referenced by id. Modeled as a plain field // rather than a `.relation()` to match the polymorphic-target shape above. The TIME_ENTRY_CODE // grant kind (and its companion timeEntryCodeKey) was dropped in issue #39 — nothing evaluated // it; the assignment path (assignWorkRule) only ever consults WORK_RULE grants. `grantType` is // kept as an enum and `workRuleId` stays optional to preserve the polymorphic-grant seam for a // future generalization (e.g. granting compliance/leave rules); command-layer validation // enforces that a WORK_RULE grant resolves a WorkRule. WorkRuleAssignment integration is #41. grantType: db .enum(["WORK_RULE"] as const) .description("What kind of grant this is; currently always WORK_RULE"), workRuleId: db .uuid({ optional: true }) .description("Id of the WorkRule granted to the target; set when grantType is WORK_RULE"), // Effective-dating per ADR-013 effectiveStart: db.date().description("Date this eligibility generation becomes effective"), effectiveEnd: db .date({ optional: true }) .description( "Date this eligibility generation stops being effective; null means current generation", ), versionOf: db .uuid() .description( "Stable key grouping all generations of the same logical eligibility rule across time", ), }; type ReservedFields = keyof typeof builtins | CommonReservedFields; export interface CreateEligibilityRuleTypeParams> { fields?: NoReservedFields; } export function createEligibilityRuleType>( params: CreateEligibilityRuleTypeParams, ) { return db .table("EligibilityRule", { ...builtins, ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const eligibilityRule = createEligibilityRuleType({});