feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - rr-Utils hardening

This commit is contained in:
Dave
2025-11-07 15:57:22 -05:00
parent e5ed11287d
commit 00e6d31a88

View File

@@ -46,6 +46,7 @@ const makeVehicleSearchPayloadFromJob = (job) => {
/**
* Normalize customer candidates from VIN/name blocks, including address + owner flag
* IMPORTANT: If no ServVehicle/CustomerNo exists (e.g. name-only hits), fall back to NameRecId.
* @param res
* @param ownersSet
* @returns {any[]}
@@ -88,7 +89,7 @@ const normalizeCustomerCandidates = (res, { ownersSet = null } = {}) => {
"PostalCode",
"Country",
"CountryCode",
"County" // << allow County
"County"
]);
const unknown = Object.keys(chosen || {}).filter((k) => !allowed.has(k));
if (unknown.length) console.log("[RR:normCandidates] Unexpected address keys seen:", unknown);
@@ -112,25 +113,42 @@ const normalizeCustomerCandidates = (res, { ownersSet = null } = {}) => {
const address = pickAddr(nci?.Address);
for (const custNo of custNos) {
const cno = String(custNo).trim();
if (!cno) continue;
// NEW: fallback to NameRecId when no ServVehicle/CustomerNo exists (e.g., pure name search)
const nameRecIdRaw = nci?.NameId?.NameRecId;
const nameRecId = nameRecIdRaw != null ? String(nameRecIdRaw).trim() : "";
if (Array.isArray(custNos) && custNos.length > 0) {
for (const custNo of custNos) {
const cno = String(custNo).trim();
if (!cno) continue;
const item = {
custNo: cno,
name: name || `Customer ${cno}`,
address: address || undefined
};
if (ownersSet && ownersSet.has(cno)) {
item.isVehicleOwner = true;
item.vinOwner = true;
}
out.push(item);
}
} else if (nameRecId) {
// Use NameRecId as the identifier; this is what the RR "name" search provides
const cno = nameRecId;
const item = {
custNo: cno,
name: name || `Customer ${cno}`,
address: address || undefined
};
if (ownersSet && ownersSet.has(cno)) {
item.isVehicleOwner = true;
item.vinOwner = true;
}
// owner flag cannot be inferred without a VIN owner set
out.push(item);
}
}
// Deduplicate by custNo, merge owner/address flags
const byId = new Map();
for (const c of out) {
const key = (c.custNo || "").trim();