fix(parcel-sync): populate county data during login, not via PATCH

Root cause: PATCH endpoint created a new EterraClient which tried
to re-login with expired session → 401. Now county refresh runs
immediately after successful login in the session route, using the
same authenticated client (fire-and-forget). Component reloads UAT
data 5s after connection to pick up fresh county info.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-22 22:23:46 +02:00
parent 379e7e4d3f
commit 899b5c4cf7
3 changed files with 243 additions and 42 deletions
@@ -554,51 +554,28 @@ export function ParcelSyncModule() {
}, [fetchDbSummary]);
/* ════════════════════════════════════════════════════════════ */
/* Auto-refresh county data when eTerra is connected */
/* Reload UAT data when session connects (county data may */
/* have been populated by the login flow) */
/* ════════════════════════════════════════════════════════════ */
const countyRefreshAttempted = useRef(false);
const prevConnected = useRef(false);
useEffect(() => {
if (!session.connected || countyRefreshAttempted.current) return;
if (uatData.length === 0) return;
// Check if most UATs are missing county data
const withCounty = uatData.filter((u) => u.county).length;
if (withCounty > uatData.length * 0.5) return; // >50% already have county
countyRefreshAttempted.current = true;
console.log("[uats] Auto-refreshing county data from eTerra…");
fetch("/api/eterra/uats", { method: "PATCH" })
.then((res) => res.json())
.then(
(result: {
updated?: number;
error?: string;
phase1?: number;
phase2?: number;
codeMatches?: number;
nameMatches?: number;
unmatched?: number;
debug?: unknown;
}) => {
console.log("[uats] County refresh result:", result);
if (result.updated && result.updated > 0) {
// Reload UAT data with fresh county info
fetch("/api/eterra/uats")
.then((res) => res.json())
.then((data: { uats?: UatEntry[] }) => {
if (data.uats && data.uats.length > 0) setUatData(data.uats);
})
.catch(() => {});
}
},
)
.catch((err) => {
console.error("[uats] County refresh failed:", err);
});
}, [session.connected, uatData]);
if (session.connected && !prevConnected.current) {
// Just connected — reload UATs after a short delay to let
// the server-side county refresh finish
const timer = setTimeout(() => {
fetch("/api/eterra/uats")
.then((res) => res.json())
.then((data: { uats?: UatEntry[] }) => {
if (data.uats && data.uats.length > 0) setUatData(data.uats);
})
.catch(() => {});
}, 5000);
return () => clearTimeout(timer);
}
prevConnected.current = session.connected;
}, [session.connected]);
/* ════════════════════════════════════════════════════════════ */
/* UAT autocomplete filter */