feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint - Remove old attempt at Reynolds Integration in favor of new library.
This commit is contained in:
@@ -5,7 +5,6 @@ const { FortellisJobExport, FortellisSelectedCustomer } = require("../fortellis/
|
||||
const CdkCalculateAllocations = require("../cdk/cdk-calculate-allocations").default;
|
||||
const { exportJobToRR } = require("../rr/rr-job-export");
|
||||
const lookupApi = require("../rr/rr-lookup");
|
||||
const { getRRConfigForBodyshop } = require("../rr/rr-config");
|
||||
|
||||
const redisSocketEvents = ({
|
||||
io,
|
||||
@@ -341,94 +340,122 @@ const redisSocketEvents = ({
|
||||
});
|
||||
};
|
||||
|
||||
// Reynolds & Reynolds socket events (uses new client-backed ops)
|
||||
const registerRREvents = (socket) => {
|
||||
const log = (level, message, ctx) => RRLogger(socket)(level, message, ctx);
|
||||
|
||||
const resolveBodyshopId = (payload, job) =>
|
||||
payload?.bodyshopId || socket.bodyshopId || job?.shopid || job?.bodyshopId;
|
||||
|
||||
const resolveJobId = (explicitJobId, payload, job) =>
|
||||
explicitJobId ||
|
||||
payload?.jobid ||
|
||||
payload?.txEnvelope?.jobid ||
|
||||
job?.id ||
|
||||
payload?.txEnvelope?.job?.id ||
|
||||
null;
|
||||
|
||||
// Orchestrated Export (Customer → Vehicle → Repair Order)
|
||||
socket.on("rr-export-job", async (payload = {}) => {
|
||||
try {
|
||||
// Back-compat: old callers: { jobid, txEnvelope }; new: { job, options }
|
||||
// Prefer direct job, otherwise try txEnvelope.job
|
||||
const job = payload.job || payload.txEnvelope?.job;
|
||||
const options = payload.options || payload.txEnvelope?.options || {};
|
||||
// Resolve per-bodyshop RR config strictly from DB:
|
||||
const bodyshopId = payload.bodyshopId || socket.bodyshopId || job?.shopid;
|
||||
const cfg = await getRRConfigForBodyshop(bodyshopId);
|
||||
const bodyshopId = resolveBodyshopId(payload, job);
|
||||
const jobid = resolveJobId(payload.jobid, payload, job);
|
||||
|
||||
if (!job) {
|
||||
RRLogger(socket, "error", "RR export missing job payload");
|
||||
log("error", "RR export missing job payload", { jobid });
|
||||
return;
|
||||
}
|
||||
if (!bodyshopId) {
|
||||
log("error", "RR export missing bodyshopId", { jobid });
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await exportJobToRR(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 });
|
||||
const result = await exportJobToRR({ bodyshopId, job, logger: log, ...options });
|
||||
|
||||
// Broadcast keyed by bodyshop + include jobid
|
||||
const room = getBodyshopRoom(bodyshopId);
|
||||
io.to(room).emit("rr-export-job:result", { jobid, bodyshopId, 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 });
|
||||
const jobid = resolveJobId(payload?.jobid, payload, payload?.job || payload?.txEnvelope?.job);
|
||||
log("error", `Error during RR export: ${error.message}`, { jobid, stack: error.stack });
|
||||
logger.log("rr-job-export-error", "error", null, null, { jobid, message: error.message, stack: error.stack });
|
||||
}
|
||||
});
|
||||
|
||||
// Combined search
|
||||
// Combined search (customer/vehicle)
|
||||
socket.on("rr-lookup-combined", async ({ jobid, params } = {}, cb) => {
|
||||
try {
|
||||
const cfg = await getRRConfigForBodyshop(socket.bodyshopId);
|
||||
const data = await lookupApi.combinedSearch(socket, params || {}, cfg);
|
||||
cb?.(data);
|
||||
const bodyshopId = resolveBodyshopId({ bodyshopId: params?.bodyshopId }, null);
|
||||
const resolvedJobId = resolveJobId(jobid, { jobid }, null);
|
||||
if (!bodyshopId) throw new Error("Missing bodyshopId");
|
||||
|
||||
const res = await lookupApi.combinedSearch({ bodyshopId, ...(params || {}) });
|
||||
cb?.({ jobid: resolvedJobId, data: res?.data ?? res });
|
||||
} catch (e) {
|
||||
RRLogger(socket, "error", `RR combined lookup error: ${e.message}`);
|
||||
cb?.(null);
|
||||
log("error", `RR combined lookup error: ${e.message}`, { jobid });
|
||||
cb?.({ jobid, error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Get Advisors
|
||||
socket.on("rr-get-advisors", async ({ jobid, params } = {}, cb) => {
|
||||
try {
|
||||
const cfg = await getRRConfigForBodyshop(socket.bodyshopId);
|
||||
const data = await lookupApi.getAdvisors(socket, params || {}, cfg);
|
||||
cb?.(data);
|
||||
const bodyshopId = resolveBodyshopId({ bodyshopId: params?.bodyshopId }, null);
|
||||
const resolvedJobId = resolveJobId(jobid, { jobid }, null);
|
||||
if (!bodyshopId) throw new Error("Missing bodyshopId");
|
||||
|
||||
const res = await lookupApi.getAdvisors({ bodyshopId, ...(params || {}) });
|
||||
cb?.({ jobid: resolvedJobId, data: res?.data ?? res });
|
||||
} catch (e) {
|
||||
RRLogger(socket, "error", `RR get advisors error: ${e.message}`);
|
||||
cb?.(null);
|
||||
log("error", `RR get advisors error: ${e.message}`, { jobid });
|
||||
cb?.({ jobid, error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Get Parts
|
||||
socket.on("rr-get-parts", async ({ jobid, params } = {}, cb) => {
|
||||
try {
|
||||
const cfg = await getRRConfigForBodyshop(socket.bodyshopId);
|
||||
const data = await lookupApi.getParts(socket, params || {}, cfg);
|
||||
cb?.(data);
|
||||
const bodyshopId = resolveBodyshopId({ bodyshopId: params?.bodyshopId }, null);
|
||||
const resolvedJobId = resolveJobId(jobid, { jobid }, null);
|
||||
if (!bodyshopId) throw new Error("Missing bodyshopId");
|
||||
|
||||
const res = await lookupApi.getParts({ bodyshopId, ...(params || {}) });
|
||||
cb?.({ jobid: resolvedJobId, data: res?.data ?? res });
|
||||
} catch (e) {
|
||||
RRLogger(socket, "error", `RR get parts error: ${e.message}`);
|
||||
cb?.(null);
|
||||
log("error", `RR get parts error: ${e.message}`, { jobid });
|
||||
cb?.({ jobid, error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// (Optional) Selected customer — only keep this if you actually implement it for RR
|
||||
// Optional: Selected customer — currently a no-op for RR
|
||||
socket.on("rr-selected-customer", async ({ jobid, selectedCustomerId } = {}) => {
|
||||
try {
|
||||
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 });
|
||||
}
|
||||
const resolvedJobId = resolveJobId(jobid, { jobid }, null);
|
||||
log("info", "rr-selected-customer not implemented for RR (no-op)", {
|
||||
jobid: resolvedJobId,
|
||||
selectedCustomerId
|
||||
});
|
||||
});
|
||||
|
||||
// Calculate allocations (unchanged — CDK utility)
|
||||
// Calculate allocations (CDK utility unchanged)
|
||||
socket.on("rr-calculate-allocations", async (jobid, callback) => {
|
||||
try {
|
||||
const allocations = await CdkCalculateAllocations(socket, jobid);
|
||||
callback(allocations);
|
||||
const resolvedJobId = resolveJobId(jobid, { jobid }, null);
|
||||
const allocations = await CdkCalculateAllocations(socket, resolvedJobId);
|
||||
callback({ jobid: resolvedJobId, 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 });
|
||||
log("error", `Error during RR calculate allocations: ${error.message}`, { jobid, stack: error.stack });
|
||||
logger.log("rr-calc-allocations-error", "error", null, null, {
|
||||
jobid,
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
callback?.({ jobid, error: error.message });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Call Handlers
|
||||
registerRoomAndBroadcastEvents(socket);
|
||||
registerUpdateEvents(socket);
|
||||
|
||||
Reference in New Issue
Block a user