feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
112
server/rr/rr-customers.js
Normal file
112
server/rr/rr-customers.js
Normal file
@@ -0,0 +1,112 @@
|
||||
// server/rr/rr-customers.js
|
||||
// Minimal RR customer create helper
|
||||
|
||||
const { RRClient } = require("./lib/index.cjs");
|
||||
const { getRRConfigFromBodyshop } = require("./rr-config");
|
||||
const RRLogger = require("./rr-logger");
|
||||
|
||||
// Build client + opts from bodyshop
|
||||
function buildClientAndOpts(bodyshop) {
|
||||
const cfg = getRRConfigFromBodyshop(bodyshop);
|
||||
const client = new RRClient({
|
||||
baseUrl: cfg.baseUrl,
|
||||
username: cfg.username,
|
||||
password: cfg.password,
|
||||
timeoutMs: cfg.timeoutMs,
|
||||
retries: cfg.retries
|
||||
});
|
||||
const opts = {
|
||||
routing: cfg.routing,
|
||||
envelope: {
|
||||
sender: {
|
||||
component: "Rome",
|
||||
task: "CVC",
|
||||
referenceId: "CreateCustomer",
|
||||
creator: "RCI",
|
||||
senderName: "RCI"
|
||||
}
|
||||
}
|
||||
};
|
||||
return { client, opts };
|
||||
}
|
||||
|
||||
// minimal field extraction
|
||||
function digitsOnly(s) {
|
||||
return String(s || "").replace(/[^\d]/g, "");
|
||||
}
|
||||
|
||||
function buildCustomerPayloadFromJob(job, overrides = {}) {
|
||||
const firstName = overrides.firstName ?? job?.ownr_fn ?? job?.customer?.first_name ?? "";
|
||||
const lastName = overrides.lastName ?? job?.ownr_ln ?? job?.customer?.last_name ?? "";
|
||||
const company = overrides.company ?? job?.ownr_co_nm ?? job?.customer?.company_name ?? "";
|
||||
|
||||
// Prefer owner phone; fall back to customer phones
|
||||
const phone =
|
||||
overrides.phone ??
|
||||
job?.ownr_ph1 ??
|
||||
job?.customer?.mobile ??
|
||||
job?.customer?.home_phone ??
|
||||
job?.customer?.phone ??
|
||||
"";
|
||||
|
||||
const payload = {
|
||||
// These keys follow the RR client’s customers op conventions (the lib normalizes case)
|
||||
firstName: firstName || undefined,
|
||||
lastName: lastName || undefined,
|
||||
companyName: company || undefined,
|
||||
phone: digitsOnly(phone) || undefined,
|
||||
email: overrides.email || job?.ownr_ea || job?.customer?.email || undefined,
|
||||
address: {
|
||||
line1: overrides.addressLine1 ?? job?.ownr_addr1 ?? job?.customer?.address_line1 ?? undefined,
|
||||
line2: overrides.addressLine2 ?? job?.ownr_addr2 ?? job?.customer?.address_line2 ?? undefined,
|
||||
city: overrides.city ?? job?.ownr_city ?? job?.customer?.city ?? undefined,
|
||||
state: overrides.state ?? job?.ownr_st ?? job?.customer?.state ?? job?.customer?.province ?? undefined,
|
||||
postalCode: overrides.postalCode ?? job?.ownr_zip ?? job?.customer?.postal_code ?? undefined,
|
||||
country: overrides.country ?? job?.ownr_ctry ?? job?.customer?.country ?? "CA"
|
||||
}
|
||||
};
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a customer in RR and return { custNo, raw }.
|
||||
* Tries common op names to stay compatible with the generated client.
|
||||
*/
|
||||
async function createRRCustomer({ bodyshop, job, overrides = {}, socket }) {
|
||||
const log = RRLogger(socket, { ns: "rr" });
|
||||
const { client, opts } = buildClientAndOpts(bodyshop);
|
||||
const payload = buildCustomerPayloadFromJob(job, overrides);
|
||||
|
||||
let res;
|
||||
// Try common method names; your lib exposes one of these.
|
||||
if (typeof client.createCustomer === "function") {
|
||||
res = await client.createCustomer(payload, opts);
|
||||
} else if (typeof client.insertCustomer === "function") {
|
||||
res = await client.insertCustomer(payload, opts);
|
||||
} else if (client.customers && typeof client.customers.create === "function") {
|
||||
res = await client.customers.create(payload, opts);
|
||||
} else {
|
||||
throw new Error("RR customer create operation not found in client");
|
||||
}
|
||||
|
||||
const data = res?.data ?? res;
|
||||
const custNo =
|
||||
data?.custNo ??
|
||||
data?.CustNo ??
|
||||
data?.customerNo ??
|
||||
data?.CustomerNo ??
|
||||
data?.customer?.custNo ??
|
||||
data?.Customer?.CustNo;
|
||||
|
||||
if (!custNo) {
|
||||
log("error", "RR create customer returned no custNo", { data });
|
||||
throw new Error("RR create customer returned no custNo");
|
||||
}
|
||||
|
||||
return { custNo, raw: data };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createRRCustomer
|
||||
};
|
||||
Reference in New Issue
Block a user