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

This commit is contained in:
Dave
2025-10-08 13:57:34 -04:00
parent 2ffc4b81f4
commit de02b34a63
28 changed files with 2550 additions and 2443 deletions

View File

@@ -3,7 +3,24 @@ const FortellisLogger = require("../fortellis/fortellis-logger");
const RRLogger = require("../rr/rr-logger");
const { FortellisJobExport, FortellisSelectedCustomer } = require("../fortellis/fortellis");
const CdkCalculateAllocations = require("../cdk/cdk-calculate-allocations").default;
const { RRJobExport, RRSelectedCustomer } = require("../rr/rr-job-export");
const { exportJobToRome } = require("../rr/rr-job-export");
const lookupApi = require("../rr/rr-lookup");
const RRCalculateAllocations = require("../rr/rr-calculate-allocations");
function resolveRRConfigFrom(payload = {}) {
// Back-compat: allow txEnvelope.config from old callers
const cfg = payload.config || payload.bodyshopConfig || payload.txEnvelope?.config || {};
return {
baseUrl: cfg.baseUrl || process.env.RR_BASE_URL,
username: cfg.username || process.env.RR_USERNAME,
password: cfg.password || process.env.RR_PASSWORD,
ppsysId: cfg.ppsysId || process.env.RR_PPSYSID,
dealer_number: cfg.dealer_number || process.env.RR_DEALER_NUMBER,
store_number: cfg.store_number || process.env.RR_STORE_NUMBER,
branch_number: cfg.branch_number || process.env.RR_BRANCH_NUMBER,
rrTransport: (cfg.rrTransport || process.env.RR_TRANSPORT || "STAR").toUpperCase()
};
}
const redisSocketEvents = ({
io,
@@ -340,75 +357,35 @@ const redisSocketEvents = ({
};
const registerRREvents = (socket) => {
socket.on("rr-export-job", async ({ jobid, txEnvelope }) => {
// Orchestrated Export (Customer → Vehicle → Repair Order)
socket.on("rr-export-job", async (payload = {}) => {
try {
await RRJobExport({
socket,
redisHelpers: {
setSessionData,
getSessionData,
addUserSocketMapping,
removeUserSocketMapping,
refreshUserSocketTTL,
getUserSocketMappingByBodyshop,
setSessionTransactionData,
getSessionTransactionData,
clearSessionTransactionData
},
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom },
jobid,
txEnvelope
});
// Back-compat: old callers: { jobid, txEnvelope }; new: { job, config, options }
// Prefer direct job/config, otherwise try txEnvelope.{job,config}
const job = payload.job || payload.txEnvelope?.job;
const options = payload.options || payload.txEnvelope?.options || {};
const cfg = resolveRRConfigFrom(payload);
if (!job) {
RRLogger(socket, "error", "RR export missing job payload");
return;
}
const result = await exportJobToRome(socket, job, cfg, options);
// Broadcast to bodyshop room for UI to pick up
const room = getBodyshopRoom(socket.bodyshopId);
io.to(room).emit("rr-export-job:result", { jobid: job.id, result });
} catch (error) {
RRLogger(socket, "error", `Error during RR export: ${error.message}`);
logger.log("rr-job-export-error", "error", null, null, { message: error.message, stack: error.stack });
}
});
socket.on("rr-selected-customer", async ({ jobid, selectedCustomerId }) => {
// Combined search
socket.on("rr-lookup-combined", async ({ jobid, params } = {}, cb) => {
try {
await RRSelectedCustomer({
socket,
redisHelpers: {
setSessionData,
getSessionData,
addUserSocketMapping,
removeUserSocketMapping,
refreshUserSocketTTL,
getUserSocketMappingByBodyshop,
setSessionTransactionData,
getSessionTransactionData,
clearSessionTransactionData
},
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom },
jobid,
selectedCustomerId
});
} catch (error) {
RRLogger(socket, "error", `Error during RR selected-customer: ${error.message}`);
logger.log("rr-selected-customer-error", "error", null, null, { message: error.message, stack: error.stack });
}
});
socket.on("rr-calculate-allocations", async (jobid, callback) => {
try {
const allocations = await CdkCalculateAllocations(socket, jobid);
callback(allocations);
} catch (error) {
RRLogger(socket, "error", `Error during RR calculate allocations: ${error.message}`);
logger.log("rr-calc-allocations-error", "error", null, null, { message: error.message, stack: error.stack });
}
});
socket.on("rr-lookup-combined", async ({ jobid, params }, cb) => {
try {
const { RrCombinedSearch } = require("../rr/rr-lookup");
const data = await RrCombinedSearch({
socket,
redisHelpers: { setSessionTransactionData, getSessionTransactionData },
jobid,
params
});
const cfg = resolveRRConfigFrom({}); // if you want per-call overrides, pass them in the payload and merge here
const data = await lookupApi.combinedSearch(socket, params || {}, cfg);
cb?.(data);
} catch (e) {
RRLogger(socket, "error", `RR combined lookup error: ${e.message}`);
@@ -416,15 +393,11 @@ const redisSocketEvents = ({
}
});
socket.on("rr-get-advisors", async ({ jobid, params }, cb) => {
// Get Advisors
socket.on("rr-get-advisors", async ({ jobid, params } = {}, cb) => {
try {
const { RrGetAdvisors } = require("../rr/rr-lookup");
const data = await RrGetAdvisors({
socket,
redisHelpers: { setSessionTransactionData, getSessionTransactionData },
jobid,
params
});
const cfg = resolveRRConfigFrom({});
const data = await lookupApi.getAdvisors(socket, params || {}, cfg);
cb?.(data);
} catch (e) {
RRLogger(socket, "error", `RR get advisors error: ${e.message}`);
@@ -432,23 +405,46 @@ const redisSocketEvents = ({
}
});
socket.on("rr-get-parts", async ({ jobid, params }, cb) => {
// Get Parts
socket.on("rr-get-parts", async ({ jobid, params } = {}, cb) => {
try {
const { RrGetParts } = require("../rr/rr-lookup");
const data = await RrGetParts({
socket,
redisHelpers: { setSessionTransactionData, getSessionTransactionData },
jobid,
params
});
const cfg = resolveRRConfigFrom({});
const data = await lookupApi.getParts(socket, params || {}, cfg);
cb?.(data);
} catch (e) {
RRLogger(socket, "error", `RR get parts error: ${e.message}`);
cb?.(null);
}
});
};
// (Optional) Selected customer — only keep this if you actually implement it for RR
socket.on("rr-selected-customer", async ({ jobid, selectedCustomerId } = {}) => {
try {
// If you dont have an RRSelectedCustomer implementation now, either:
// 1) no-op with a log, or
// 2) emit a structured event UI can handle as "not supported".
RRLogger(socket, "info", "rr-selected-customer not implemented for RR (no-op)", {
jobid,
selectedCustomerId
});
// If later you add support, call your implementation here.
} catch (error) {
RRLogger(socket, "error", `Error during RR selected-customer: ${error.message}`);
logger.log("rr-selected-customer-error", "error", null, null, { message: error.message, stack: error.stack });
}
});
// Calculate allocations (unchanged — CDK utility)
socket.on("rr-calculate-allocations", async (jobid, callback) => {
try {
const allocations = await RRCalculateAllocations(socket, jobid);
callback(allocations);
} catch (error) {
RRLogger(socket, "error", `Error during RR calculate allocations: ${error.message}`);
logger.log("rr-calc-allocations-error", "error", null, null, { message: error.message, stack: error.stack });
}
});
};
// Call Handlers
registerRoomAndBroadcastEvents(socket);
registerUpdateEvents(socket);