fix(work-orders): clear registry service when WO type changes

Selecting a work-order type on wizard step 1 only patched type and
severity, leaving pm, serviceId, extraServices, and the vendor filtered
by that service in the draft. Switching type (e.g. PM -> Reactive) then
submitted a serviceId that is not in the new type's registry list, which
backend ResolveServiceAsync (SH-187/#131) rejects as ServiceInvalid and
the create toast renders as the generic "Unable to create the work
order". Clear the service and its vendor whenever the type actually
changes; re-selecting the same type leaves the selection untouched.
This commit is contained in:
Codex Review Integration 2026-09-16 19:33:08 -03:00
parent 3a902bfebe
commit 71756ca3a5
2 changed files with 83 additions and 0 deletions

View file

@ -262,9 +262,23 @@ export function WizardStepTypeSchedule({
<WoTypePicker
selected={draft.type}
onSelect={(t) => {
const typeChanged = t !== draft.type;
onPatch({
type: t,
severity: nextSeverityForType(draft.type, t, draft.severity),
// A registry service is scoped to its work-order type, so a service picked
// under the previous type fails backend ResolveServiceAsync (SH-187/#131).
// Clear the service (and its vendor, filtered by the service) on a type change.
...(typeChanged
? {
pm: "",
serviceId: null,
extraServices: [],
vendorId: "",
vendorName: "",
techPhone: "",
}
: {}),
});
}}
/>

View file

@ -0,0 +1,69 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { WizardStepTypeSchedule } from "@/app/(protected)/workorders/_components/wizard/wizard-step-type-schedule";
import {
EMPTY_WIZARD_DRAFT,
type WorkOrderWizardDraft,
} from "@/domain/work-orders/types/work-order-wizard";
const USERS = [{ id: "sarah", name: "Sarah Chen" }];
describe("WizardStepTypeSchedule — service clear on type change", () => {
it("clears the service and its vendor when the work-order type changes", () => {
const onPatch = vi.fn();
const draft: WorkOrderWizardDraft = {
...EMPTY_WIZARD_DRAFT,
type: "PM",
pm: "HVAC",
serviceId: "1",
extraServices: ["Plumbing"],
vendorId: "9",
vendorName: "Old Vendor",
techPhone: "555-0100",
};
render(
<WizardStepTypeSchedule draft={draft} users={USERS} onUpdate={vi.fn()} onPatch={onPatch} />,
);
fireEvent.click(screen.getByRole("button", { name: /Reactive/ }));
expect(onPatch).toHaveBeenCalledWith(
expect.objectContaining({
type: "Reactive",
pm: "",
serviceId: null,
extraServices: [],
vendorId: "",
vendorName: "",
techPhone: "",
}),
);
});
it("keeps the selected service when the same type is re-selected", () => {
const onPatch = vi.fn();
const draft: WorkOrderWizardDraft = {
...EMPTY_WIZARD_DRAFT,
type: "PM",
pm: "HVAC",
serviceId: "1",
extraServices: ["Plumbing"],
vendorId: "9",
vendorName: "Old Vendor",
techPhone: "555-0100",
};
render(
<WizardStepTypeSchedule draft={draft} users={USERS} onUpdate={vi.fn()} onPatch={onPatch} />,
);
fireEvent.click(screen.getByRole("button", { name: /Scheduled preventive maintenance/ }));
const patch = onPatch.mock.calls[0][0] as Partial<WorkOrderWizardDraft>;
expect(patch).not.toHaveProperty("pm");
expect(patch).not.toHaveProperty("serviceId");
expect(patch).not.toHaveProperty("extraServices");
expect(patch).not.toHaveProperty("vendorId");
});
});