Stop party sync feedback loop causing repeated PATCH overfetch.

Cache FunZone snapshot sync keys and harden partyNeedsSync comparisons so purchases/suppliers no longer re-patch the same party on every store update.
This commit is contained in:
2026-07-08 15:49:33 +03:30
parent 11dfe28623
commit f1a8380a58
4 changed files with 91 additions and 30 deletions

View File

@@ -9,7 +9,7 @@ import { findExistingParty, mergeFunZoneParties, partyNeedsSync } from './funzon
/** Syncs FunZone owners/customers into accounting parties on app load. */
export function GlobalPartySync() {
const { ready, upsertPartyAsync, reload } = useStore()
const { ready, upsertPartyAsync } = useStore()
const synced = useRef(false)
useEffect(() => {
@@ -36,16 +36,21 @@ export function GlobalPartySync() {
for (const incoming of batches) {
const existing = findExistingParty(currentParties, incoming.externalId!, incoming.externalSource)
if (existing && !partyNeedsSync(existing, incoming)) continue
await upsertPartyAsync({ ...incoming, id: existing?.id ?? createId('party-') })
const next = { ...incoming, id: existing?.id ?? createId('party-') }
await upsertPartyAsync(next)
// Keep the local snapshot in sync so later comparisons in this pass don't re-PATCH.
currentParties = existing
? currentParties.map((p) => (p.id === existing.id ? { ...p, ...next } : p))
: [...currentParties, next]
}
reload()
// upsertPartyAsync already updates AppStore; avoid a full reload that re-fetches every resource.
} catch {
synced.current = false
}
}
void run()
}, [ready, upsertPartyAsync, reload])
}, [ready, upsertPartyAsync])
return null
}