feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Expanded Logs / Formatting change

This commit is contained in:
Dave
2025-11-12 17:01:54 -05:00
parent 556cd993b9
commit 90f653c0b7
13 changed files with 444 additions and 254 deletions

View File

@@ -1,11 +1,13 @@
// File: server/rr/rr-service-vehicles.js
// Idempotent Service Vehicle ensure: if VIN exists (owner match or not), don't fail.
const RRLogger = require("./rr-logger");
const { buildClientAndOpts, rrCombinedSearch } = require("./rr-lookup");
// --- helpers ---
function pickVin({ vin, job }) {
/**
* Pick and normalize VIN from inputs
* @param vin
* @param job
* @returns {string}
*/
const pickVin = ({ vin, job }) => {
const v = vin || job?.v_vin || job?.vehicle?.vin || job?.vin || job?.vehicleVin || null;
if (!v) return "";
@@ -13,14 +15,27 @@ function pickVin({ vin, job }) {
.replace(/[^A-Za-z0-9]/g, "")
.toUpperCase()
.slice(0, 17);
}
};
function pickCustNo({ selectedCustomerNo, custNo, customerNo }) {
/**
* Pick and normalize customer number from inputs
* @param selectedCustomerNo
* @param custNo
* @param customerNo
* @returns {string|string}
*/
const pickCustNo = ({ selectedCustomerNo, custNo, customerNo }) => {
const c = selectedCustomerNo ?? custNo ?? customerNo ?? null;
return c != null ? String(c).trim() : "";
}
};
function ownersFromCombined(res, wantedVin) {
/**
* Extract owner customer numbers from combined search results
* @param res
* @param wantedVin
* @returns {Set<any>}
*/
const ownersFromCombined = (res, wantedVin) => {
const blocks = Array.isArray(res?.data) ? res.data : Array.isArray(res) ? res : [];
const owners = new Set();
for (const blk of blocks) {
@@ -35,14 +50,19 @@ function ownersFromCombined(res, wantedVin) {
}
}
return owners;
}
};
function isAlreadyExistsError(e) {
/**
* Determine if error indicates "already exists"
* @param e
* @returns {boolean}
*/
const isAlreadyExistsError = (e) => {
if (!e) return false;
if (e.code === 300) return true;
const msg = (e.message || "").toUpperCase();
return msg.includes("ALREADY EXISTS") || msg.includes("VEHICLE ALREADY EXISTS");
}
};
/**
* Ensure/create a Service Vehicle in RR for the given VIN + customer.
@@ -56,7 +76,7 @@ function isAlreadyExistsError(e) {
*
* Returns: { created:boolean, exists:boolean, vin, customerNo, svId?, status? }
*/
async function ensureRRServiceVehicle(args = {}) {
const ensureRRServiceVehicle = async (args = {}) => {
const {
client: inClient,
routing: inRouting,
@@ -199,7 +219,7 @@ async function ensureRRServiceVehicle(args = {}) {
});
throw e;
}
}
};
module.exports = {
ensureRRServiceVehicle