shoc-frontend-new/src/domain/work-orders/mappers/expand-scalar-board-ops.ts
Arthur Bassi 6e99dc0af2 refactor(work-orders): pass board-api governance maintainability gate
Split oversized API/mapper/UI modules and extract focused helpers so the
exact head satisfies godfile and complexity ratchets without relaxing thresholds.
2026-07-28 09:56:27 -03:00

49 lines
2 KiB
TypeScript

import type { WorkOrderBoardEditableField } from "@/domain/work-orders/types/work-order-board";
import type { WorkOrderTablePatch } from "@/domain/work-orders/types/work-order-table-row";
export type BoardFieldOp = {
field: WorkOrderBoardEditableField;
value: string;
};
export function mapDocStatusToApi(value: string): string {
if (value === "NN") {
return "NotNeeded";
}
return value;
}
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 },
{ 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: "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;
}