import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; import { reportedTimeBlock } from "./reportedTimeBlock"; import { timecard } from "./timecard"; const builtins = (params: { userType?: TailorAnyDBType }) => { // Cross-module FK: relation only when the foreign type is injected; plain uuid at codegen const correctedBy = db .uuid() .description("Foreign key to the user-management User who made this historical correction"); return { // Intra-module FK (required): the LOCKED Timecard this correction was recorded against timecardId: db .uuid() .relation({ type: "n-1", toward: { type: timecard, as: "timecard" }, backward: "correctionLogs", }) .description("Foreign key to the LOCKED Timecard whose period this correction changed"), // Intra-module FK (optional): present when the correction targets a specific reported block targetReportedBlockId: db .uuid({ optional: true }) .relation({ type: "n-1", toward: { type: reportedTimeBlock, as: "targetReportedBlock" }, backward: "correctionLogs", }) .description( "Foreign key to the specific ReportedTimeBlock this correction targets; absent for period-level corrections", ), field: db.string().description("Name of the single atomic field that was changed"), previousValue: db.string().description("Value of the field before this correction"), newValue: db.string().description("Value of the field after this correction"), reason: db .string() .description("Mandatory, non-empty reason explaining why this correction was made"), // Cross-module FK (required): every entry records the actor who made the correction correctedBy: params.userType ? correctedBy.relation({ type: "n-1", toward: { type: params.userType, as: "correctedByUser" }, backward: "timeCorrectionLogs", }) : correctedBy, correctedAt: db.datetime().description("Timestamp this historical correction was recorded"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateTimeCorrectionLogTypeParams> { fields?: NoReservedFields; userType?: TailorAnyDBType; } export function createTimeCorrectionLogType>( params: CreateTimeCorrectionLogTypeParams, ) { return db .table("TimeCorrectionLog", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } // Codegen instance with no injected cross-module types; real FK (correctedBy -> // user-management User) injected via module.ts export const timeCorrectionLog = createTimeCorrectionLogType({});