feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - rr-Utils hardening
This commit is contained in:
@@ -46,6 +46,7 @@ const makeVehicleSearchPayloadFromJob = (job) => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalize customer candidates from VIN/name blocks, including address + owner flag
|
* 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 res
|
||||||
* @param ownersSet
|
* @param ownersSet
|
||||||
* @returns {any[]}
|
* @returns {any[]}
|
||||||
@@ -88,7 +89,7 @@ const normalizeCustomerCandidates = (res, { ownersSet = null } = {}) => {
|
|||||||
"PostalCode",
|
"PostalCode",
|
||||||
"Country",
|
"Country",
|
||||||
"CountryCode",
|
"CountryCode",
|
||||||
"County" // << allow County
|
"County"
|
||||||
]);
|
]);
|
||||||
const unknown = Object.keys(chosen || {}).filter((k) => !allowed.has(k));
|
const unknown = Object.keys(chosen || {}).filter((k) => !allowed.has(k));
|
||||||
if (unknown.length) console.log("[RR:normCandidates] Unexpected address keys seen:", unknown);
|
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);
|
const address = pickAddr(nci?.Address);
|
||||||
|
|
||||||
for (const custNo of custNos) {
|
// NEW: fallback to NameRecId when no ServVehicle/CustomerNo exists (e.g., pure name search)
|
||||||
const cno = String(custNo).trim();
|
const nameRecIdRaw = nci?.NameId?.NameRecId;
|
||||||
if (!cno) continue;
|
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 = {
|
const item = {
|
||||||
custNo: cno,
|
custNo: cno,
|
||||||
name: name || `Customer ${cno}`,
|
name: name || `Customer ${cno}`,
|
||||||
address: address || undefined
|
address: address || undefined
|
||||||
};
|
};
|
||||||
|
// owner flag cannot be inferred without a VIN owner set
|
||||||
if (ownersSet && ownersSet.has(cno)) {
|
|
||||||
item.isVehicleOwner = true;
|
|
||||||
item.vinOwner = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
out.push(item);
|
out.push(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deduplicate by custNo, merge owner/address flags
|
||||||
const byId = new Map();
|
const byId = new Map();
|
||||||
for (const c of out) {
|
for (const c of out) {
|
||||||
const key = (c.custNo || "").trim();
|
const key = (c.custNo || "").trim();
|
||||||
|
|||||||
Reference in New Issue
Block a user