feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// server/rr/rr-customers.js
|
||||
// Minimal RR customer create helper
|
||||
// Minimal RR customer create helper (maps dmsRecKey -> custNo for callers)
|
||||
|
||||
const { RRClient } = require("./lib/index.cjs");
|
||||
const { getRRConfigFromBodyshop } = require("./rr-config");
|
||||
@@ -15,13 +15,16 @@ function buildClientAndOpts(bodyshop) {
|
||||
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: "CVC",
|
||||
referenceId: "CreateCustomer",
|
||||
task: "CU",
|
||||
referenceId: "Insert",
|
||||
creator: "RCI",
|
||||
senderName: "RCI"
|
||||
}
|
||||
@@ -50,7 +53,7 @@ function buildCustomerPayloadFromJob(job, overrides = {}) {
|
||||
"";
|
||||
|
||||
const payload = {
|
||||
// These keys follow the RR client’s customers op conventions (the lib normalizes case)
|
||||
// These keys follow the RR client’s conventions; the lib normalizes case internally.
|
||||
firstName: firstName || undefined,
|
||||
lastName: lastName || undefined,
|
||||
companyName: company || undefined,
|
||||
@@ -71,7 +74,8 @@ function buildCustomerPayloadFromJob(job, overrides = {}) {
|
||||
|
||||
/**
|
||||
* Create a customer in RR and return { custNo, raw }.
|
||||
* Tries common op names to stay compatible with the generated client.
|
||||
* 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" });
|
||||
@@ -79,19 +83,20 @@ async function createRRCustomer({ bodyshop, job, overrides = {}, socket }) {
|
||||
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") {
|
||||
try {
|
||||
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");
|
||||
} catch (e) {
|
||||
log("error", "RR insertCustomer transport error", { message: e?.message, stack: e?.stack });
|
||||
throw e;
|
||||
}
|
||||
|
||||
const data = res?.data ?? res;
|
||||
const custNo =
|
||||
const data = res?.data ?? res; // be tolerant to shapes
|
||||
const trx = res?.statusBlocks?.transaction;
|
||||
|
||||
// Primary: map dmsRecKey -> custNo
|
||||
let custNo =
|
||||
data?.dmsRecKey ??
|
||||
// legacy fallbacks (if shapes ever change)
|
||||
data?.custNo ??
|
||||
data?.CustNo ??
|
||||
data?.customerNo ??
|
||||
@@ -100,10 +105,23 @@ async function createRRCustomer({ bodyshop, job, overrides = {}, socket }) {
|
||||
data?.Customer?.CustNo;
|
||||
|
||||
if (!custNo) {
|
||||
log("error", "RR create customer returned no custNo", { data });
|
||||
throw new Error("RR create customer returned no 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 don’t need changes
|
||||
return { custNo, raw: data };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user