From f36a07c99c55a323406eaa652bef06e8817f16a8 Mon Sep 17 00:00:00 2001 From: Alexandre Brandizzi Date: Fri, 25 Sep 2026 19:15:22 -0300 Subject: [PATCH] fix(workorders): Site dialog clears the previous site's contact on a site switch Picking another site on an existing work order kept the old site's POC name, phone and notes in the fields. When the new site's record failed to load, the dialog never synced it, Save stayed enabled, and the work order pointing at the new site was saved with the previous site's contact and notes as its override. A site switch now empties the POC name and phone for every work order, and the notes for existing ones, so the fields hold either the new site's record or what the dispatcher types for it. --- .../cells/use-site-dialog-form-fields.ts | 8 +++-- .../site-dialog-site-record-resync.test.tsx | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/app/(protected)/workorders/_components/list/table/cells/use-site-dialog-form-fields.ts b/src/app/(protected)/workorders/_components/list/table/cells/use-site-dialog-form-fields.ts index db1db3e7..25ccb0d2 100644 --- a/src/app/(protected)/workorders/_components/list/table/cells/use-site-dialog-form-fields.ts +++ b/src/app/(protected)/workorders/_components/list/table/cells/use-site-dialog-form-fields.ts @@ -97,9 +97,11 @@ export function useSiteDialogFormFields({ setContactsDirty(true); setUserEdited(false); setDirty(true); - if (createMode) { - setPn(""); - setPp(""); + // The previous site's contact never carries over, even when the new site fails to load. + setPn(""); + setPp(""); + if (!createMode) { + setNotes(""); } setPocFilledFor(""); } else if (id !== pocFilledFor) { diff --git a/src/test/app/(protected)/workorders/site-dialog-site-record-resync.test.tsx b/src/test/app/(protected)/workorders/site-dialog-site-record-resync.test.tsx index 12be55f2..e61015cd 100644 --- a/src/test/app/(protected)/workorders/site-dialog-site-record-resync.test.tsx +++ b/src/test/app/(protected)/workorders/site-dialog-site-record-resync.test.tsx @@ -133,6 +133,41 @@ describe("Work order Site dialog keeps the site record in sync", () => { }); }); + it("does not carry the previous site's contact onto a site whose record failed to load", async () => { + // The legacy contact fields too, so nothing can refill the cleared fields from DAL1. + const dallasWithContact: Location = { ...dallas, contact: "Jane", phone: "(421) 433-0032" }; + getById.mockImplementation((id: string) => + String(id) === "1" + ? Promise.resolve(dallasWithContact) + : Promise.reject(new Error("Network error")), + ); + const { onSave } = renderDialog(); + expect(await screen.findByDisplayValue("Jane")).toBeInTheDocument(); + + pickSite("CHI2"); + expect(await screen.findByText(SITE_UNAVAILABLE_COPY)).toBeInTheDocument(); + expect(primaryField("POC name")).toHaveValue(""); + expect(primaryField("POC phone")).toHaveValue(""); + expect(primaryField("Notes…")).toHaveValue(""); + fireEvent.click(screen.getByRole("button", { name: /^save$/i })); + expect(await screen.findByText(/name is required/i)).toBeInTheDocument(); + expect(onSave).not.toHaveBeenCalled(); + + fireEvent.change(primaryField("POC name"), { target: { value: "Carl" } }); + fireEvent.change(primaryField("POC phone"), { target: { value: "(312) 555-0101" } }); + fireEvent.click(screen.getByRole("button", { name: /^save$/i })); + + await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1)); + expect(onSave.mock.calls[0][0]).toMatchObject({ + locationId: "2", + pocName: "Carl", + pocPhone: "(312) 555-0101", + pocNotes: "", + additionalContacts: [], + }); + expect(updateContactInfo).not.toHaveBeenCalled(); + }); + it("waits for the refetch instead of syncing a cached site record, so a save never reverts it", async () => { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } },