fix(workorders): sync the whole saved row into the panel detail

Copying only the version left the cached detail with pre-save fields, so a
queued second panel save could send them with a fresh version and silently
revert what the first save wrote (e.g. POC name).
This commit is contained in:
Alexandre Brandizzi 2026-10-02 00:11:11 -03:00
parent 32b4ea0efa
commit 47cdca55c2
2 changed files with 49 additions and 8 deletions

View file

@ -134,20 +134,19 @@ function readDetailRow(
return queryClient.getQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(id))?.info;
}
/** Carry a saved row's version into the open panel's detail before the next queued patch runs. */
function syncDetailRowVersion(
queryClient: QueryClient,
id: string | number,
row: WorkOrderTableRow,
) {
/** Carry a saved row into the open panel's detail before the next queued patch runs. */
function syncDetailRow(queryClient: QueryClient, id: string | number, row: WorkOrderTableRow) {
queryClient.setQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(id), (detail) =>
detail && row.rowVersion
? {
...detail,
// The whole saved row, not just its version: a queued patch built from fresh
// version + stale fields would silently revert what the previous save wrote.
info: {
...detail.info,
rowVersion: row.rowVersion,
...row,
dispatchRowVersion: row.dispatchRowVersion || detail.info.dispatchRowVersion,
mediaCount: detail.info.mediaCount,
},
}
: detail,
@ -166,7 +165,7 @@ function recordSavedRow(
row: WorkOrderTableRow,
) {
savedRows.set(String(id), row);
syncDetailRowVersion(queryClient, id, row);
syncDetailRow(queryClient, id, row);
}
async function runPatchAndSyncDetail(

View file

@ -7,10 +7,12 @@ import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-ta
import { queryKeys } from "@/infra/query-key/query-key";
const patchBoardField = vi.fn();
const updatePoc = vi.fn();
vi.mock("@/domain/work-orders/api/work-orders-api", () => ({
workOrdersApi: {
patchBoardField: (...args: unknown[]) => patchBoardField(...args),
updatePoc: (...args: unknown[]) => updatePoc(...args),
},
}));
@ -123,6 +125,46 @@ describe("useWorkOrderTableMutations for a work order outside the loaded list",
).toBe("saved-2");
});
it("keeps the first save's fields when a queued second panel save patches the same group", async () => {
const client = makeClient();
client.setQueryData<WorkOrderBoardDetail>(queryKeys.workOrders.boardDetail(9), {
info: { ...DETAIL_ROW, pocName: "Old POC", pocPhone: "111", pocNotes: "" },
} as WorkOrderBoardDetail);
let version = 0;
updatePoc.mockImplementation(
async (_id: unknown, payload: { pocName: string; pocPhone: string; pocNotes: string }) => {
version += 1;
return { ...DETAIL_ROW, ...payload, rowVersion: `saved-${version}` };
},
);
const onSecondSuccess = vi.fn();
const { result } = renderHook(
() =>
useWorkOrderTableMutations({
onPatch: vi.fn(),
clearPatch: vi.fn(),
getRow: () => undefined,
}),
{ wrapper: makeWrapper(client) },
);
act(() => {
result.current.patchField(9, { pocName: "New POC" }, { fallbackRow: DETAIL_ROW });
result.current.patchField(
9,
{ pocPhone: "555" },
{ fallbackRow: DETAIL_ROW, onSuccess: onSecondSuccess },
);
});
await waitFor(() => expect(onSecondSuccess).toHaveBeenCalled());
expect(updatePoc.mock.calls[1][1]).toMatchObject({
pocName: "New POC",
pocPhone: "555",
workOrderVersion: "saved-1",
});
});
it("chains the saved version when the panel's detail query never cached data", async () => {
let version = 0;
patchBoardField.mockImplementation(async () => {