feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -1,34 +1,34 @@
|
||||
// server/rr/rrRoutes.js
|
||||
"use strict";
|
||||
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
const RRLogger = require("./rr-logger");
|
||||
const { RrApiError } = require("./rr-error");
|
||||
|
||||
const customerApi = require("./rr-customer");
|
||||
const roApi = require("./rr-repair-orders");
|
||||
const { getRRConfigForBodyshop } = require("./rr-config");
|
||||
const lookupApi = require("./rr-lookup");
|
||||
const { insertCustomer, updateCustomer } = require("./rr-customer");
|
||||
const { exportJobToRR } = require("./rr-job-export");
|
||||
const { SelectedCustomer } = require("./rr-selected-customer");
|
||||
const { QueryJobData } = require("./rr-job-helpers");
|
||||
|
||||
// --- 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 });
|
||||
}
|
||||
|
||||
// --- helpers & middleware (kept local for this router) ---
|
||||
function socketOf(req) {
|
||||
try {
|
||||
return req.app?.get?.("socket") || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
// attach a minimal "socket-like" emitter for logger compatibility
|
||||
return {
|
||||
emit: () => {
|
||||
//
|
||||
},
|
||||
handshake: { auth: { token: req?.headers?.authorization?.replace(/^Bearer\s+/i, "") } },
|
||||
user: req?.user
|
||||
};
|
||||
}
|
||||
|
||||
function ok(res, payload) {
|
||||
return res.status(200).json(payload || { ok: true });
|
||||
}
|
||||
function fail(res, e) {
|
||||
const code = e?.code === "BAD_REQUEST" ? 400 : e?.code === "NOT_CONFIGURED" ? 412 : 500;
|
||||
return res.status(code).json({ error: e?.message || String(e), code: e?.code || "RR_API_ERROR" });
|
||||
}
|
||||
|
||||
function requireBodyshopId(req) {
|
||||
@@ -39,117 +39,122 @@ function requireBodyshopId(req) {
|
||||
const bodyshopId = fromBody || fromJob || fromHeader;
|
||||
if (!bodyshopId) {
|
||||
throw new RrApiError(
|
||||
"Missing bodyshopId (in body.bodyshopId, body.job.shopid/bodyshopId, or x-bodyshop-id header)",
|
||||
"Missing bodyshopId (expected in body.bodyshopId, body.job.shopid/bodyshopId, or x-bodyshop-id header)",
|
||||
"BAD_REQUEST"
|
||||
);
|
||||
}
|
||||
return bodyshopId;
|
||||
}
|
||||
|
||||
// --- customers ---
|
||||
|
||||
router.post("/rr/customer/insert", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
// --- sanity/config checks ---
|
||||
router.get("/rr/config", async (req, res) => {
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const { customer } = req.body || {};
|
||||
if (!customer) throw new RrApiError("Missing 'customer' in request body", "BAD_REQUEST");
|
||||
const result = await customerApi.insertCustomer({ bodyshopId, payload: customer });
|
||||
RRLogger(socket)("info", "RR customer insert", { bodyshopId });
|
||||
return ok(res, { data: result.data });
|
||||
const cfg = await getRRConfigForBodyshop(bodyshopId);
|
||||
return ok(res, { data: cfg });
|
||||
} 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);
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const { customer } = req.body || {};
|
||||
if (!customer) throw new RrApiError("Missing 'customer' in request body", "BAD_REQUEST");
|
||||
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 ---
|
||||
|
||||
router.post("/rr/repair-order/create", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
try {
|
||||
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);
|
||||
try {
|
||||
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 ---
|
||||
|
||||
router.post("/rr/lookup/advisors", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const result = await lookupApi.getAdvisors({ bodyshopId, ...req.body });
|
||||
return ok(res, { data: result.data });
|
||||
const data = await lookupApi.getAdvisors({ bodyshopId, ...(req.body || {}) });
|
||||
return ok(res, { 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);
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const result = await lookupApi.getParts({ bodyshopId, ...req.body });
|
||||
return ok(res, { data: result.data });
|
||||
const data = await lookupApi.getParts({ bodyshopId, ...(req.body || {}) });
|
||||
return ok(res, { 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);
|
||||
router.post("/rr/combined-search", async (req, res) => {
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const result = await lookupApi.combinedSearch({ bodyshopId, ...req.body });
|
||||
return ok(res, { data: result.data });
|
||||
const data = await lookupApi.combinedSearch({ bodyshopId, ...(req.body || {}) });
|
||||
return ok(res, { data });
|
||||
} catch (e) {
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
// --- customers (basic insert/update) ---
|
||||
router.post("/rr/customer/insert", async (req, res) => {
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const data = await insertCustomer({ bodyshopId, payload: req.body });
|
||||
return ok(res, { data });
|
||||
} catch (e) {
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/rr/customer/update", async (req, res) => {
|
||||
try {
|
||||
const bodyshopId = requireBodyshopId(req);
|
||||
const data = await updateCustomer({ bodyshopId, payload: req.body });
|
||||
return ok(res, { data });
|
||||
} catch (e) {
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* NEW: set or create the selected RR customer for a given job
|
||||
* body: { jobid: uuid, selectedCustomerId?: string, bodyshopId?: uuid }
|
||||
*/
|
||||
router.post("/rr/customer/selected", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const logger = (level, message, ctx) => RRLogger(socket)(level, message, ctx);
|
||||
try {
|
||||
const { jobid, selectedCustomerId } = req.body || {};
|
||||
if (!jobid) throw new RrApiError("Missing 'jobid' in body", "BAD_REQUEST");
|
||||
|
||||
// We allow bodyshopId in the body, but will resolve from JobData if not present.
|
||||
const bodyshopId = req.body?.bodyshopId || null;
|
||||
|
||||
const result = await SelectedCustomer({
|
||||
socket,
|
||||
jobid,
|
||||
bodyshopId,
|
||||
selectedCustomerId,
|
||||
redisHelpers: req.redisHelpers
|
||||
});
|
||||
|
||||
logger("info", "RR /rr/customer/selected success", { jobid, selectedCustomerId: result.selectedCustomerId });
|
||||
return ok(res, { data: result });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/customer/selected failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* NEW: fetch canonical JobData used for DMS exports (mirrors Fortellis/CDK QueryJobData)
|
||||
* body: { jobid: uuid }
|
||||
*/
|
||||
router.post("/rr/job/query", async (req, res) => {
|
||||
try {
|
||||
const { jobid } = req.body || {};
|
||||
if (!jobid) throw new RrApiError("Missing 'jobid' in body", "BAD_REQUEST");
|
||||
const data = await QueryJobData({ socket: socketOf(req), jobid });
|
||||
return ok(res, { data });
|
||||
} catch (e) {
|
||||
RRLogger(socket)("error", "RR /rr/lookup/combined-search failed", { error: e.message });
|
||||
return fail(res, e);
|
||||
}
|
||||
});
|
||||
|
||||
// --- export orchestrator ---
|
||||
|
||||
router.post("/rr/export/job", async (req, res) => {
|
||||
const socket = socketOf(req);
|
||||
const logger = (level, message, ctx) => RRLogger(socket)(level, message, ctx);
|
||||
|
||||
Reference in New Issue
Block a user