Files
bodyshop/server/rr/rr-customers.js

120 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
});
// For customer INSERT, the STAR envelope typically uses Task="CU" and ReferenceId="Insert".
// Routing (dealer/store/area) is provided via opts.routing and applied by the lib.
const opts = {
routing: cfg.routing,
envelope: {
sender: {
component: "Rome",
task: "CU",
referenceId: "Insert",
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 clients conventions; the lib normalizes case internally.
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 }.
* NOTE: The library returns { data: { dmsRecKey, status, statusCode }, statusBlocks, ... }.
* We map data.dmsRecKey -> custNo for compatibility with existing callers.
*/
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 {
res = await client.insertCustomer(payload, opts);
} catch (e) {
log("error", "RR insertCustomer transport error", { message: e?.message, stack: e?.stack });
throw e;
}
const data = res?.data ?? res; // be tolerant to shapes
const trx = res?.statusBlocks?.transaction;
// Primary: map dmsRecKey -> custNo
let custNo = data?.dmsRecKey;
if (!custNo) {
log("error", "RR insertCustomer returned no dmsRecKey/custNo", {
status: trx?.status,
statusCode: trx?.statusCode,
message: trx?.message,
data
});
throw new Error(
`RR insertCustomer returned no dmsRecKey (status=${trx?.status ?? "?"} code=${trx?.statusCode ?? "?"}${
trx?.message ? ` msg=${trx.message}` : ""
})`
);
}
// Normalize to string for safety
custNo = String(custNo);
// Preserve existing return shape so callers dont need changes
return { custNo, raw: data };
}
module.exports = {
createRRCustomer
};