mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-01 09:43:11 +00:00
162 lines
4.5 KiB
TypeScript
162 lines
4.5 KiB
TypeScript
import type {
|
|
WorkOrderBoardEditableField,
|
|
WorkOrderBoardPatchPayload,
|
|
} from "@/domain/work-orders/types/work-order-board";
|
|
import type {
|
|
WorkOrderTablePatch,
|
|
WorkOrderTableRow,
|
|
} from "@/domain/work-orders/types/work-order-table-row";
|
|
|
|
export type BoardFieldOp = {
|
|
field: WorkOrderBoardEditableField;
|
|
value: string;
|
|
};
|
|
|
|
const DISPATCH_FIELDS = new Set<WorkOrderBoardEditableField>(["vendorId", "apptDate", "apptTime"]);
|
|
|
|
const LOCAL_ONLY_KEYS = new Set<keyof WorkOrderTablePatch>([
|
|
"mediaCount",
|
|
"dayGroup",
|
|
"dayLabel",
|
|
"dispatcherName",
|
|
"company",
|
|
"tech",
|
|
"techPhone",
|
|
"location",
|
|
"locationId",
|
|
"pocName",
|
|
"pocPhone",
|
|
"pocNotes",
|
|
"title",
|
|
"priority",
|
|
"apptStart",
|
|
"apptEnd",
|
|
"rescheduleCount",
|
|
"carriedOver",
|
|
"isPastDue",
|
|
"originalWeek",
|
|
"originalDate",
|
|
]);
|
|
|
|
export function mapDocStatusToApi(value: string): string {
|
|
if (value === "NN") {
|
|
return "NotNeeded";
|
|
}
|
|
return value;
|
|
}
|
|
|
|
export function isLocalOnlyTablePatch(patch: WorkOrderTablePatch): boolean {
|
|
const keys = Object.keys(patch) as (keyof WorkOrderTablePatch)[];
|
|
return keys.length > 0 && keys.every((key) => LOCAL_ONLY_KEYS.has(key));
|
|
}
|
|
|
|
/**
|
|
* Expands a UI table patch into ordered board field ops (one PATCH each).
|
|
* Schedule/week-only fields are emitted first in a stable order.
|
|
*/
|
|
export function expandTablePatchToBoardFieldOps(patch: WorkOrderTablePatch): BoardFieldOp[] {
|
|
if (isLocalOnlyTablePatch(patch)) {
|
|
return [];
|
|
}
|
|
|
|
const ops: BoardFieldOp[] = [];
|
|
|
|
const hasScheduleRelated =
|
|
patch.scheduledOn !== undefined ||
|
|
patch.targetWeek !== undefined ||
|
|
patch.scheduleWeekOnly !== undefined;
|
|
|
|
if (hasScheduleRelated) {
|
|
if (patch.scheduleWeekOnly === true) {
|
|
ops.push({ field: "scheduleWeekOnly", value: "true" });
|
|
if (patch.targetWeek !== undefined) {
|
|
ops.push({ field: "targetWeek", value: patch.targetWeek });
|
|
}
|
|
if (patch.scheduledOn !== undefined) {
|
|
ops.push({ field: "scheduledDate", value: patch.scheduledOn });
|
|
}
|
|
} else {
|
|
if (patch.scheduleWeekOnly === false) {
|
|
ops.push({ field: "scheduleWeekOnly", value: "false" });
|
|
}
|
|
if (patch.scheduledOn !== undefined) {
|
|
ops.push({ field: "scheduledDate", value: patch.scheduledOn });
|
|
}
|
|
if (patch.targetWeek !== undefined) {
|
|
ops.push({ field: "targetWeek", value: patch.targetWeek });
|
|
}
|
|
}
|
|
}
|
|
|
|
if (patch.woNumber !== undefined) {
|
|
ops.push({ field: "woNumber", value: patch.woNumber });
|
|
}
|
|
if (patch.type !== undefined) {
|
|
ops.push({ field: "workOrderType", value: patch.type });
|
|
}
|
|
if (patch.site !== undefined) {
|
|
ops.push({ field: "siteCode", value: patch.site });
|
|
}
|
|
if (patch.status !== undefined) {
|
|
ops.push({ field: "lifecycleStatus", value: patch.status });
|
|
}
|
|
if (patch.dispatcherId !== undefined) {
|
|
ops.push({ field: "assignTo", value: patch.dispatcherId });
|
|
}
|
|
if (patch.dueDate !== undefined) {
|
|
ops.push({ field: "dueDate", value: patch.dueDate });
|
|
}
|
|
if (patch.vendorId !== undefined) {
|
|
ops.push({ field: "vendorId", value: String(patch.vendorId) });
|
|
}
|
|
if (patch.apptTime !== undefined) {
|
|
ops.push({ field: "apptTime", value: patch.apptTime });
|
|
}
|
|
if (patch.docStatus !== undefined) {
|
|
ops.push({ field: "docStatus", value: mapDocStatusToApi(patch.docStatus) });
|
|
}
|
|
if (patch.pm !== undefined) {
|
|
ops.push({ field: "pm", value: patch.pm });
|
|
}
|
|
if (patch.extraServices !== undefined) {
|
|
ops.push({ field: "extraServices", value: JSON.stringify(patch.extraServices) });
|
|
}
|
|
if (patch.serviceNotes !== undefined) {
|
|
ops.push({ field: "serviceNotes", value: patch.serviceNotes });
|
|
}
|
|
if (patch.flagColor !== undefined) {
|
|
ops.push({ field: "flagColor", value: patch.flagColor ?? "" });
|
|
}
|
|
|
|
return ops;
|
|
}
|
|
|
|
export function isDispatchBoardField(field: WorkOrderBoardEditableField): boolean {
|
|
return DISPATCH_FIELDS.has(field);
|
|
}
|
|
|
|
export function buildBoardPatchPayload(
|
|
op: BoardFieldOp,
|
|
row: Pick<WorkOrderTableRow, "rowVersion" | "dispatchRowVersion" | "primaryDispatchId">,
|
|
): WorkOrderBoardPatchPayload {
|
|
if (!row.rowVersion) {
|
|
throw new Error("workOrderVersion is required for board patch");
|
|
}
|
|
|
|
const payload: WorkOrderBoardPatchPayload = {
|
|
field: op.field,
|
|
value: op.value,
|
|
workOrderVersion: row.rowVersion,
|
|
};
|
|
|
|
if (isDispatchBoardField(op.field)) {
|
|
if (row.dispatchRowVersion) {
|
|
payload.dispatchVersion = row.dispatchRowVersion;
|
|
}
|
|
if (row.primaryDispatchId != null) {
|
|
payload.primaryDispatchId = row.primaryDispatchId;
|
|
}
|
|
}
|
|
|
|
return payload;
|
|
}
|