fix: dynamic workspaceId for no-geometry scan (was hardcoded 65)
- resolveWorkspacePk chain: explicit param -> GisUat DB -> ArcGIS layer query - UI passes workspacePk from UAT selection to scan API - Fixes: FELEACU (Cluj, workspace!=65) returning 0 immovables - Better messaging: shows X total, Y with geometry, Z without - Shows warning when 0 immovables found (workspace resolution failed)
This commit is contained in:
@@ -22,6 +22,65 @@ import { EterraClient } from "./eterra-client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Workspace resolution (county → eTerra workspace PK) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Resolve the eTerra workspace PK for a SIRUTA.
|
||||
* Chain: explicit param → GisUat DB row → ArcGIS layer query → null.
|
||||
*/
|
||||
async function resolveWorkspacePk(
|
||||
client: EterraClient,
|
||||
siruta: string,
|
||||
explicitPk?: number | null,
|
||||
): Promise<number | null> {
|
||||
// 1. Explicit param
|
||||
if (explicitPk && Number.isFinite(explicitPk) && explicitPk > 0) {
|
||||
return explicitPk;
|
||||
}
|
||||
|
||||
// 2. DB lookup
|
||||
try {
|
||||
const row = await prisma.gisUat.findUnique({
|
||||
where: { siruta },
|
||||
select: { workspacePk: true },
|
||||
});
|
||||
if (row?.workspacePk && row.workspacePk > 0) return row.workspacePk;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
|
||||
// 3. ArcGIS layer query — fetch 1 feature from TERENURI_ACTIVE for this siruta
|
||||
try {
|
||||
const features = await client.listLayer(
|
||||
{
|
||||
id: "TERENURI_ACTIVE",
|
||||
name: "TERENURI_ACTIVE",
|
||||
endpoint: "aut",
|
||||
whereTemplate: "{{adminField}}={{siruta}} AND IS_ACTIVE=1",
|
||||
},
|
||||
siruta,
|
||||
{ limit: 1, outFields: "WORKSPACE_ID" },
|
||||
);
|
||||
const wsId = features?.[0]?.attributes?.WORKSPACE_ID;
|
||||
if (wsId != null) {
|
||||
const num = Number(wsId);
|
||||
if (Number.isFinite(num) && num > 0) {
|
||||
// Persist for future lookups
|
||||
prisma.gisUat
|
||||
.update({ where: { siruta }, data: { workspacePk: num } })
|
||||
.catch(() => {});
|
||||
return num;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const normalizeId = (value: unknown) => {
|
||||
if (value === null || value === undefined) return "";
|
||||
const text = String(value).trim();
|
||||
@@ -43,6 +102,8 @@ export type NoGeomScanResult = {
|
||||
paperCadNo?: string;
|
||||
paperCfNo?: string;
|
||||
}>;
|
||||
/** Error message if workspace couldn't be resolved */
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export type NoGeomSyncResult = {
|
||||
@@ -64,12 +125,26 @@ export async function scanNoGeometryParcels(
|
||||
siruta: string,
|
||||
options?: {
|
||||
onProgress?: (page: number, totalPages: number) => void;
|
||||
workspacePk?: number | null;
|
||||
},
|
||||
): Promise<NoGeomScanResult> {
|
||||
// 0. Resolve workspace
|
||||
const wsPk = await resolveWorkspacePk(client, siruta, options?.workspacePk);
|
||||
if (!wsPk) {
|
||||
return {
|
||||
totalImmovables: 0,
|
||||
totalInDb: 0,
|
||||
noGeomCount: 0,
|
||||
samples: [],
|
||||
error: `Nu s-a putut determina workspace-ul (județul) pentru SIRUTA ${siruta}`,
|
||||
};
|
||||
}
|
||||
|
||||
// 1. Fetch all immovables from eTerra
|
||||
const allImmovables = await fetchAllImmovables(
|
||||
client,
|
||||
siruta,
|
||||
wsPk,
|
||||
options?.onProgress,
|
||||
);
|
||||
|
||||
@@ -131,12 +206,25 @@ export async function syncNoGeometryParcels(
|
||||
siruta: string,
|
||||
options?: {
|
||||
onProgress?: (done: number, total: number, phase: string) => void;
|
||||
workspacePk?: number | null;
|
||||
},
|
||||
): Promise<NoGeomSyncResult> {
|
||||
try {
|
||||
// 0. Resolve workspace
|
||||
const wsPk = await resolveWorkspacePk(client, siruta, options?.workspacePk);
|
||||
if (!wsPk) {
|
||||
return {
|
||||
imported: 0,
|
||||
skipped: 0,
|
||||
errors: 0,
|
||||
status: "error",
|
||||
error: `Nu s-a putut determina workspace-ul pentru SIRUTA ${siruta}`,
|
||||
};
|
||||
}
|
||||
|
||||
// 1. Fetch all immovables
|
||||
options?.onProgress?.(0, 1, "Descărcare listă imobile (fără geometrie)");
|
||||
const allImmovables = await fetchAllImmovables(client, siruta);
|
||||
const allImmovables = await fetchAllImmovables(client, siruta, wsPk);
|
||||
|
||||
// 2. Get existing features from DB
|
||||
const existingFeatures = await prisma.gisFeature.findMany({
|
||||
@@ -251,6 +339,7 @@ export async function syncNoGeometryParcels(
|
||||
async function fetchAllImmovables(
|
||||
client: EterraClient,
|
||||
siruta: string,
|
||||
workspaceId: number,
|
||||
onProgress?: (page: number, totalPages: number) => void,
|
||||
): Promise<any[]> {
|
||||
const all: any[] = [];
|
||||
@@ -258,10 +347,6 @@ async function fetchAllImmovables(
|
||||
let totalPages = 1;
|
||||
let includeInscrisCF = true;
|
||||
|
||||
// The workspace ID for eTerra admin unit queries.
|
||||
// Default to 65 (standard workspace); the eTerra API resolves by adminUnit.
|
||||
const workspaceId = 65;
|
||||
|
||||
while (page < totalPages) {
|
||||
const response = await client.fetchImmovableListByAdminUnit(
|
||||
workspaceId,
|
||||
|
||||
Reference in New Issue
Block a user