import type { WorkOrderBoardEditableField } from "@/domain/work-orders/types/work-order-board"; import type { WorkOrderTablePatch } from "@/domain/work-orders/types/work-order-table-row"; import { mapDocStatusToApi } from "@/domain/work-orders/mappers/doc-status-mapper"; export type BoardFieldOp = { field: WorkOrderBoardEditableField; value: string; }; export { mapDocStatusToApi }; type ScalarBoardOpRule = { key: keyof WorkOrderTablePatch; field: WorkOrderBoardEditableField; toValue: (value: unknown) => string; }; /** One row per scalar patch field, in emission order. */ const SCALAR_BOARD_OP_RULES: ScalarBoardOpRule[] = [ { key: "woNumber", field: "woNumber", toValue: (value) => value as string }, // Severity before type so Emergency/Reactive never land without required severity. { key: "severity", field: "severity", toValue: (value) => (value == null ? "" : String(value)), }, { key: "type", field: "workOrderType", toValue: (value) => value as string }, { key: "site", field: "siteCode", toValue: (value) => value as string }, { key: "status", field: "lifecycleStatus", toValue: (value) => value as string }, { key: "dispatcherId", field: "assignTo", toValue: (value) => value as string }, { key: "dueDate", field: "dueDate", toValue: (value) => value as string }, { key: "vendorId", field: "vendorId", toValue: (value) => String(value) }, { key: "apptTime", field: "apptTime", toValue: (value) => value as string }, { key: "docStatus", field: "docStatus", toValue: (value) => mapDocStatusToApi(value as string) }, { key: "completedDate", field: "completedDate", toValue: (value) => value as string }, { key: "pm", field: "pm", toValue: (value) => value as string }, { key: "extraServices", field: "extraServices", toValue: (value) => JSON.stringify(value) }, { key: "serviceNotes", field: "serviceNotes", toValue: (value) => value as string }, { key: "flagColor", field: "flagColor", toValue: (value) => (value as string | null) ?? "" }, ]; export function expandScalarBoardOps(patch: WorkOrderTablePatch): BoardFieldOp[] { const ops: BoardFieldOp[] = []; for (const rule of SCALAR_BOARD_OP_RULES) { const value = patch[rule.key]; if (value === undefined) continue; ops.push({ field: rule.field, value: rule.toValue(value) }); } return ops; }