From 8fa89a767534d1843bc458230fdf462d66e1077f Mon Sep 17 00:00:00 2001 From: AI Assistant Date: Sun, 22 Mar 2026 21:34:29 +0200 Subject: [PATCH] fix(parcel-sync): restore SIRUTA in dropdown, add county debug output - Restore SIRUTA code display in parentheses next to UAT name - PATCH response now includes debug samples (sampleUat keys, county raw data) visible in browser console for diagnosing matching issues - POST endpoint now supports resync (upsert mode, safe to call again) - Client logs full PATCH result to browser console for debugging Co-Authored-By: Claude Opus 4.6 (1M context) --- src/app/api/eterra/uats/route.ts | 44 +++++++++++++----- .../components/parcel-sync-module.tsx | 46 ++++++++++++------- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/app/api/eterra/uats/route.ts b/src/app/api/eterra/uats/route.ts index 16af464..bceb266 100644 --- a/src/app/api/eterra/uats/route.ts +++ b/src/app/api/eterra/uats/route.ts @@ -165,7 +165,9 @@ export async function GET() { /* ------------------------------------------------------------------ */ /* POST /api/eterra/uats */ /* */ -/* Seed DB from static uat.json. */ +/* Seed or resync DB from static uat.json. */ +/* Uses upsert so it's safe to call repeatedly — new UATs are added, */ +/* existing names are updated, county/workspacePk are preserved. */ /* eTerra nomenPk ≠ SIRUTA, so we cannot use the nomenclature API */ /* for populating UAT data. uat.json has correct SIRUTA codes. */ /* Workspace (county) PKs are resolved lazily via ArcGIS layer query */ @@ -174,15 +176,6 @@ export async function GET() { export async function POST() { try { - // Check if DB already has data - const dbCount = await prisma.gisUat.count(); - if (dbCount > 0) { - return NextResponse.json({ - synced: false, - reason: "already-seeded", - total: dbCount, - }); - } // Read uat.json from public/ directory let rawUats: Array<{ siruta: string; name: string }>; @@ -368,6 +361,11 @@ export async function PATCH() { let nameMatches = 0; const matchedSirutas = new Set(); let loggedSample = false; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let sampleCounty: any = null; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let sampleUat: any = null; + let totalEterraUats = 0; for (const [countyPk, countyName] of countyMap) { if (matchedSirutas.size >= needsCounty.length) break; @@ -376,12 +374,20 @@ export async function PATCH() { const rawUats = await client.fetchAdminUnitsByCounty(countyPk); const uats = unwrapArray(rawUats); + totalEterraUats += uats.length; + // Log first county's first UAT for debugging if (!loggedSample && uats.length > 0) { + sampleUat = uats[0]; + sampleCounty = { pk: countyPk, name: countyName, uatCount: uats.length }; console.log( - `[uats-patch] Sample UAT from ${countyName}:`, + `[uats-patch] Sample UAT from ${countyName} (${uats.length} UATs):`, JSON.stringify(uats[0]).slice(0, 500), ); + console.log( + `[uats-patch] Sample UAT keys:`, + Object.keys(uats[0] ?? {}), + ); loggedSample = true; } @@ -444,7 +450,23 @@ export async function PATCH() { codeMatches, nameMatches, totalCounties: countyMap.size, + totalEterraUats, unmatched, + // Include debug samples so we can see what eTerra returns + debug: { + sampleCounty, + sampleUatKeys: sampleUat ? Object.keys(sampleUat) : null, + sampleUat: sampleUat + ? JSON.parse(JSON.stringify(sampleUat).slice(0, 500)) + : null, + sampleCountyRaw: counties[0] + ? { + keys: Object.keys(counties[0]), + nomenPk: counties[0].nomenPk, + name: counties[0].name, + } + : null, + }, }); } catch (error) { const message = error instanceof Error ? error.message : "Eroare server"; diff --git a/src/modules/parcel-sync/components/parcel-sync-module.tsx b/src/modules/parcel-sync/components/parcel-sync-module.tsx index 0415e34..1c5f021 100644 --- a/src/modules/parcel-sync/components/parcel-sync-module.tsx +++ b/src/modules/parcel-sync/components/parcel-sync-module.tsx @@ -572,18 +572,32 @@ export function ParcelSyncModule() { fetch("/api/eterra/uats", { method: "PATCH" }) .then((res) => res.json()) - .then((result: { updated?: number; error?: string }) => { - 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(() => {}); + .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]); /* ════════════════════════════════════════════════════════════ */ @@ -1771,8 +1785,11 @@ export function ParcelSyncModule() { setSearchResults([]); }} > - + {item.name} + + ({item.siruta}) + {item.county && ( –{" "} @@ -1787,9 +1804,6 @@ export function ParcelSyncModule() { )} - - {item.siruta} - ))}