import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField } from "@tailor-platform/sdk"; import { appointmentType } from "./appointmentType"; import { assignment } from "./assignment"; import { position } from "./position"; import { worker } from "./worker"; const builtins = () => { return { // Intra-module FK: AppointmentHistory belongs to exactly one Worker workerId: db .uuid() .relation({ type: "n-1", toward: { type: worker, as: "worker" }, backward: "appointmentHistory", }) .description("Foreign key to the Worker this appointment (order) concerns"), // Intra-module FK: AppointmentHistory references the Assignment generation it created or changed assignmentId: db .uuid() .relation({ type: "n-1", toward: { type: assignment, as: "assignment" }, backward: "appointmentHistory", }) .description("Foreign key to the Assignment generation this appointment created or changed"), // Intra-module FK, optional: origin post of the move; absent for HIRE fromPositionId: db .uuid({ optional: true }) .relation({ type: "n-1", toward: { type: position, as: "fromPosition" }, backward: "appointmentsFrom", }) .description("Foreign key to the origin Position of this move; absent for HIRE"), // Intra-module FK, optional: destination post of the move; absent for TERMINATION toPositionId: db .uuid({ optional: true }) .relation({ type: "n-1", toward: { type: position, as: "toPosition" }, backward: "appointmentsTo", }) .description("Foreign key to the destination Position of this move; absent for TERMINATION"), // Intra-module FK to the AppointmentType catalog entry recorded by this appointment. The // business label (HIRE / PROMOTION / …) is catalog data referenced by id; the fixed behaviour // it drove is the catalog entry's `action`. appointmentTypeId: db .uuid() .relation({ type: "n-1", toward: { type: appointmentType, as: "appointmentType" }, backward: "appointmentHistory", }) .description("Foreign key to the AppointmentType catalog entry recorded by this entry"), // When the appointment takes effect effectiveDate: db.date().description("Date this appointment takes effect"), // appointment issue date; may precede effectiveDate for a future-dated appointment issuedAt: db .date() .description( "Date this appointment was issued (appointment issue date); may precede effectiveDate", ), // When the appointment's Assignment side effect was actually carried out. Set at record time // for appointments applied immediately (CREATE_* — a generation is opened even if future-dated — // and any non-future CHANGE/END). Null for a future-dated CHANGE/END: the entry is upcoming and // its side effect is applied by applyDueAppointments once effectiveDate arrives. appliedAt: db .datetime({ optional: true }) .description( "When the appointment's assignment side effect was applied; null = still upcoming", ), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateAppointmentHistoryTypeParams> { fields?: NoReservedFields; } export function createAppointmentHistoryType< const F extends Record = Record, >(params: CreateAppointmentHistoryTypeParams) { return db .table("AppointmentHistory", { ...builtins(), // Custom fields spread + timestamps ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } // Codegen instance; all intra-module FKs are always injected directly export const appointmentHistory = createAppointmentHistoryType({});