169 lines
5.5 KiB
JavaScript
169 lines
5.5 KiB
JavaScript
// 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 lookupApi = require("./rr-lookup");
|
|
const { exportJobToRR } = require("./rr-job-export");
|
|
|
|
// --- 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 socketOf(req) {
|
|
try {
|
|
return req.app?.get?.("socket") || null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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 (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);
|
|
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 });
|
|
} 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 });
|
|
} 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 });
|
|
} 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);
|
|
try {
|
|
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);
|
|
}
|
|
});
|
|
|
|
// --- export orchestrator ---
|
|
|
|
router.post("/rr/export/job", async (req, res) => {
|
|
const socket = socketOf(req);
|
|
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 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);
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|