{(posting) => posting ? <>
+ Posted to warehouse {posting.warehouseId} on {new Date(posting.createdAt).toLocaleString()}.
+ entry.id)}
+ rows={posting.entries.map((entry) => [
+ lineNames.get(entry.receiptLineId) ?? entry.receiptLineId,
+ entry.delta,
+ View stock ledger,
+ ])} />
+ {!posting.entries.length && The inspection accepted zero material. The posting recorded no stock movements.
}
+ > : can("inventory.manage") ? (
+
+ ) : This receipt is awaiting warehouse posting.}
+ ;
+}
+
+function PostReceipt({ path, receiptId, refresh }: {
+ path: string;
+ receiptId: string;
+ refresh: () => Promise;
+}) {
+ const { scope } = usePlatform();
+ const client = useQueryClient();
+ const command = useIdempotentCommand(path, receiptPostingInput, receiptPosting, receiptId);
+ async function submit(input: unknown) {
+ try {
+ await command.run(input);
+ } catch (error) {
+ if (error instanceof ApiError && error.code === "MATERIAL_RECEIPT_ALREADY_POSTED") await refresh();
+ throw error;
+ }
+ await client.invalidateQueries({
+ predicate: ({ queryKey }) => queryKey[0] === "private" && queryKey[1] === scope &&
+ typeof queryKey[2] === "string" && queryKey[2].startsWith("/inventory/material-stock-items"),
+ });
+ await refresh();
+ }
+ return command.pending ? (
+ ({})} submit={submit}>
+ The server may have posted this receipt. Retrying uses the same request and warehouse {command.pending.warehouseId}.
+
+ ) : ;
+}
+
+function WarehousePicker({ submit }: { submit: (input: unknown) => Promise }) {
+ const [offset, setOffset] = useState(0);
+ const query = usePrivateQuery(`/inventory/warehouses?limit=25&offset=${offset}`, z.array(warehouse));
+ return {(rows) => <>
+ {rows.length ? ({ warehouseId: readText(data, "warehouseId") })} submit={submit}>
+ Choose the warehouse to credit with all accepted quantities from this receipt. A receipt can be posted once.
+ ({ value: row.id, label: row.name })),
+ ]} />
+
+ : <>
+ No warehouses on this page.
+ {offset > 0 && }
+ >}
+ >};
+}
diff --git a/test/workflows.ui.test.tsx b/test/workflows.ui.test.tsx
index 49dd859..db262ce 100644
--- a/test/workflows.ui.test.tsx
+++ b/test/workflows.ui.test.tsx
@@ -12,6 +12,7 @@ import CartPage from "../src/pages/CartPage";
import AccessPage, { RolesPage } from "../src/pages/AccessPage";
import OrdersPage from "../src/pages/OrdersPage";
import ProcurementPage from "../src/pages/ProcurementPage";
+import { ReceiptStockPosting } from "../src/pages/ReceiptStockPosting";
import theme from "../src/theme";
import { Switch, Route } from "wouter";
const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
@@ -173,6 +174,45 @@ function harness(children: ReactNode) {
);
return { client, ...result };
}
+
+test("receipt posting retries the same warehouse and key after a lost response", async () => {
+ const attempts: unknown[] = [];
+ const posting = {
+ id, receiptId: id, warehouseId: id, createdAt: "2026-11-12T00:00:00.000Z",
+ entries: [{ id, stockItemId: id, receiptLineId: id, kind: "RECEIPT_ACCEPTED", delta: "0.125" }],
+ };
+ vi.stubGlobal("fetch", vi.fn(async (url: string, options?: RequestInit) => {
+ if (url.endsWith("/auth/login")) return json(session);
+ if (url.endsWith("/auth/me")) return json({ ...me, permissions: ["inventory.read", "inventory.manage"] });
+ if (url.includes("/inventory/warehouses?")) return json([{ id, name: "Main warehouse" }]);
+ if (url.endsWith("/stock-posting")) {
+ if (options?.method === "POST") {
+ attempts.push(JSON.parse(String(options.body)));
+ if (attempts.length === 1) throw new Error("Response lost");
+ return json(posting);
+ }
+ return attempts.length ? json(posting) : json({ code: "MATERIAL_POSTING_NOT_FOUND" }, 404);
+ }
+ throw new Error(`Unexpected request ${url}`);
+ }));
+ function AuthorizedPosting() {
+ const { can } = usePlatform();
+ return can("inventory.read") ? : null;
+ }
+ harness();
+ const user = userEvent.setup();
+ await user.click(screen.getByText("Test login"));
+ await user.selectOptions(await screen.findByRole("combobox", { name: "Destination warehouse" }), id);
+ await user.click(screen.getByRole("button", { name: "Post accepted material" }));
+ await user.click(await screen.findByRole("button", { name: "Retry warehouse posting" }));
+ const ledger = await screen.findByRole("link", { name: "View stock ledger" });
+ expect(ledger.getAttribute("href")).toBe(`/admin/material-stock/${id}`);
+ expect(attempts).toHaveLength(2);
+ expect(attempts[0]).toEqual({ warehouseId: id, idempotencyKey: expect.any(String) });
+ expect(attempts[1]).toEqual(attempts[0]);
+ expect(screen.getByText("0.125")).toBeTruthy();
+});
+
test("uncertain checkout replays identical input after the cart version changes", async () => {
const attempts: unknown[] = [];
let cartChanged = false;