feat(geoportal): enrichment API + CF download + bulk enrichment

New API endpoints:
- POST /api/geoportal/enrich — enriches all parcels for a SIRUTA,
  skips already-enriched, persists in GisFeature.enrichment column
- GET /api/geoportal/cf-status?nrCad=... — checks if CF extract exists,
  returns download URL if available

Feature panel:
- No enrichment: "Enrichment" button (triggers eTerra sync for UAT)
- Has enrichment + CF available: "Descarca CF" button (direct download)
- Has enrichment + no CF: "Comanda CF" button (link to ePay tab)
- Copy button always visible
- After enrichment completes, panel auto-reloads data

Selection toolbar:
- Bulk "Enrichment" button for selected parcels (per unique SIRUTA)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-24 10:48:08 +02:00
parent 800c45916e
commit c4122cea01
4 changed files with 217 additions and 55 deletions
+61
View File
@@ -0,0 +1,61 @@
/**
* GET /api/geoportal/cf-status?nrCad=...
*
* Checks if a CF extract exists for a given cadastral number.
* Returns download info if available.
*/
import { NextResponse } from "next/server";
import { prisma } from "@/core/storage/prisma";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
export async function GET(req: Request) {
const url = new URL(req.url);
const nrCad = url.searchParams.get("nrCad")?.trim();
if (!nrCad) {
return NextResponse.json({ error: "nrCad obligatoriu" }, { status: 400 });
}
try {
// Find the latest completed CF extract for this cadastral number
const extract = await prisma.cfExtract.findFirst({
where: {
nrCadastral: nrCad,
status: "completed",
minioPath: { not: "" },
},
orderBy: { completedAt: "desc" },
select: {
id: true,
nrCadastral: true,
nrCF: true,
status: true,
minioPath: true,
documentName: true,
completedAt: true,
expiresAt: true,
},
});
if (!extract || !extract.minioPath) {
return NextResponse.json({ available: false });
}
const expired = extract.expiresAt && new Date(extract.expiresAt) < new Date();
return NextResponse.json({
available: !expired,
expired: !!expired,
id: extract.id,
nrCF: extract.nrCF,
documentName: extract.documentName,
completedAt: extract.completedAt?.toISOString(),
downloadUrl: `/api/ancpi/download?id=${extract.id}`,
});
} catch (error) {
const msg = error instanceof Error ? error.message : "Eroare";
return NextResponse.json({ error: msg }, { status: 500 });
}
}
+54
View File
@@ -0,0 +1,54 @@
/**
* POST /api/geoportal/enrich
*
* Enriches parcels for a given SIRUTA. Skips already-enriched features.
* Uses existing eTerra session or env credentials.
* Enrichment data is PERSISTED in GisFeature.enrichment column.
*
* Body: { siruta: string } or { ids: string[] } (feature UUIDs)
*/
import { NextResponse } from "next/server";
import { EterraClient } from "@/modules/parcel-sync/services/eterra-client";
import { enrichFeatures } from "@/modules/parcel-sync/services/enrich-service";
import { getSessionCredentials } from "@/modules/parcel-sync/services/session-store";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
export async function POST(req: Request) {
try {
const body = (await req.json()) as { siruta?: string };
const siruta = String(body.siruta ?? "").trim();
if (!siruta) {
return NextResponse.json({ error: "SIRUTA obligatoriu" }, { status: 400 });
}
// Get credentials: session first, then env
const session = getSessionCredentials();
const username = session?.username || process.env.ETERRA_USERNAME || "";
const password = session?.password || process.env.ETERRA_PASSWORD || "";
if (!username || !password) {
return NextResponse.json(
{ error: "Credentiale eTerra lipsa. Logheaza-te in eTerra Parcele mai intai." },
{ status: 401 }
);
}
const client = await EterraClient.create(username, password);
const result = await enrichFeatures(client, siruta);
return NextResponse.json({
status: result.status,
enrichedCount: result.enrichedCount,
buildingCrossRefs: result.buildingCrossRefs,
message: result.enrichedCount > 0
? `${result.enrichedCount} parcele imbogatite cu succes`
: "Toate parcelele au deja date de enrichment",
});
} catch (error) {
const msg = error instanceof Error ? error.message : "Eroare la enrichment";
return NextResponse.json({ error: msg }, { status: 500 });
}
}
@@ -1,10 +1,12 @@
"use client";
import { useEffect, useState } from "react";
import { X, Loader2, Sparkles, FileText, Download } from "lucide-react";
import { X, Loader2, Sparkles, FileDown, Download, ClipboardCopy } from "lucide-react";
import { Button } from "@/shared/components/ui/button";
import type { ClickedFeature, FeatureDetail, FeatureEnrichmentData } from "../types";
type CfStatus = { available: boolean; expired?: boolean; downloadUrl?: string; documentName?: string };
type FeatureInfoPanelProps = {
feature: ClickedFeature | null;
onClose: () => void;
@@ -15,9 +17,11 @@ export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) {
const [loading, setLoading] = useState(false);
const [enriching, setEnriching] = useState(false);
const [enrichMsg, setEnrichMsg] = useState("");
const [cfStatus, setCfStatus] = useState<CfStatus | null>(null);
// Fetch feature detail
useEffect(() => {
if (!feature) { setDetail(null); return; }
if (!feature) { setDetail(null); setCfStatus(null); return; }
const objectId = feature.properties.object_id ?? feature.properties.objectId;
const siruta = feature.properties.siruta;
@@ -26,10 +30,23 @@ export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) {
let cancelled = false;
setLoading(true);
setEnrichMsg("");
setCfStatus(null);
fetch(`/api/geoportal/feature?objectId=${objectId}&siruta=${siruta}&sourceLayer=${feature.sourceLayer}`)
.then((r) => r.ok ? r.json() : Promise.reject())
.then((data: { feature: FeatureDetail }) => { if (!cancelled) setDetail(data.feature); })
.then((data: { feature: FeatureDetail }) => {
if (cancelled) return;
setDetail(data.feature);
// Check CF status if we have a cadastral ref
const e = data.feature.enrichment as FeatureEnrichmentData | null;
const nrCad = e?.NR_CAD ?? data.feature.cadastralRef;
if (nrCad) {
fetch(`/api/geoportal/cf-status?nrCad=${encodeURIComponent(nrCad)}`)
.then((r) => r.ok ? r.json() : null)
.then((cf: CfStatus | null) => { if (!cancelled && cf) setCfStatus(cf); })
.catch(() => {});
}
})
.catch(() => { if (!cancelled) setDetail(null); })
.finally(() => { if (!cancelled) setLoading(false); });
@@ -45,24 +62,32 @@ export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) {
? String(feature.properties.name ?? "UAT")
: cadRef ? `Parcela ${cadRef}` : `#${feature.properties.object_id ?? "?"}`;
const hasEnrichment = !!e && !!e.NR_CAD;
const nrCf = e?.NR_CF ?? "";
const siruta = String(feature.properties.siruta ?? detail?.siruta ?? "");
const handleEnrich = async () => {
if (!detail?.siruta || !detail?.objectId) return;
if (!siruta) return;
setEnriching(true);
setEnrichMsg("");
try {
// Trigger enrichment for this specific parcel's UAT
const resp = await fetch("/api/eterra/enrich", {
const resp = await fetch("/api/geoportal/enrich", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ siruta: detail.siruta, featureId: detail.id }),
body: JSON.stringify({ siruta }),
});
const d = await resp.json();
if (resp.ok) {
setEnrichMsg("Enrichment pornit. Reincarca pagina dupa cateva secunde.");
setEnrichMsg(d.message ?? "Enrichment finalizat");
// Reload feature detail after enrichment
const objectId = feature.properties.object_id ?? feature.properties.objectId;
if (objectId) {
const r = await fetch(`/api/geoportal/feature?objectId=${objectId}&siruta=${siruta}&sourceLayer=${feature.sourceLayer}`);
if (r.ok) {
const data = await r.json();
setDetail(data.feature);
}
}
} else {
const d = await resp.json().catch(() => null);
setEnrichMsg((d && typeof d === "object" && "error" in d) ? String((d as { error: string }).error) : "Eroare la enrichment");
setEnrichMsg(d.error ?? "Eroare la enrichment");
}
} catch {
setEnrichMsg("Eroare retea");
@@ -71,6 +96,17 @@ export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) {
}
};
const handleCopy = () => {
const text = [
cadRef && `Nr. cad: ${cadRef}`,
e?.NR_CF && `CF: ${e.NR_CF}`,
(e?.SUPRAFATA_2D ?? feature.properties.area_value) && `S: ${e?.SUPRAFATA_2D ?? feature.properties.area_value} mp`,
e?.PROPRIETARI && e.PROPRIETARI !== "-" && `Prop: ${e.PROPRIETARI}`,
siruta && `SIRUTA: ${siruta}`,
].filter(Boolean).join("\n");
navigator.clipboard.writeText(text);
};
return (
<div className="bg-background/95 backdrop-blur-sm border rounded-lg shadow-lg w-72 overflow-hidden">
{/* Header */}
@@ -81,7 +117,6 @@ export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) {
</Button>
</div>
{/* Content */}
<div className="px-3 py-2 text-xs space-y-1">
{loading && (
<div className="flex items-center gap-2 text-muted-foreground py-2 justify-center">
@@ -99,22 +134,16 @@ export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) {
{!loading && !isUat && (
<>
<Row label="SIRUTA" value={feature.properties.siruta} />
<Row label="SIRUTA" value={siruta} />
<Row label="Nr. cadastral" value={e?.NR_CAD ?? cadRef} />
<Row label="Nr. CF" value={nrCf} />
<Row label="Nr. CF" value={e?.NR_CF} />
<Row label="Suprafata" value={formatArea(e?.SUPRAFATA_2D ?? feature.properties.area_value)} />
{hasEnrichment && (
<>
{e?.PROPRIETARI && e.PROPRIETARI !== "-" && (
<Row label="Proprietari" value={e.PROPRIETARI} />
)}
{e?.INTRAVILAN && e.INTRAVILAN !== "-" && (
<Row label="Intravilan" value={e.INTRAVILAN} />
)}
{e?.CATEGORIE_FOLOSINTA && e.CATEGORIE_FOLOSINTA !== "-" && (
<Row label="Categorie" value={e.CATEGORIE_FOLOSINTA} />
)}
{e?.PROPRIETARI && e.PROPRIETARI !== "-" && <Row label="Proprietari" value={e.PROPRIETARI} />}
{e?.INTRAVILAN && e.INTRAVILAN !== "-" && <Row label="Intravilan" value={e.INTRAVILAN} />}
{e?.CATEGORIE_FOLOSINTA && e.CATEGORIE_FOLOSINTA !== "-" && <Row label="Categorie" value={e.CATEGORIE_FOLOSINTA} />}
</>
)}
@@ -122,50 +151,42 @@ export function FeatureInfoPanel({ feature, onClose }: FeatureInfoPanelProps) {
<div className="flex gap-1.5 pt-2 border-t mt-2">
{!hasEnrichment && (
<Button
variant="outline"
size="sm"
className="h-7 text-xs gap-1 flex-1"
onClick={handleEnrich}
disabled={enriching}
title="Obtine date detaliate (proprietari, CF, categorie folosinta) de la eTerra"
variant="outline" size="sm" className="h-7 text-xs gap-1 flex-1"
onClick={handleEnrich} disabled={enriching}
title="Obtine date detaliate (proprietari, CF, categorie) de la eTerra. Datele se salveaza permanent in baza de date."
>
{enriching ? <Loader2 className="h-3 w-3 animate-spin" /> : <Sparkles className="h-3 w-3" />}
Enrichment
</Button>
)}
{hasEnrichment && nrCf && (
{cfStatus?.available && cfStatus.downloadUrl && (
<Button
variant="outline"
size="sm"
className="h-7 text-xs gap-1 flex-1"
onClick={() => {
window.open(`/parcel-sync?tab=extrase&search=${encodeURIComponent(cadRef)}`, "_blank");
}}
title="Comanda sau descarca extras CF de la ANCPI ePay"
variant="outline" size="sm" className="h-7 text-xs gap-1 flex-1"
onClick={() => window.open(cfStatus.downloadUrl, "_blank")}
title={`Descarca extras CF: ${cfStatus.documentName ?? "PDF"}`}
>
<FileText className="h-3 w-3" />
Extras CF
<FileDown className="h-3 w-3" />
Descarca CF
</Button>
)}
{hasEnrichment && !cfStatus?.available && (
<Button
variant="outline" size="sm" className="h-7 text-xs gap-1 flex-1"
onClick={() => window.open(`/parcel-sync?tab=extrase&search=${encodeURIComponent(cadRef)}`, "_blank")}
title="Comanda extras CF de la ANCPI ePay"
>
<Download className="h-3 w-3" />
Comanda CF
</Button>
)}
<Button
variant="outline"
size="sm"
className="h-7 text-xs gap-1 flex-1"
onClick={() => {
const text = [
cadRef && `Nr. cad: ${cadRef}`,
nrCf && `CF: ${nrCf}`,
e?.SUPRAFATA_2D && `S: ${e.SUPRAFATA_2D} mp`,
e?.PROPRIETARI && e.PROPRIETARI !== "-" && `Prop: ${e.PROPRIETARI}`,
].filter(Boolean).join("\n");
navigator.clipboard.writeText(text);
}}
title="Copiaza informatiile in clipboard"
variant="outline" size="sm" className="h-7 text-xs gap-1"
onClick={handleCopy} title="Copiaza informatiile in clipboard"
>
<Download className="h-3 w-3" />
Copiaza
<ClipboardCopy className="h-3 w-3" />
</Button>
</div>
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
import { Download, Trash2, MousePointerClick, Square, PenTool, Loader2 } from "lucide-react";
import { Download, Trash2, MousePointerClick, Square, PenTool, Loader2, Sparkles } from "lucide-react";
import { Button } from "@/shared/components/ui/button";
import {
DropdownMenu,
@@ -42,6 +42,7 @@ export function SelectionToolbar({
className,
}: SelectionToolbarProps) {
const [exporting, setExporting] = useState(false);
const [enriching, setEnriching] = useState(false);
const active = selectionMode !== "off";
const handleExport = async (format: ExportFormat) => {
@@ -136,6 +137,31 @@ export function SelectionToolbar({
</DropdownMenuContent>
</DropdownMenu>
<Button
variant="ghost" size="sm" className="h-7 px-2 text-xs gap-1"
disabled={enriching}
title="Enrichment pentru parcelele selectate (obtine proprietari, CF, categorie). Datele se salveaza permanent."
onClick={async () => {
// Get unique SIRUTAs from selected features
const sirutas = [...new Set(selectedFeatures.map((f) => String(f.properties.siruta ?? "")).filter(Boolean))];
if (sirutas.length === 0) return;
setEnriching(true);
try {
for (const siruta of sirutas) {
await fetch("/api/geoportal/enrich", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ siruta }),
});
}
} catch { /* noop */ }
setEnriching(false);
}}
>
{enriching ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Sparkles className="h-3.5 w-3.5" />}
Enrichment
</Button>
<Button variant="ghost" size="sm" className="h-7 w-7 p-0" onClick={onClearSelection} title="Sterge selectia">
<Trash2 className="h-3.5 w-3.5" />
</Button>