fix(search): robust address from all structured fields, multi-address support

- Always build from structured fields first (street, postalNo, building, locality)
- Fall back to addressDescription ONLY when no structured fields exist
- Support multiple addresses per immovable (joined with |)
- Deduplicate identical addresses
- Handle addressDescription as last-resort fallback
This commit is contained in:
AI Assistant
2026-03-06 22:57:11 +02:00
parent 742acb2d74
commit 0b049274b1
+33 -10
View File
@@ -99,14 +99,14 @@ function persistWorkspace(siruta: string, workspacePk: number) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function formatAddress(item?: any) {
const address = item?.immovableAddresses?.[0]?.address ?? null;
if (!address) return "";
const addresses = item?.immovableAddresses ?? [];
if (addresses.length === 0) return "";
// If addressDescription is a clean string, use it
if (address.addressDescription) {
const desc = String(address.addressDescription).trim();
if (desc.length > 3 && !desc.includes("[object")) return desc;
}
// Build from ALL addresses (some parcels have multiple)
const formatted: string[] = [];
for (const entry of addresses) {
const address = entry?.address ?? entry;
if (!address) continue;
const parts: string[] = [];
@@ -130,11 +130,11 @@ function formatAddress(item?: any) {
const houseNo = address.postalNo ?? address.buildingNo ?? null;
if (houseNo) parts.push(`Nr. ${houseNo}`);
// Building details (apartments, floors, etc.)
// Building details
if (address.buildingSectionNo) parts.push(`Bl. ${address.buildingSectionNo}`);
if (address.buildingEntryNo) parts.push(`Sc. ${address.buildingEntryNo}`);
if (address.buildingFloorNo) parts.push(`Et. ${address.buildingFloorNo}`);
if (address.buildingUnitNo) parts.push(`Ap. ${address.buildingUnitNo}`);
if (address.buildingSectionNo) parts.push(`Bl. ${address.buildingSectionNo}`);
// Locality
const localityName =
@@ -150,7 +150,30 @@ function formatAddress(item?: any) {
: (address.county?.name ?? "");
if (countyName) parts.push(`Jud. ${countyName}`);
return parts.length ? parts.join(", ") : "";
// Postal code
if (address.postalCode) parts.push(`Cod ${address.postalCode}`);
if (parts.length > 0) {
formatted.push(parts.join(", "));
} else if (address.addressDescription) {
// Fall back to description only if no structured fields found
const desc = String(address.addressDescription).trim();
if (desc.length > 2 && !desc.includes("[object")) {
formatted.push(desc);
}
}
}
// If we still have nothing, try addressDescription from first entry
if (formatted.length === 0) {
const desc = addresses[0]?.address?.addressDescription ?? addresses[0]?.addressDescription;
if (desc) {
const s = String(desc).trim();
if (s.length > 2 && !s.includes("[object")) return s;
}
}
return [...new Set(formatted)].join(" | ");
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any