feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Cashiering Checkpoint

This commit is contained in:
Dave
2025-11-12 16:13:23 -05:00
parent e3b4620d0c
commit 556cd993b9
6 changed files with 510 additions and 91 deletions

View File

@@ -3,7 +3,70 @@ const { RRClient } = require("./lib/index.cjs");
const { getRRConfigFromBodyshop } = require("./rr-config");
const RRLogger = require("./rr-logger");
// Build client + opts from bodyshop
const COUNTRY_MAP = {
US: "US",
USA: "US",
"UNITED STATES": "US",
CA: "CA",
CAN: "CA",
CANADA: "CA"
};
function toCountry2(v) {
const s = String(v || "")
.trim()
.toUpperCase();
if (!s) return "US"; // sane default
if (COUNTRY_MAP[s]) return COUNTRY_MAP[s];
// fallbacks: prefer 2-char; last resort: take first 2
return s.length === 2 ? s : s.slice(0, 2);
}
function normalizePhone(num) {
const d = String(num || "").replace(/\D/g, "");
const n = d.length === 11 && d.startsWith("1") ? d.slice(1) : d;
return n.slice(0, 10);
}
function normalizePostal(pc, country) {
const s = String(pc || "").trim();
if (country === "US") return s.replace(/[^0-9]/g, "").slice(0, 5);
if (country === "CA") return s.toUpperCase().replace(/\s+/g, "").slice(0, 6);
return s;
}
function sanitizeRRCustomerPayload(payload = {}) {
const out = { ...payload };
out.addresses = (payload.addresses || []).map((a) => {
const country = toCountry2(a.country);
return {
...a,
country,
state: String(a.state || "")
.toUpperCase()
.slice(0, 2),
postalCode: normalizePostal(a.postalCode, country)
};
});
out.phones = (payload.phones || []).map((p) => ({
...p,
number: normalizePhone(p.number)
}));
// trim names defensively (RR has various max lengths by site config)
if (out.firstName) out.firstName = String(out.firstName).trim().slice(0, 30);
if (out.lastName) out.lastName = String(out.lastName).trim().slice(0, 30);
return out;
}
/**
* Build an RR client + common opts from a bodyshop row
* @param bodyshop
* @returns {{client: *, opts: {routing: {dealerNumber: *, storeNumber: *, areaNumber: *}, envelope: {sender: {component: string, task: string, referenceId: string, creator: string, senderName: string}}}}}
*/
function buildClientAndOpts(bodyshop) {
const cfg = getRRConfigFromBodyshop(bodyshop);
const client = new RRClient({
@@ -29,19 +92,29 @@ function buildClientAndOpts(bodyshop) {
return { client, opts };
}
/**
* Strip all non-digit characters from a string
* @param s
* @returns {string}
*/
function digitsOnly(s) {
return String(s || "").replace(/\D/g, "");
}
/**
* Return a new array with only unique values from the input array
* @param arr
* @returns {any[]}
*/
function uniq(arr) {
return Array.from(new Set(arr));
}
/**
* Build a payload that matches the RR client expectations for insert/update:
* - ibFlag: 'I' (individual) or 'B' (business). If we have a first name, default to 'I', else 'B' if company present.
* - Must include lastName OR customerName.
* - addresses[] / phones[] / emails[] per the librarys toView() contract.
* Build RR customer payload from job.ownr_* fields, with optional overrides.
* @param job
* @param overrides
* @returns {{ibFlag: string, firstName, lastName, customerName, createdBy, customerType, addresses: [{type, line1: *, line2, city, state, postalCode, country}], phones: {number: *}[], emails: [{address: string}]}}
*/
function buildCustomerPayloadFromJob(job, overrides = {}) {
// Pull ONLY from job.ownr_* fields (no job.customer.*)
@@ -114,7 +187,8 @@ async function createRRCustomer({ bodyshop, job, overrides = {}, socket }) {
let res;
try {
res = await client.insertCustomer(payload, opts);
const safePayload = sanitizeRRCustomerPayload(payload);
res = await client.insertCustomer(safePayload, opts);
} catch (e) {
log("error", "RR insertCustomer transport error", { message: e?.message, stack: e?.stack, payload });
throw e;