mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-02 00:23:13 +00:00
fix(work-orders): refresh CompDoc on Generate failure and drop EditWorkorder date helper
Invalidate board/location queries when Save Draft or Generate fails so partial prerequisite writes stay authoritative, and remove the unused CompDoc mapCompletedDateUpdateFormData EditWorkorder helper.
This commit is contained in:
parent
6481d9ce86
commit
7ea0d2c464
4 changed files with 18 additions and 35 deletions
|
|
@ -137,6 +137,14 @@ export function useCompDocDialog({
|
|||
void persist({}, "No");
|
||||
}
|
||||
|
||||
function refreshAuthoritativeState() {
|
||||
void queryClient.invalidateQueries({ queryKey: queryKeys.workOrders.all });
|
||||
if (locId && !isMockLocation) {
|
||||
void queryClient.invalidateQueries({ queryKey: queryKeys.locations.detail(locId) });
|
||||
void queryClient.invalidateQueries({ queryKey: queryKeys.locations.all });
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSaveDraft() {
|
||||
try {
|
||||
await syncCompletedDateIfChanged();
|
||||
|
|
@ -144,6 +152,7 @@ export function useCompDocDialog({
|
|||
await persist({}, "No");
|
||||
showCompDocDraftSavedToast();
|
||||
} catch (error) {
|
||||
refreshAuthoritativeState();
|
||||
toast.error(error instanceof Error ? error.message : "Failed to save draft");
|
||||
}
|
||||
}
|
||||
|
|
@ -158,6 +167,8 @@ export function useCompDocDialog({
|
|||
setGeneratedAt(ts);
|
||||
setDocState("generated");
|
||||
} catch (error) {
|
||||
// Keep Draft UI; refresh so any partially persisted date/address is authoritative.
|
||||
refreshAuthoritativeState();
|
||||
toast.error(error instanceof Error ? error.message : "Failed to generate document");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -405,17 +405,6 @@ export function mapWorkOrderFormToUpdateFormData(
|
|||
return formData;
|
||||
}
|
||||
|
||||
/** Partial EditWorkorder payload — only Id + CompletedDate (avoids legacy GetWorkorderById). */
|
||||
export function mapCompletedDateUpdateFormData(
|
||||
id: string | number,
|
||||
completedDate: string,
|
||||
): FormData {
|
||||
const formData = new FormData();
|
||||
formData.append("Id", String(id));
|
||||
appendIfPresent(formData, "CompletedDate", completedDate);
|
||||
return formData;
|
||||
}
|
||||
|
||||
export function mapLocationOption(raw: unknown): LocationOption {
|
||||
const item = asRecord(raw);
|
||||
const name = readString(item, "name", "Name", "locationName", "LocationName");
|
||||
|
|
|
|||
|
|
@ -148,11 +148,12 @@ describe("CompDocDialog Save Draft / Generate completion contract", () => {
|
|||
expect(row.status).toBe("Scheduled");
|
||||
});
|
||||
|
||||
it("Generate does not write docStatus Yes when completedDate persistence fails", async () => {
|
||||
it("Generate keeps Draft and skips docStatus Yes when completedDate patch fails", async () => {
|
||||
const row = baseRow({ completedDate: "2026-07-01" });
|
||||
const client = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
|
||||
});
|
||||
const invalidateSpy = vi.spyOn(client, "invalidateQueries");
|
||||
onDateCompletedChange.mockRejectedValueOnce(new Error("conflict"));
|
||||
|
||||
render(
|
||||
|
|
@ -174,5 +175,9 @@ describe("CompDocDialog Save Draft / Generate completion contract", () => {
|
|||
expect(onDateCompletedChange).toHaveBeenCalledWith("2026-07-20");
|
||||
});
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(screen.getByRole("button", { name: /^generate$/i })).toBeInTheDocument();
|
||||
await waitFor(() => {
|
||||
expect(invalidateSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
mapCompletedDateUpdateFormData,
|
||||
mapWorkOrderFormToCreatePayload,
|
||||
mapWorkOrderFormToUpdateFormData,
|
||||
} from "@/domain/work-orders/mappers/work-order-mapper";
|
||||
|
|
@ -137,7 +136,7 @@ describe("mapWorkOrderFormToUpdateFormData", () => {
|
|||
expect(formData.getAll("CategoryIds")).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps completedDate on EditWorkorder and omits DocStatus (board field)", () => {
|
||||
it("keeps completedDate on full EditWorkorder form and omits DocStatus (board field)", () => {
|
||||
const values = {
|
||||
...createDefaultWorkOrderFormValues(),
|
||||
createdDate: "2026-01-15",
|
||||
|
|
@ -151,24 +150,3 @@ describe("mapWorkOrderFormToUpdateFormData", () => {
|
|||
expect(formData.has("DocStatus")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mapCompletedDateUpdateFormData", () => {
|
||||
it("builds FormData with only Id and CompletedDate", () => {
|
||||
const formData = mapCompletedDateUpdateFormData(42, "2026-07-30");
|
||||
|
||||
expect(formData.get("Id")).toBe("42");
|
||||
expect(formData.get("CompletedDate")).toBe("2026-07-30");
|
||||
expect(formData.get("Status")).toBeNull();
|
||||
expect(formData.get("WorkerOrderTitle")).toBeNull();
|
||||
expect(formData.get("Description")).toBeNull();
|
||||
expect(formData.getAll("ContactIds")).toEqual([]);
|
||||
expect(formData.getAll("CategoryIds")).toEqual([]);
|
||||
});
|
||||
|
||||
it("omits empty CompletedDate", () => {
|
||||
const formData = mapCompletedDateUpdateFormData(1, " ");
|
||||
|
||||
expect(formData.get("Id")).toBe("1");
|
||||
expect(formData.get("CompletedDate")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue