33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
const { getRRConfigForBodyshop } = require("./rr-config");
|
|
const { RrApiError } = require("./rr-error");
|
|
|
|
/**
|
|
* Extracts bodyshopId from body, job, or header and loads RR config.
|
|
* @returns {Promise<{ bodyshopId: string, config: any }>}
|
|
*/
|
|
async function resolveRRConfigHttp(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/bodyshopId, or x-bodyshop-id header)",
|
|
"BAD_REQUEST"
|
|
);
|
|
}
|
|
|
|
const config = await getRRConfigForBodyshop(bodyshopId);
|
|
if (!config?.dealerNumber) {
|
|
throw new RrApiError(`RR config not found for bodyshopId=${bodyshopId} (missing dealerNumber)`, "NOT_CONFIGURED");
|
|
}
|
|
|
|
return { bodyshopId, config };
|
|
}
|
|
|
|
module.exports = { resolveRRConfigHttp };
|