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

This commit is contained in:
Dave
2025-10-14 13:23:32 -04:00
parent 6bab792b5e
commit 5a9381ebdb
11 changed files with 754 additions and 911 deletions

View File

@@ -29,32 +29,57 @@ const { exportJobToRome } = require("./rr-job-export"); // orchestrator
// Diagnostics
const { listActions, verifyTemplatesExist } = require("./rr-wsdl");
// Helpers
// DB-driven RR config (no env fallback for dealer/store/branch here)
const { getRRConfigForBodyshop } = require("./rr-config");
// -------------------- Helpers --------------------
function ok(res, payload = {}) {
return res.json({ success: true, ...payload });
}
function fail(res, error, status = 400) {
const message = error?.message || String(error);
return res.status(status).json({ success: false, error: message, code: error?.code });
}
function pickConfig(req) {
// Accept config in either { config } or { bodyshopConfig }
return req.body?.config || req.body?.bodyshopConfig || {};
}
function socketOf(req) {
// If you stash a socket/logging context on the app, grab it; otherwise null
return (req.app && req.app.get && req.app.get("socket")) || null;
}
/**
* Resolve the per-bodyshop RR config strictly from DB.
* Looks for bodyshopId in:
* - req.body.bodyshopId
* - req.body.job?.shopid
* - x-bodyshop-id header
* Throws if not found.
*/
async function resolveRRConfigHttp(req) {
const candidateHeader = req.get && req.get("x-bodyshop-id");
const body = req.body || {};
const bodyshopId = body.bodyshopId || (body.job && (body.job.shopid || body.job.bodyshopId)) || candidateHeader;
if (!bodyshopId) {
throw new RrApiError(
"Missing bodyshopId (expected in body.bodyshopId, body.job.shopid, or x-bodyshop-id header)",
"BAD_REQUEST"
);
}
return getRRConfigForBodyshop(bodyshopId);
}
// -------------------- Customers --------------------
router.post("/rr/customer/insert", async (req, res) => {
const socket = socketOf(req);
const { customer } = req.body || {};
const cfg = pickConfig(req);
try {
if (!customer) throw new RrApiError("Missing 'customer' in request body", "BAD_REQUEST");
const cfg = await resolveRRConfigHttp(req); // DB-driven, required
const result = await customerApi.insertCustomer(socket, customer, cfg);
return ok(res, result);
} catch (err) {
@@ -66,10 +91,10 @@ router.post("/rr/customer/insert", async (req, res) => {
router.post("/rr/customer/update", async (req, res) => {
const socket = socketOf(req);
const { customer } = req.body || {};
const cfg = pickConfig(req);
try {
if (!customer) throw new RrApiError("Missing 'customer' in request body", "BAD_REQUEST");
const cfg = await resolveRRConfigHttp(req);
const result = await customerApi.updateCustomer(socket, customer, cfg);
return ok(res, result);
} catch (err) {
@@ -83,10 +108,10 @@ router.post("/rr/customer/update", async (req, res) => {
router.post("/rr/repair-order/create", async (req, res) => {
const socket = socketOf(req);
const { job } = req.body || {};
const cfg = pickConfig(req);
try {
if (!job) throw new RrApiError("Missing 'job' in request body", "BAD_REQUEST");
const cfg = await resolveRRConfigHttp(req);
const result = await roApi.createRepairOrder(socket, job, cfg);
return ok(res, result);
} catch (err) {
@@ -98,10 +123,10 @@ router.post("/rr/repair-order/create", async (req, res) => {
router.post("/rr/repair-order/update", async (req, res) => {
const socket = socketOf(req);
const { job } = req.body || {};
const cfg = pickConfig(req);
try {
if (!job) throw new RrApiError("Missing 'job' in request body", "BAD_REQUEST");
const cfg = await resolveRRConfigHttp(req);
const result = await roApi.updateRepairOrder(socket, job, cfg);
return ok(res, result);
} catch (err) {
@@ -115,9 +140,9 @@ router.post("/rr/repair-order/update", async (req, res) => {
router.post("/rr/lookup/advisors", async (req, res) => {
const socket = socketOf(req);
const { criteria = {} } = req.body || {};
const cfg = pickConfig(req);
try {
const cfg = await resolveRRConfigHttp(req);
const result = await lookupApi.getAdvisors(socket, criteria, cfg);
return ok(res, result);
} catch (err) {
@@ -129,9 +154,9 @@ router.post("/rr/lookup/advisors", async (req, res) => {
router.post("/rr/lookup/parts", async (req, res) => {
const socket = socketOf(req);
const { criteria = {} } = req.body || {};
const cfg = pickConfig(req);
try {
const cfg = await resolveRRConfigHttp(req);
const result = await lookupApi.getParts(socket, criteria, cfg);
return ok(res, result);
} catch (err) {
@@ -143,9 +168,9 @@ router.post("/rr/lookup/parts", async (req, res) => {
router.post("/rr/lookup/combined-search", async (req, res) => {
const socket = socketOf(req);
const { criteria = {} } = req.body || {};
const cfg = pickConfig(req);
try {
const cfg = await resolveRRConfigHttp(req);
const result = await lookupApi.combinedSearch(socket, criteria, cfg);
return ok(res, result);
} catch (err) {
@@ -159,10 +184,10 @@ router.post("/rr/lookup/combined-search", async (req, res) => {
router.post("/rr/export/job", async (req, res) => {
const socket = socketOf(req);
const { job, options = {} } = req.body || {};
const cfg = pickConfig(req);
try {
if (!job) throw new RrApiError("Missing 'job' in request body", "BAD_REQUEST");
const cfg = await resolveRRConfigHttp(req);
const result = await exportJobToRome(socket, job, cfg, options);
return ok(res, result);
} catch (err) {