import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField } from "@tailor-platform/sdk"; import { workRule } from "./workRule"; const builtins = { // Polymorphic target (who is on the rule): 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, not 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 assignment applies to (cross-module): a Worker, Position, JobProfile, EmploymentType, or WorkRegime, per targetType", ), workRuleId: db .uuid() .relation({ type: "n-1", toward: { type: workRule, as: "workRule" }, backward: "workRuleAssignments", }) .description( "Foreign key to the WorkRule generation assigned; calculation re-resolves the generation effective on the calculated date via the rule's versionOf", ), // Effective-dating per ADR-013 effectiveStart: db.date().description("Date this assignment generation becomes effective"), effectiveEnd: db .date({ optional: true }) .description( "Date this assignment generation stops being effective; null means current generation", ), versionOf: db .uuid() .description( "Stable key grouping all generations of the same logical assignment (one target's rule binding) across time", ), }; type ReservedFields = keyof typeof builtins | CommonReservedFields; export interface CreateWorkRuleAssignmentTypeParams> { fields?: NoReservedFields; } export function createWorkRuleAssignmentType>( params: CreateWorkRuleAssignmentTypeParams, ) { return db .table("WorkRuleAssignment", { ...builtins, ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const workRuleAssignment = createWorkRuleAssignmentType({});