fix: resend offline-queued entries with an idempotency key

When the server saved an inspection, harvest, treatment, feeding, varroa
measurement or swarm check but the answer was lost (dead zone at the
apiary), the form queued the entry and the sync later created it a second
time - including a second automation task. Each opened form now creates
one Idempotency-Key, sends it with the request and stores it with the
queued entry, so the retry returns the first record. Entries queued before
this change still sync without a key.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Nick SzaunigandClaude Opus 5.5 committed 2026-09-27 05:31:04 +02:00
1 parent 5d11756a87
commit 5c2ea7bf1e
16 files changed
+139 -33

No files matched your search

@@ -54,6 +54,26 @@ describe("syncPendingInspections", () => {
expect(mockedRemovePending).toHaveBeenCalledWith(2);
});
it("resends a queued entry with the idempotency key it was queued with", async () => {
// The server may already have saved the first attempt; the same key lets
// it answer with that record instead of storing a duplicate.
mockedGetPending.mockResolvedValue([
{ ...pendingEntry(1), idempotencyKey: "key-from-the-form-0001" },
pendingEntry(2),
]);
mockedCreateInspection.mockResolvedValue({ id: "x" } as never);
await syncPendingInspections();
expect(mockedCreateInspection).toHaveBeenNthCalledWith(1, pendingEntry(1).input, {
idempotencyKey: "key-from-the-form-0001",
});
// Entries queued before keys existed still sync, just without one.
expect(mockedCreateInspection).toHaveBeenNthCalledWith(2, pendingEntry(2).input, {
idempotencyKey: undefined,
});
});
it("keeps a rejected entry queued and continues with the rest on a server-side ApiError", async () => {
mockedGetPending.mockResolvedValue([pendingEntry(1), pendingEntry(2)]);
mockedCreateInspection