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

This commit is contained in:
Dave
2025-11-25 14:13:10 -05:00
parent ae7d150a6c
commit 2b1836d450
4 changed files with 172 additions and 301 deletions

View File

@@ -1,8 +1,8 @@
const CreateRRLogEvent = require("./rr-logger-event");
const { rrCombinedSearch, rrGetAdvisors, buildClientAndOpts } = require("./rr-lookup");
const { QueryJobData } = require("./rr-job-helpers");
const { QueryJobData, buildRogogFromAllocations, buildRolaborFromRogog } = require("./rr-job-helpers");
const { exportJobToRR, finalizeRRRepairOrder } = require("./rr-job-export");
const CdkCalculateAllocations = require("./rr-calculate-allocations").default;
const RRCalculateAllocations = require("./rr-calculate-allocations").default;
const { createRRCustomer } = require("./rr-customers");
const { ensureRRServiceVehicle } = require("./rr-service-vehicles");
const { classifyRRVendorError } = require("./rr-errors");
@@ -898,10 +898,73 @@ const registerRREvents = ({ socket, redisHelpers }) => {
socket.on("rr-calculate-allocations", async (jobid, cb) => {
try {
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: begin", { jobid });
const allocations = await CdkCalculateAllocations(socket, jobid);
cb?.(allocations);
socket.emit("rr-calculate-allocations:result", allocations);
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: success", { items: allocations?.length });
const raw = await RRCalculateAllocations(socket, jobid);
// If the helper returns an explicit error shape, just pass it through.
if (raw && raw.ok === false) {
cb?.(raw);
socket.emit("rr-calculate-allocations:result", raw);
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: helper returned error", {
jobid,
error: raw.error
});
return;
}
let ack;
let jobAllocations;
if (Array.isArray(raw)) {
// Legacy shape: plain allocations array
jobAllocations = raw;
ack = { jobAllocations: raw };
} else {
ack = raw || {};
jobAllocations = Array.isArray(ack.jobAllocations) ? ack.jobAllocations : [];
}
// Try to derive OpCode from bodyshop; fall back to default
let opCode = "28TOZ";
try {
const { bodyshopId } = await getSessionOrSocket(redisHelpers, socket);
const bodyshop = await getBodyshopForSocket({ bodyshopId, socket });
opCode = bodyshop?.rr_configuration?.baseOpCode || opCode;
} catch (e) {
CreateRRLogEvent(socket, "WARN", "rr-calculate-allocations: bodyshop lookup failed, using default OpCode", {
error: e.message
});
}
let rogg = null;
let rolabor = null;
try {
rogg = buildRogogFromAllocations(jobAllocations, { opCode, payType: "Cust" });
if (rogg) {
rolabor = buildRolaborFromRogog(rogg, { payType: "Cust" });
}
} catch (e) {
CreateRRLogEvent(socket, "WARN", "rr-calculate-allocations: failed to build ROGOG/ROLABOR preview", {
error: e.message
});
}
const enriched = {
...ack,
rogg,
rolabor
};
cb?.(enriched);
socket.emit("rr-calculate-allocations:result", enriched);
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: success", {
jobid,
jobAllocations: jobAllocations.length,
hasRogg: !!rogg,
hasRolabor: !!rolabor
});
} catch (e) {
CreateRRLogEvent(socket, "ERROR", "rr-calculate-allocations: failed", { error: e.message, jobid });
cb?.({ ok: false, error: e.message });