diff --git a/src/app/(protected)/workorders/_components/list/table/cells/build-site-dialog-patch.ts b/src/app/(protected)/workorders/_components/list/table/cells/build-site-dialog-patch.ts index fc557598..8cdac978 100644 --- a/src/app/(protected)/workorders/_components/list/table/cells/build-site-dialog-patch.ts +++ b/src/app/(protected)/workorders/_components/list/table/cells/build-site-dialog-patch.ts @@ -1,5 +1,6 @@ import { extractLocationLabel } from "@/app/(protected)/workorders/_components/list/table/cells/site-dialog-helpers"; import type { SitePatch } from "@/app/(protected)/workorders/_components/list/table/cells/site-dialog-types"; +import { normalizeAdditionalContacts } from "@/domain/work-orders/mappers/additional-contacts-mapper"; import { resolveSiteCode } from "@/domain/work-orders/mappers/work-order-board-mapper"; import type { WorkOrderAdditionalContact } from "@/domain/work-orders/types/work-order-additional-contact"; import type { LocationOption } from "@/domain/work-orders/types/work-order"; @@ -41,7 +42,8 @@ export function buildSiteDialogPatch({ // Following the site record, the dialog's extra contacts are the site's: copy them only when edited. const sendContacts = followsSiteRecord ? contactsDirty : contactsDirty || baselineHadContacts; if (sendContacts) { - patch.additionalContacts = extraContacts; + // The work order keeps only complete name/phone rows; site contact ids stay on the site request. + patch.additionalContacts = normalizeAdditionalContacts(extraContacts) ?? []; } return patch; } diff --git a/src/test/app/(protected)/workorders/build-site-dialog-patch.test.ts b/src/test/app/(protected)/workorders/build-site-dialog-patch.test.ts new file mode 100644 index 00000000..91f349c4 --- /dev/null +++ b/src/test/app/(protected)/workorders/build-site-dialog-patch.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, it } from "vitest"; +import { buildSiteDialogPatch } from "@/app/(protected)/workorders/_components/list/table/cells/build-site-dialog-patch"; + +const base = { + code: "S-100", + locId: "1", + selected: undefined, + pocName: "Jane", + pocPhone: "(421) 433-1111", + pocNotes: "", + contactsDirty: true, + baselineHadContacts: true, + followsSiteRecord: true, +}; + +describe("buildSiteDialogPatch", () => { + it("copies site-loaded extra contacts to the work order without their site contact ids", () => { + const patch = buildSiteDialogPatch({ + ...base, + extraContacts: [{ name: "Bob", phone: "(421) 433-7777", siteContactId: 32 }], + }); + + expect(patch.additionalContacts).toEqual([{ name: "Bob", phone: "(421) 433-7777" }]); + }); + + it("drops half-filled and empty extra contacts from the work-order patch", () => { + const patch = buildSiteDialogPatch({ + ...base, + extraContacts: [ + { name: "Bob", phone: "(421) 433-7777", notes: "Night shift", siteContactId: 32 }, + { name: "Half", phone: "", siteContactId: 33 }, + { name: "", phone: "(421) 433-0000" }, + { name: "", phone: "" }, + ], + }); + + expect(patch.additionalContacts).toEqual([ + { name: "Bob", phone: "(421) 433-7777", notes: "Night shift" }, + ]); + }); + + it("still clears the work order's copy when every extra contact was removed", () => { + const patch = buildSiteDialogPatch({ ...base, extraContacts: [] }); + + expect(patch.additionalContacts).toEqual([]); + }); + + it("leaves additional contacts off the patch when they were not edited", () => { + const patch = buildSiteDialogPatch({ + ...base, + contactsDirty: false, + extraContacts: [{ name: "Bob", phone: "(421) 433-7777", siteContactId: 32 }], + }); + + expect(patch).not.toHaveProperty("additionalContacts"); + }); +}); diff --git a/src/test/app/(protected)/workorders/site-dialog-site-record.test.tsx b/src/test/app/(protected)/workorders/site-dialog-site-record.test.tsx index 121e7dbd..c51d8130 100644 --- a/src/test/app/(protected)/workorders/site-dialog-site-record.test.tsx +++ b/src/test/app/(protected)/workorders/site-dialog-site-record.test.tsx @@ -195,7 +195,7 @@ describe("Work order Site dialog edits the site record", () => { pocName: "Jane", pocPhone: "(421) 433-9999", pocNotes: "", - additionalContacts: [{ name: "Bob", phone: "(421) 433-7777", siteContactId: 32 }], + additionalContacts: [{ name: "Bob", phone: "(421) 433-7777" }], }), ); expect(onOpenChange).toHaveBeenCalledWith(false);