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:
@@ -99,58 +99,81 @@ 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[] = [];
|
||||
const parts: string[] = [];
|
||||
|
||||
// Street: dictionaryItem.name = type ("Strada"), name = actual name ("DIANEI")
|
||||
const streetObj = address.street;
|
||||
if (streetObj) {
|
||||
const streetType =
|
||||
typeof streetObj === "string"
|
||||
? ""
|
||||
: (streetObj?.dictionaryItem?.name ?? "");
|
||||
const streetName =
|
||||
typeof streetObj === "string" ? streetObj : (streetObj?.name ?? "");
|
||||
if (streetType && streetName) {
|
||||
parts.push(`${streetType} ${streetName}`);
|
||||
} else if (streetName) {
|
||||
parts.push(`Str. ${streetName}`);
|
||||
// Street: dictionaryItem.name = type ("Strada"), name = actual name ("DIANEI")
|
||||
const streetObj = address.street;
|
||||
if (streetObj) {
|
||||
const streetType =
|
||||
typeof streetObj === "string"
|
||||
? ""
|
||||
: (streetObj?.dictionaryItem?.name ?? "");
|
||||
const streetName =
|
||||
typeof streetObj === "string" ? streetObj : (streetObj?.name ?? "");
|
||||
if (streetType && streetName) {
|
||||
parts.push(`${streetType} ${streetName}`);
|
||||
} else if (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
|
||||
const houseNo = address.postalNo ?? address.buildingNo ?? null;
|
||||
if (houseNo) parts.push(`Nr. ${houseNo}`);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Building details (apartments, floors, etc.)
|
||||
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(", ") : "";
|
||||
return [...new Set(formatted)].join(" | ");
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
Reference in New Issue
Block a user