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:
@@ -1,18 +1,5 @@
|
||||
/**
|
||||
* @file rrRoutes.js
|
||||
* @description Express routes for Reynolds & Reynolds (Rome) integration.
|
||||
* Endpoints:
|
||||
* - POST /rr/customer/insert
|
||||
* - POST /rr/customer/update
|
||||
* - POST /rr/repair-order/create
|
||||
* - POST /rr/repair-order/update
|
||||
* - POST /rr/lookup/advisors
|
||||
* - POST /rr/lookup/parts
|
||||
* - POST /rr/lookup/combined-search
|
||||
* - POST /rr/export/job
|
||||
* - GET /rr/actions
|
||||
* - GET /rr/templates/verify
|
||||
*/
|
||||
// server/rr/rrRoutes.js
|
||||
"use strict";
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
@@ -20,19 +7,12 @@ const router = express.Router();
|
||||
const RRLogger = require("./rr-logger");
|
||||
const { RrApiError } = require("./rr-error");
|
||||
|
||||
// Domain modules
|
||||
const customerApi = require("./rr-customer"); // insertCustomer, updateCustomer
|
||||
const roApi = require("./rr-repair-orders"); // createRepairOrder, updateRepairOrder
|
||||
const lookupApi = require("./rr-lookup"); // getAdvisors, getParts, combinedSearch
|
||||
const { exportJobToRR } = require("./rr-job-export"); // orchestrator
|
||||
const customerApi = require("./rr-customer");
|
||||
const roApi = require("./rr-repair-orders");
|
||||
const lookupApi = require("./rr-lookup");
|
||||
const { exportJobToRR } = require("./rr-job-export");
|
||||
|
||||
// Diagnostics
|
||||
const { listActions, verifyTemplatesExist } = require("./rr-wsdl");
|
||||
|
||||
// DB-driven RR config (no env fallback for dealer/store/branch here)
|
||||
const { getRRConfigForBodyshop } = require("./rr-config");
|
||||
|
||||
// -------------------- Helpers --------------------
|
||||
// --- helpers ---
|
||||
|
||||
function ok(res, payload = {}) {
|
||||
return res.json({ success: true, ...payload });
|
||||
@@ -44,174 +24,144 @@ function fail(res, error, status = 400) {
|
||||
}
|
||||
|
||||
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;
|
||||
try {
|
||||
return req.app?.get?.("socket") || null;
|
||||
} catch {
|
||||
return 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;
|
||||
|
||||
function requireBodyshopId(req) {
|
||||
const body = req?.body || {};
|
||||
const fromBody = body.bodyshopId;
|
||||
const fromJob = body.job && (body.job.shopid || body.job.bodyshopId);
|
||||
const fromHeader = typeof req.get === "function" ? req.get("x-bodyshop-id") : undefined;
|
||||
const bodyshopId = fromBody || fromJob || fromHeader;
|
||||
if (!bodyshopId) {
|
||||
throw new RrApiError(
|
||||
"Missing bodyshopId (expected in body.bodyshopId, body.job.shopid, or x-bodyshop-id header)",
|
||||
"Missing bodyshopId (in body.bodyshopId, body.job.shopid/bodyshopId, or x-bodyshop-id header)",
|
||||
"BAD_REQUEST"
|
||||
);
|
||||
}
|
||||
|
||||
return getRRConfigForBodyshop(bodyshopId);
|
||||
return bodyshopId;
|
||||
}
|
||||
|
||||
// -------------------- Customers --------------------
|
||||
// --- customers ---
|
||||
|
||||
router.post("/rr/customer/insert", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { customer } = req.body || {};
|
||||
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const { customer } = req.body || {};
|
||||
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) {
|
||||
RRLogger(socket, "error", "RR /customer/insert failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
const result = await customerApi.insertCustomer({ bodyshopId, payload: customer });
|
||||
RRLogger(socket)("info", "RR customer insert", { bodyshopId });
|
||||
return ok(res, { data: result.data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/customer/insert failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/rr/customer/update", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { customer } = req.body || {};
|
||||
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const { customer } = req.body || {};
|
||||
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) {
|
||||
RRLogger(socket, "error", "RR /customer/update failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
const result = await customerApi.updateCustomer({ bodyshopId, payload: customer });
|
||||
RRLogger(socket)("info", "RR customer update", { bodyshopId });
|
||||
return ok(res, { data: result.data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/customer/update failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
// -------------------- Repair Orders --------------------
|
||||
// --- repair orders ---
|
||||
|
||||
router.post("/rr/repair-order/create", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { job } = req.body || {};
|
||||
|
||||
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) {
|
||||
RRLogger(socket, "error", "RR /repair-order/create failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const { ro } = req.body || {};
|
||||
if (!ro) throw new RrApiError("Missing 'ro' in request body", "BAD_REQUEST");
|
||||
const result = await roApi.createRepairOrder({ bodyshopId, payload: ro });
|
||||
RRLogger(socket)("info", "RR create RO", { bodyshopId });
|
||||
return ok(res, { data: result.data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/repair-order/create failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/rr/repair-order/update", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { job } = req.body || {};
|
||||
|
||||
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) {
|
||||
RRLogger(socket, "error", "RR /repair-order/update failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const { ro } = req.body || {};
|
||||
if (!ro) throw new RrApiError("Missing 'ro' in request body", "BAD_REQUEST");
|
||||
const result = await roApi.updateRepairOrder({ bodyshopId, payload: ro });
|
||||
RRLogger(socket)("info", "RR update RO", { bodyshopId });
|
||||
return ok(res, { data: result.data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/repair-order/update failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
// -------------------- Lookups --------------------
|
||||
// --- lookups ---
|
||||
|
||||
router.post("/rr/lookup/advisors", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { criteria = {} } = req.body || {};
|
||||
|
||||
try {
|
||||
const cfg = await resolveRRConfigHttp(req);
|
||||
const result = await lookupApi.getAdvisors(socket, criteria, cfg);
|
||||
return ok(res, result);
|
||||
} catch (err) {
|
||||
RRLogger(socket, "error", "RR /lookup/advisors failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const result = await lookupApi.getAdvisors({ bodyshopId, ...req.body });
|
||||
return ok(res, { data: result.data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/lookup/advisors failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/rr/lookup/parts", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { criteria = {} } = req.body || {};
|
||||
|
||||
try {
|
||||
const cfg = await resolveRRConfigHttp(req);
|
||||
const result = await lookupApi.getParts(socket, criteria, cfg);
|
||||
return ok(res, result);
|
||||
} catch (err) {
|
||||
RRLogger(socket, "error", "RR /lookup/parts failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const result = await lookupApi.getParts({ bodyshopId, ...req.body });
|
||||
return ok(res, { data: result.data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/lookup/parts failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/rr/lookup/combined-search", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { criteria = {} } = req.body || {};
|
||||
|
||||
try {
|
||||
const cfg = await resolveRRConfigHttp(req);
|
||||
const result = await lookupApi.combinedSearch(socket, criteria, cfg);
|
||||
return ok(res, result);
|
||||
} catch (err) {
|
||||
RRLogger(socket, "error", "RR /lookup/combined-search failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const result = await lookupApi.combinedSearch({ bodyshopId, ...req.body });
|
||||
return ok(res, { data: result.data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/lookup/combined-search failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
// -------------------- Orchestrated export --------------------
|
||||
// --- export orchestrator ---
|
||||
|
||||
router.post("/rr/export/job", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const { job, options = {} } = req.body || {};
|
||||
|
||||
const logger = (level, message, ctx) => RRLogger(socket)(level, message, ctx);
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const { job, options = {} } = req.body || {};
|
||||
if (!job) throw new RrApiError("Missing 'job' in request body", "BAD_REQUEST");
|
||||
const cfg = await resolveRRConfigHttp(req);
|
||||
const result = await exportJobToRR(socket, job, cfg, options);
|
||||
return ok(res, result);
|
||||
} catch (err) {
|
||||
RRLogger(socket, "error", "RR /export/job failed", { err: err.message });
|
||||
return fail(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
// -------------------- Diagnostics --------------------
|
||||
|
||||
router.get("/rr/actions", (_req, res) => {
|
||||
try {
|
||||
return ok(res, { actions: listActions() });
|
||||
} catch (err) {
|
||||
return fail(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/rr/templates/verify", async (_req, res) => {
|
||||
try {
|
||||
const issues = await verifyTemplatesExist();
|
||||
return ok(res, { ok: issues.length === 0, issues });
|
||||
} catch (err) {
|
||||
return fail(res, err);
|
||||
const data = await exportJobToRR({ bodyshopId, job, logger, ...options });
|
||||
return ok(res, { data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/export/job failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user