mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-06 12:22:03 +00:00
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:
parent
3a902bfebe
commit
71756ca3a5
2 changed files with 83 additions and 0 deletions
|
|
@ -262,9 +262,23 @@ export function WizardStepTypeSchedule({
|
||||||
<WoTypePicker
|
<WoTypePicker
|
||||||
selected={draft.type}
|
selected={draft.type}
|
||||||
onSelect={(t) => {
|
onSelect={(t) => {
|
||||||
|
const typeChanged = t !== draft.type;
|
||||||
onPatch({
|
onPatch({
|
||||||
type: t,
|
type: t,
|
||||||
severity: nextSeverityForType(draft.type, t, draft.severity),
|
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: "",
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
|
|
@ -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");
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Reference in a new issue