import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; // Entitlement basis (ADR-005): STATUTORY law-based annual leave, COMPENSATORY compensatory leave, MANUAL discretionary const GRANT_TYPES = ["STATUTORY", "COMPENSATORY", "MANUAL"] as const; // Provenance: HIRE front-load, ANNIVERSARY batch, MANUAL administrator action const GRANT_SOURCES = ["HIRE", "ANNIVERSARY", "MANUAL"] as const; const builtins = (params: { workerType?: TailorAnyDBType }) => { // Cross-module FK: relation only when the foreign type is injected; plain uuid at codegen const workerId = db .uuid() .description( "Foreign key to the workforce Worker who owns this grant (entitlement follows the person; ADR-016)", ); return { workerId: params.workerType ? workerId.relation({ type: "n-1", toward: { type: params.workerType, as: "worker" }, backward: "leaveGrants", }) : workerId, // By-key reference to LeaveType; no FK relation leaveTypeKey: db .string() .description("Key of the LeaveType this grant contributes to (by-key reference, no FK)"), grantType: db .enum(GRANT_TYPES) .description( "Entitlement basis: STATUTORY (law-based annual leave), COMPENSATORY (compensatory leave), or MANUAL (discretionary)", ), grantSource: db .enum(GRANT_SOURCES) .description( "Provenance: HIRE front-load, ANNIVERSARY batch, or MANUAL administrator action", ), grantedDays: db .decimal() .description( "Originally granted day amount (positive decimal; half-day 0.5 increments valid)", ), remainingDays: db .decimal() .description("Still-available day amount; between 0 and grantedDays inclusive"), grantedDate: db.date().description("Date this grant becomes valid"), expirationDate: db.date().description("Date this grant lapses; strictly after grantedDate"), expiredAt: db .datetime({ optional: true }) .description( "Timestamp the nightly expiration sweep forfeited the unused remainder; unset while unexpired", ), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateLeaveGrantTypeParams> { fields?: NoReservedFields; workerType?: TailorAnyDBType; } export function createLeaveGrantType>( params: CreateLeaveGrantTypeParams, ) { return db .table("LeaveGrant", { ...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 leaveGrant = createLeaveGrantType({});