import { defaultGqlPermission, defaultPermission } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBType } from "@tailor-platform/sdk"; import { jobProfile } from "./jobProfile"; const builtins = (params: { departmentType?: TailorAnyDBType; siteType?: TailorAnyDBType }) => { // Cross-module FK: relation only when the foreign type is injected; plain uuid at codegen const departmentId = db .uuid() .description("Foreign key to the organization Department this Position belongs to"); // Cross-module FK, optional: not every post is bound to a physical Site const siteId = db .uuid({ optional: true }) .description("Foreign key to the organization Site this Position is located at, if site-bound"); return { departmentId: params.departmentType ? departmentId.relation({ type: "n-1", toward: { type: params.departmentType, as: "department" }, backward: "positions", }) : departmentId, siteId: params.siteType ? siteId.relation({ type: "n-1", toward: { type: params.siteType, as: "site" }, backward: "positions", }) : siteId, // Intra-module FK: JobProfile supplies the role definition for this post jobProfileId: db .uuid() .relation({ type: "n-1", toward: { type: jobProfile, as: "jobProfile" }, backward: "positions", }) .description("Foreign key to the JobProfile defining this post's role"), // Number of seats this Position represents; defaults to 1 at the command layer headcount: db.int().description("Number of seats this Position represents"), // Effective-dating per ADR-013 effectiveStart: db.date().description("Date this Position generation becomes effective"), effectiveEnd: db .date({ optional: true }) .description( "Date this Position generation stops being effective; null means current generation", ), versionOf: db .uuid() .description("Stable key grouping all generations of the same logical Position across time"), }; }; // Position is an internal effective-dated master; consumers do not extend it, so no custom-field slot. export interface CreatePositionTypeParams { departmentType?: TailorAnyDBType; siteType?: TailorAnyDBType; } export function createPositionType(params: CreatePositionTypeParams) { return db .table("Position", { ...builtins(params), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } // Codegen instance with no cross-module types; real FKs injected via module.ts export const position = createPositionType({});