import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; import { employmentType } from "./employmentType"; import { worker } from "./worker"; import { workRegime } from "./workRegime"; const builtins = (params: { companyType?: TailorAnyDBType }) => { // Cross-module FK: relation only when the foreign type is injected; plain uuid at codegen const companyId = db .uuid() .description( "Foreign key to the organization module's Company (legal entity) employing the Worker", ); return { // Intra-module FK: WorkerEmployment belongs to exactly one Worker workerId: db .uuid() .relation({ type: "n-1", toward: { type: worker, as: "worker" }, backward: "employments" }) .description("Foreign key to the Worker holding this employment"), // Cross-module FK: root scope seam for future multi-legal-entity support (ADR-018) companyId: params.companyType ? companyId.relation({ type: "n-1", toward: { type: params.companyType, as: "company" }, backward: "workerEmployments", }) : companyId, // Intra-module FK to the EmploymentType catalog entry (must belong to the same company and be // ACTIVE at creation — validated at the command layer). Referenced by id, not a fixed enum. employmentTypeId: db .uuid() .relation({ type: "n-1", toward: { type: employmentType, as: "employmentType" }, backward: "workerEmployments", }) .description("Foreign key to the EmploymentType catalog entry for this employment"), // Intra-module FK to the WorkRegime catalog entry (same-company + ACTIVE, validated at the // command layer). The work/time regime the calculation strategy and work rules key off. workRegimeId: db .uuid() .relation({ type: "n-1", toward: { type: workRegime, as: "workRegime" }, backward: "workerEmployments", }) .description("Foreign key to the WorkRegime catalog entry for this employment"), hireDate: db.date().description("Date the Worker was hired into this employment"), terminationDate: db .date({ optional: true }) .description("Date the employment was terminated; null while active"), // Effective-dating standard (ADR-013): generation series effectiveStart: db.date().description("Date this generation becomes effective"), effectiveEnd: db .date({ optional: true }) .description("Date this generation stops being effective; null means the current generation"), versionOf: db .uuid() .description("Stable key binding together all generations of one continuous employment"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateWorkerEmploymentTypeParams> { fields?: NoReservedFields; companyType?: TailorAnyDBType; } export function createWorkerEmploymentType>( params: CreateWorkerEmploymentTypeParams, ) { return db .table("WorkerEmployment", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } // Codegen instance with no cross-module types; real FK injected via module.ts export const workerEmployment = createWorkerEmploymentType({});