feat(parcel-sync): full GPKG export workflow with UAT autocomplete, hero buttons, layer catalog

- Fix login button (return success instead of ok)
- Add UAT autocomplete with NFD-normalized search (3186 entries)
- Add export-bundle API: base mode (terenuri+cladiri) + magic mode (enriched parcels)
- Add export-layer-gpkg API: individual layer GPKG download
- Add gpkg-export service: ogr2ogr with GeoJSON fallback
- Add reproject service: EPSG:3844 projection support
- Add magic-mode methods to eterra-client (immApps, folosinte, immovableList, docs, parcelDetails)
- Rewrite UI: 3-tab layout (Export/Catalog/Search), progress tracking, phase trail
This commit is contained in:
AI Assistant
2026-03-06 06:53:49 +02:00
parent 7cdea66fa2
commit 09a24233bb
10 changed files with 15102 additions and 566 deletions
+881
View File
@@ -0,0 +1,881 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import JSZip from "jszip";
import { EterraClient } from "@/modules/parcel-sync/services/eterra-client";
import { findLayerById } from "@/modules/parcel-sync/services/eterra-layers";
import { esriToGeojson } from "@/modules/parcel-sync/services/esri-geojson";
import { getEpsg3844Wkt } from "@/modules/parcel-sync/services/reproject";
import { buildGpkg } from "@/modules/parcel-sync/services/gpkg-export";
import {
clearProgress,
setProgress,
} from "@/modules/parcel-sync/services/progress-store";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
type ExportBundleRequest = {
username?: string;
password?: string;
siruta?: string | number;
jobId?: string;
mode?: "base" | "magic";
};
const validate = (body: ExportBundleRequest) => {
const username = String(body.username ?? "").trim();
const password = String(body.password ?? "").trim();
const siruta = String(body.siruta ?? "").trim();
const jobId = body.jobId ? String(body.jobId).trim() : undefined;
const mode = body.mode === "magic" ? "magic" : "base";
if (!username) throw new Error("Email is required");
if (!password) throw new Error("Password is required");
if (!/^\d+$/.test(siruta)) throw new Error("SIRUTA must be numeric");
return { username, password, siruta, jobId, mode };
};
const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
const scheduleClear = (jobId?: string) => {
if (!jobId) return;
setTimeout(() => clearProgress(jobId), 60_000);
};
const isRetryable = (error: unknown) => {
const err = error as { response?: { status?: number }; code?: string };
const status = err?.response?.status ?? 0;
if ([429, 500, 502, 503, 504].includes(status)) return true;
return err?.code === "ECONNRESET" || err?.code === "ETIMEDOUT";
};
const formatNumber = (value: number) =>
Number.isFinite(value) ? value.toFixed(2).replace(/\.00$/, "") : "";
const normalizeId = (value: unknown) => {
if (value === null || value === undefined) return "";
const text = String(value).trim();
if (!text) return "";
return text.replace(/\.0$/, "");
};
const normalizeCadRef = (value: unknown) =>
normalizeId(value).replace(/\s+/g, "").toUpperCase();
const baseCadRef = (value: unknown) => {
const ref = normalizeCadRef(value);
if (!ref) return "";
return ref.includes("-") ? ref.split("-")[0]! : ref;
};
const makeWorkspaceKey = (workspaceId: unknown, immovableId: unknown) => {
const ws = normalizeId(workspaceId);
const im = normalizeId(immovableId);
if (!ws || !im) return "";
return `${ws}:${im}`;
};
export async function POST(req: Request) {
let jobId: string | undefined;
let message: string | undefined;
let phase = "Initializare";
let note: string | undefined;
let status: "running" | "done" | "error" = "running";
let downloaded = 0;
let total: number | undefined;
let completedWeight = 0;
let currentWeight = 0;
let phaseTotal: number | undefined;
let phaseCurrent: number | undefined;
const pushProgress = () => {
if (!jobId) return;
setProgress({
jobId,
downloaded,
total,
status,
phase,
note,
message,
phaseCurrent,
phaseTotal,
});
};
const updateOverall = (fraction = 0) => {
const overall = completedWeight + currentWeight * fraction;
const clipped = Math.min(100, Math.max(0, overall));
downloaded = Number(clipped.toFixed(1));
total = 100;
pushProgress();
};
const setPhaseState = (
next: string,
weight: number,
nextTotal?: number,
) => {
phase = next;
currentWeight = weight;
phaseTotal = nextTotal;
phaseCurrent = nextTotal ? 0 : undefined;
note = undefined;
updateOverall(0);
};
const updatePhaseProgress = (value: number, nextTotal?: number) => {
if (typeof nextTotal === "number") phaseTotal = nextTotal;
if (phaseTotal && phaseTotal > 0) {
phaseCurrent = value;
updateOverall(Math.min(1, value / phaseTotal));
} else {
phaseCurrent = undefined;
updateOverall(0);
}
};
const finishPhase = () => {
completedWeight += currentWeight;
currentWeight = 0;
phaseTotal = undefined;
phaseCurrent = undefined;
note = undefined;
updateOverall(0);
};
const withHeartbeat = async <T,>(task: () => Promise<T>) => {
let tick = 0.1;
updatePhaseProgress(tick, 1);
const interval = setInterval(() => {
tick = Math.min(0.9, tick + 0.05);
updatePhaseProgress(tick, 1);
}, 1200);
try {
return await task();
} finally {
clearInterval(interval);
}
};
try {
const body = (await req.json()) as ExportBundleRequest;
const validated = validate(body);
jobId = validated.jobId;
pushProgress();
const terenuriLayer = findLayerById("TERENURI_ACTIVE");
const cladiriLayer = findLayerById("CLADIRI_ACTIVE");
if (!terenuriLayer || !cladiriLayer)
throw new Error("Missing layer configuration");
const weights =
validated.mode === "magic"
? {
auth: 3,
count: 2,
terenuri: 23,
cladiri: 13,
detalii: 34,
gpkgT: 8,
gpkgC: 7,
gpkgM: 5,
zip: 5,
}
: {
auth: 3,
count: 2,
terenuri: 40,
cladiri: 22,
detalii: 0,
gpkgT: 10,
gpkgC: 10,
gpkgM: 0,
zip: 13,
};
/* Auth */
setPhaseState("Autentificare", weights.auth, 1);
const client = await EterraClient.create(
validated.username,
validated.password,
{ timeoutMs: 120_000 },
);
updatePhaseProgress(1, 1);
finishPhase();
/* Count */
const safeCount = async (layerId: "terenuri" | "cladiri") => {
try {
return await client.countLayer(
layerId === "terenuri" ? terenuriLayer : cladiriLayer,
validated.siruta,
);
} catch (error) {
const msg =
error instanceof Error ? error.message : "Unexpected server error";
if (msg.toLowerCase().includes("count unavailable")) return undefined;
throw error;
}
};
setPhaseState("Numarare", weights.count, 2);
const terenuriCount = await safeCount("terenuri");
updatePhaseProgress(1, 2);
const cladiriCount = await safeCount("cladiri");
updatePhaseProgress(2, 2);
finishPhase();
const calcPageSize = (cnt?: number) => {
if (!cnt || cnt <= 0) return 1000;
if (cnt <= 200) return Math.max(50, Math.ceil(cnt / 2));
return Math.min(1000, Math.max(200, Math.ceil(cnt / 8)));
};
/* Download terenuri */
setPhaseState("Descarcare terenuri", weights.terenuri, terenuriCount);
const terenuriFeatures = await client.fetchAllLayer(
terenuriLayer,
validated.siruta,
{
total: terenuriCount,
pageSize: calcPageSize(terenuriCount),
delayMs: 250,
onProgress: (count, totalCount) =>
updatePhaseProgress(count, totalCount),
},
);
finishPhase();
/* Download cladiri */
setPhaseState("Descarcare cladiri", weights.cladiri, cladiriCount);
const cladiriFeatures = await client.fetchAllLayer(
cladiriLayer,
validated.siruta,
{
total: cladiriCount,
pageSize: calcPageSize(cladiriCount),
delayMs: 250,
onProgress: (count, totalCount) =>
updatePhaseProgress(count, totalCount),
},
);
finishPhase();
const terenuriGeo = esriToGeojson(terenuriFeatures);
const cladiriGeo = esriToGeojson(cladiriFeatures);
const srsWkt = getEpsg3844Wkt();
const terenuriFields = await client.getLayerFieldNames(terenuriLayer);
const cladiriFields = await client.getLayerFieldNames(cladiriLayer);
let terenuriGpkg: Buffer | null = null;
let cladiriGpkg: Buffer | null = null;
let magicGpkg: Buffer | null = null;
let csvContent: string | null = null;
let hasBuildingCount = 0;
let legalBuildingCount = 0;
if (validated.mode === "magic") {
/* ── Magic mode: enrich parcels ─────────────────────────── */
setPhaseState("Detalii parcele", weights.detalii, terenuriCount);
const immAppsCache = new Map<string, any[]>();
const folCache = new Map<string, any[]>();
let lastRequest = 0;
const minInterval = 250;
const throttled = async <T,>(fn: () => Promise<T>) => {
let attempt = 0;
while (true) {
const now = Date.now();
const wait = Math.max(0, lastRequest + minInterval - now);
if (wait > 0) {
note = `Throttling ${Math.ceil(wait)}ms`;
pushProgress();
await sleep(wait);
}
try {
const result = await fn();
lastRequest = Date.now();
note = undefined;
pushProgress();
return result;
} catch (error) {
if (!isRetryable(error) || attempt >= 2) throw error;
attempt += 1;
const backoff = Math.min(5000, 1000 * attempt);
note = `Backoff ${backoff}ms (retry ${attempt})`;
pushProgress();
await sleep(backoff);
}
}
};
const pickApplication = (entries: any[], applicationId?: number) => {
if (!entries.length) return null;
if (applicationId) {
const match = entries.find(
(entry: any) => entry?.applicationId === applicationId,
);
if (match) return match;
}
return (
entries
.filter((entry: any) => entry?.dataCerere)
.sort(
(a: any, b: any) => (b.dataCerere ?? 0) - (a.dataCerere ?? 0),
)[0] ?? entries[0]
);
};
const normalizeIntravilan = (values: string[]) => {
const normalized = values
.map((v) => String(v ?? "").trim().toLowerCase())
.filter(Boolean);
const unique = new Set(normalized);
if (!unique.size) return "-";
if (unique.size === 1)
return unique.has("da") ? "Da" : unique.has("nu") ? "Nu" : "Mixt";
return "Mixt";
};
const formatCategories = (entries: any[]) => {
const map = new Map<string, number>();
for (const entry of entries) {
const key = String(entry?.categorieFolosinta ?? "").trim();
if (!key) continue;
const area = Number(entry?.suprafata ?? 0);
map.set(
key,
(map.get(key) ?? 0) + (Number.isFinite(area) ? area : 0),
);
}
return Array.from(map.entries())
.map(([k, a]) => `${k}:${formatNumber(a)}`)
.join("; ");
};
const formatAddress = (item?: any) => {
const address = item?.immovableAddresses?.[0]?.address ?? null;
if (!address) return "-";
const parts: string[] = [];
if (address.addressDescription)
parts.push(address.addressDescription);
if (address.street) parts.push(`Str. ${address.street}`);
if (address.buildingNo) parts.push(`Nr. ${address.buildingNo}`);
if (address.locality?.name) parts.push(address.locality.name);
return parts.length ? parts.join(", ") : "-";
};
/* Building cross-ref map */
const buildingMap = new Map<
string,
{ has: boolean; legal: boolean }
>();
for (const feature of cladiriFeatures) {
const attrs = feature.attributes ?? {};
const immovableId =
attrs.IMMOVABLE_ID ?? attrs.IMOVABLE_ID ?? null;
const workspaceId = attrs.WORKSPACE_ID ?? null;
const baseRef = baseCadRef(
attrs.NATIONAL_CADASTRAL_REFERENCE ?? "",
);
const isLegal =
Number(attrs.IS_LEGAL ?? 0) === 1 ||
String(attrs.IS_LEGAL ?? "").toLowerCase() === "true";
const add = (key: string) => {
if (!key) return;
const existing = buildingMap.get(key) ?? {
has: false,
legal: false,
};
existing.has = true;
if (isLegal) existing.legal = true;
buildingMap.set(key, existing);
};
const immKey = normalizeId(immovableId);
const wKey = makeWorkspaceKey(workspaceId, immovableId);
if (immKey) add(immKey);
if (wKey) add(wKey);
if (baseRef) add(baseRef);
}
/* Fetch immovable list */
const immovableListById = new Map<string, any>();
const immovableListByCad = new Map<string, any>();
const docByImmovable = new Map<string, any>();
const ownersByLandbook = new Map<string, Set<string>>();
const addOwner = (landbook: string, name: string) => {
if (!landbook || !name) return;
const existing =
ownersByLandbook.get(landbook) ?? new Set<string>();
existing.add(name);
ownersByLandbook.set(landbook, existing);
};
let listPage = 0;
let listTotalPages = 1;
let includeInscrisCF = true;
while (listPage < listTotalPages) {
const listResponse = await throttled(() =>
client.fetchImmovableListByAdminUnit(
65,
validated.siruta,
listPage,
200,
includeInscrisCF,
),
);
if (
listPage === 0 &&
!(listResponse?.content ?? []).length &&
includeInscrisCF
) {
includeInscrisCF = false;
listPage = 0;
listTotalPages = 1;
continue;
}
listTotalPages =
typeof listResponse?.totalPages === "number"
? listResponse.totalPages
: listTotalPages;
(listResponse?.content ?? []).forEach((item: any) => {
const idKey = normalizeId(item?.immovablePk);
if (idKey) immovableListById.set(idKey, item);
const cadKey = normalizeCadRef(
item?.identifierDetails ?? "",
);
if (cadKey) immovableListByCad.set(cadKey, item);
});
listPage += 1;
}
/* Fetch documentation data */
const immovableIds = Array.from(immovableListById.keys());
const docBatchSize = 50;
for (let i = 0; i < immovableIds.length; i += docBatchSize) {
const batch = immovableIds.slice(i, i + docBatchSize);
const docResponse = await throttled(() =>
client.fetchDocumentationData(65, batch),
);
(docResponse?.immovables ?? []).forEach((item: any) => {
const idKey = normalizeId(item?.immovablePk);
if (idKey) docByImmovable.set(idKey, item);
});
(docResponse?.partTwoRegs ?? []).forEach((item: any) => {
if (
String(item?.nodeType ?? "").toUpperCase() === "P" &&
item?.landbookIE
) {
const name = String(item?.nodeName ?? "").trim();
if (name) addOwner(String(item.landbookIE), name);
}
});
}
/* Build CSV + detail map */
const csvRows: string[] = [];
const headers = [
"OBJECTID",
"IMMOVABLE_ID",
"APPLICATION_ID",
"NATIONAL_CADASTRAL_REFERENCE",
"NR_CAD",
"AREA_VALUE",
"NR_CF",
"NR_CF_VECHI",
"NR_TOPO",
"ADRESA",
"PROPRIETARI",
"SUPRAFATA_2D",
"SUPRAFATA_R",
"SOLICITANT",
"INTRAVILAN",
"CATEGORIE_FOLOSINTA",
"HAS_BUILDING",
"BUILD_LEGAL",
];
csvRows.push(headers.join(","));
const detailsByObjectId = new Map<
string,
Record<string, unknown>
>();
for (let index = 0; index < terenuriFeatures.length; index += 1) {
const feature = terenuriFeatures[index]!;
const attrs = feature.attributes ?? {};
const objectId = attrs.OBJECTID ?? "";
const immovableId = attrs.IMMOVABLE_ID ?? "";
const workspaceId = attrs.WORKSPACE_ID ?? "";
const applicationId = (attrs.APPLICATION_ID as number) ?? null;
let solicitant = "-";
let intravilan = "-";
let categorie = "-";
let proprietari = "-";
let nrCF = "-";
let nrCFVechi = "-";
let nrTopo = "-";
let addressText = "-";
if (immovableId && workspaceId) {
const appKey = `${workspaceId}:${immovableId}`;
let apps = immAppsCache.get(appKey);
if (!apps) {
apps = await throttled(() =>
client.fetchImmAppsByImmovable(
immovableId as string | number,
workspaceId as string | number,
),
);
immAppsCache.set(appKey, apps);
}
const chosen = pickApplication(
apps,
Number(applicationId ?? 0),
);
const appId =
chosen?.applicationId ??
(applicationId ? Number(applicationId) : null);
solicitant =
chosen?.solicitant ?? chosen?.deponent ?? solicitant;
if (appId) {
const folKey = `${workspaceId}:${immovableId}:${appId}`;
let fol = folCache.get(folKey);
if (!fol) {
fol = await throttled(() =>
client.fetchParcelFolosinte(
workspaceId as string | number,
immovableId as string | number,
appId,
),
);
folCache.set(folKey, fol);
}
intravilan = normalizeIntravilan(
fol.map((item: any) => item?.intravilan ?? ""),
);
categorie = formatCategories(fol);
}
}
const cadRefRaw = (attrs.NATIONAL_CADASTRAL_REFERENCE ??
"") as string;
const cadRef = normalizeCadRef(cadRefRaw);
const immKey = normalizeId(immovableId);
const listItem =
(immKey ? immovableListById.get(immKey) : undefined) ??
(cadRef ? immovableListByCad.get(cadRef) : undefined);
const docKey = listItem?.immovablePk
? normalizeId(listItem.immovablePk)
: "";
const docItem = docKey
? docByImmovable.get(docKey)
: undefined;
const landbookIE = docItem?.landbookIE ?? "";
const owners =
landbookIE && ownersByLandbook.get(String(landbookIE))
? Array.from(
ownersByLandbook.get(String(landbookIE)) ?? [],
)
: [];
const ownersByCad =
cadRefRaw && ownersByLandbook.get(String(cadRefRaw))
? Array.from(
ownersByLandbook.get(String(cadRefRaw)) ?? [],
)
: [];
proprietari =
Array.from(new Set([...owners, ...ownersByCad])).join(
"; ",
) || proprietari;
nrCF =
docItem?.landbookIE ||
listItem?.paperLbNo ||
listItem?.paperCadNo ||
nrCF;
const nrCFVechiRaw =
listItem?.paperLbNo || listItem?.paperCadNo || "";
nrCFVechi =
docItem?.landbookIE && nrCFVechiRaw !== nrCF
? nrCFVechiRaw
: nrCFVechi;
nrTopo =
listItem?.topNo ||
docItem?.topNo ||
listItem?.paperCadNo ||
nrTopo;
addressText = listItem
? formatAddress(listItem)
: addressText;
const parcelRef = baseCadRef(cadRefRaw);
const wKey = makeWorkspaceKey(workspaceId, immovableId);
const build =
(immKey ? buildingMap.get(immKey) : undefined) ??
(wKey ? buildingMap.get(wKey) : undefined) ??
(parcelRef ? buildingMap.get(parcelRef) : undefined) ?? {
has: false,
legal: false,
};
const hasBuilding = build.has ? 1 : 0;
const buildLegal = build.has ? (build.legal ? 1 : 0) : 0;
if (hasBuilding) hasBuildingCount += 1;
if (buildLegal === 1) legalBuildingCount += 1;
const areaValue =
typeof attrs.AREA_VALUE === "number" ? attrs.AREA_VALUE : null;
const detailRecord = {
NR_CAD: cadRefRaw,
NR_CF: nrCF,
NR_CF_VECHI: nrCFVechi,
NR_TOPO: nrTopo,
ADRESA: addressText,
PROPRIETARI: proprietari,
SUPRAFATA_2D:
areaValue !== null ? Number(areaValue.toFixed(2)) : "",
SUPRAFATA_R:
areaValue !== null ? Math.round(areaValue) : "",
SOLICITANT: solicitant,
INTRAVILAN: intravilan,
CATEGORIE_FOLOSINTA: categorie,
HAS_BUILDING: hasBuilding,
BUILD_LEGAL: buildLegal,
};
detailsByObjectId.set(String(objectId), detailRecord);
const row = [
objectId,
immovableId,
applicationId ?? "",
attrs.NATIONAL_CADASTRAL_REFERENCE ?? "",
cadRefRaw,
attrs.AREA_VALUE ?? "",
nrCF,
nrCFVechi,
nrTopo,
`"${String(addressText).replace(/"/g, '""')}"`,
`"${String(proprietari).replace(/"/g, '""')}"`,
areaValue !== null ? areaValue.toFixed(2) : "",
areaValue !== null ? Math.round(areaValue) : "",
`"${String(solicitant).replace(/"/g, '""')}"`,
intravilan,
`"${String(categorie).replace(/"/g, '""')}"`,
hasBuilding,
buildLegal,
];
csvRows.push(row.join(","));
if (index % 10 === 0)
updatePhaseProgress(index + 1, terenuriCount);
}
updatePhaseProgress(terenuriFeatures.length, terenuriCount);
finishPhase();
csvContent = csvRows.join("\n");
/* GPKG terenuri */
setPhaseState("GPKG terenuri", weights.gpkgT, 1);
terenuriGpkg = await withHeartbeat(() =>
buildGpkg({
srsId: 3844,
srsWkt,
layers: [
{
name: "TERENURI_ACTIVE",
fields: terenuriFields,
features: terenuriGeo.features,
},
],
}),
);
updatePhaseProgress(1, 1);
finishPhase();
/* GPKG cladiri */
setPhaseState("GPKG cladiri", weights.gpkgC, 1);
cladiriGpkg = await withHeartbeat(() =>
buildGpkg({
srsId: 3844,
srsWkt,
layers: [
{
name: "CLADIRI_ACTIVE",
fields: cladiriFields,
features: cladiriGeo.features,
},
],
}),
);
updatePhaseProgress(1, 1);
finishPhase();
/* GPKG magic */
setPhaseState("GPKG magic", weights.gpkgM, 1);
const magicFields = Array.from(
new Set([
...terenuriFields,
"NR_CAD",
"NR_CF",
"NR_CF_VECHI",
"NR_TOPO",
"ADRESA",
"PROPRIETARI",
"SUPRAFATA_2D",
"SUPRAFATA_R",
"SOLICITANT",
"INTRAVILAN",
"CATEGORIE_FOLOSINTA",
"HAS_BUILDING",
"BUILD_LEGAL",
]),
);
const magicFeatures = terenuriGeo.features.map((feature) => {
const objectId = String(
feature.properties?.OBJECTID ?? "",
);
const extra = detailsByObjectId.get(objectId) ?? {};
return {
...feature,
properties: { ...feature.properties, ...extra },
};
});
magicGpkg = await withHeartbeat(() =>
buildGpkg({
srsId: 3844,
srsWkt,
layers: [
{
name: "TERENURI_MAGIC",
fields: magicFields,
features: magicFeatures,
},
],
}),
);
updatePhaseProgress(1, 1);
finishPhase();
} else {
/* ── Base mode ──────────────────────────────────────────── */
setPhaseState("GPKG terenuri", weights.gpkgT, 1);
terenuriGpkg = await withHeartbeat(() =>
buildGpkg({
srsId: 3844,
srsWkt,
layers: [
{
name: "TERENURI_ACTIVE",
fields: terenuriFields,
features: terenuriGeo.features,
},
],
}),
);
updatePhaseProgress(1, 1);
finishPhase();
setPhaseState("GPKG cladiri", weights.gpkgC, 1);
cladiriGpkg = await withHeartbeat(() =>
buildGpkg({
srsId: 3844,
srsWkt,
layers: [
{
name: "CLADIRI_ACTIVE",
fields: cladiriFields,
features: cladiriGeo.features,
},
],
}),
);
updatePhaseProgress(1, 1);
finishPhase();
}
/* ZIP */
setPhaseState("Comprimare ZIP", weights.zip, 1);
const zip = new JSZip();
if (!terenuriGpkg || !cladiriGpkg)
throw new Error("Failed to build GeoPackage files");
zip.file("terenuri.gpkg", terenuriGpkg);
zip.file("cladiri.gpkg", cladiriGpkg);
const report: Record<string, unknown> = {
siruta: validated.siruta,
generatedAt: new Date().toISOString(),
terenuri: {
count: terenuriFeatures.length,
expected: terenuriCount ?? null,
},
cladiri: {
count: cladiriFeatures.length,
expected: cladiriCount ?? null,
},
};
if (validated.mode === "magic" && magicGpkg && csvContent) {
zip.file("terenuri_magic.gpkg", magicGpkg);
zip.file("terenuri_complet.csv", csvContent);
report.magic = {
csvRows: csvContent.split("\n").length - 1,
hasBuildingCount,
legalBuildingCount,
};
}
zip.file("export_report.json", JSON.stringify(report, null, 2));
const zipBuffer = await withHeartbeat(() =>
zip.generateAsync({ type: "nodebuffer", compression: "STORE" }),
);
updatePhaseProgress(1, 1);
finishPhase();
/* Done */
const terenuriLabel =
typeof terenuriCount === "number"
? `${terenuriFeatures.length}/${terenuriCount}`
: `${terenuriFeatures.length}/?`;
const cladiriLabel =
typeof cladiriCount === "number"
? `${cladiriFeatures.length}/${cladiriCount}`
: `${cladiriFeatures.length}/?`;
message = `Finalizat 100% · Terenuri ${terenuriLabel} · Cladiri ${cladiriLabel}`;
status = "done";
phase = "Finalizat";
note = undefined;
pushProgress();
scheduleClear(jobId);
const filename =
validated.mode === "magic"
? `eterra_uat_${validated.siruta}_magic.zip`
: `eterra_uat_${validated.siruta}_terenuri_cladiri.zip`;
return new Response(new Uint8Array(zipBuffer), {
headers: {
"Content-Type": "application/zip",
"Content-Disposition": `attachment; filename="${filename}"`,
},
});
} catch (error) {
const errMessage =
error instanceof Error ? error.message : "Unexpected server error";
status = "error";
message = errMessage;
note = undefined;
pushProgress();
scheduleClear(jobId);
const lower = errMessage.toLowerCase();
const statusCode =
lower.includes("login failed") || lower.includes("session")
? 401
: 400;
return new Response(JSON.stringify({ error: errMessage }), {
status: statusCode,
headers: { "Content-Type": "application/json" },
});
}
}