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
+68 -45
View File
@@ -99,58 +99,81 @@ function persistWorkspace(siruta: string, workspacePk: number) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
function formatAddress(item?: any) { function formatAddress(item?: any) {
const address = item?.immovableAddresses?.[0]?.address ?? null; const addresses = item?.immovableAddresses ?? [];
if (!address) return ""; if (addresses.length === 0) return "";
// If addressDescription is a clean string, use it // Build from ALL addresses (some parcels have multiple)
if (address.addressDescription) { const formatted: string[] = [];
const desc = String(address.addressDescription).trim(); for (const entry of addresses) {
if (desc.length > 3 && !desc.includes("[object")) return desc; const address = entry?.address ?? entry;
} if (!address) continue;
const parts: string[] = []; const parts: string[] = [];
// Street: dictionaryItem.name = type ("Strada"), name = actual name ("DIANEI") // Street: dictionaryItem.name = type ("Strada"), name = actual name ("DIANEI")
const streetObj = address.street; const streetObj = address.street;
if (streetObj) { if (streetObj) {
const streetType = const streetType =
typeof streetObj === "string" typeof streetObj === "string"
? "" ? ""
: (streetObj?.dictionaryItem?.name ?? ""); : (streetObj?.dictionaryItem?.name ?? "");
const streetName = const streetName =
typeof streetObj === "string" ? streetObj : (streetObj?.name ?? ""); typeof streetObj === "string" ? streetObj : (streetObj?.name ?? "");
if (streetType && streetName) { if (streetType && streetName) {
parts.push(`${streetType} ${streetName}`); parts.push(`${streetType} ${streetName}`);
} else if (streetName) { } else if (streetName) {
parts.push(`Str. ${streetName}`); parts.push(`Str. ${streetName}`);
}
}
// postalNo is often the house number in eTerra
const houseNo = address.postalNo ?? address.buildingNo ?? null;
if (houseNo) parts.push(`Nr. ${houseNo}`);
// 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}`);
// Locality
const localityName =
typeof address.locality === "string"
? address.locality
: (address.locality?.name ?? "");
if (localityName) parts.push(localityName);
// County
const countyName =
typeof address.county === "string"
? address.county
: (address.county?.name ?? "");
if (countyName) parts.push(`Jud. ${countyName}`);
// 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);
}
} }
} }
// postalNo is often the house number in eTerra // If we still have nothing, try addressDescription from first entry
const houseNo = address.postalNo ?? address.buildingNo ?? null; if (formatted.length === 0) {
if (houseNo) parts.push(`Nr. ${houseNo}`); 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;
}
}
// Building details (apartments, floors, etc.) return [...new Set(formatted)].join(" | ");
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 =
typeof address.locality === "string"
? address.locality
: (address.locality?.name ?? "");
if (localityName) parts.push(localityName);
// County
const countyName =
typeof address.county === "string"
? address.county
: (address.county?.name ?? "");
if (countyName) parts.push(`Jud. ${countyName}`);
return parts.length ? parts.join(", ") : "";
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any