feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
81
server/rr/rr-config.js
Normal file
81
server/rr/rr-config.js
Normal file
@@ -0,0 +1,81 @@
|
||||
// server/rr/rr-config.js
|
||||
const { GraphQLClient, gql } = require("graphql-request");
|
||||
|
||||
/**
|
||||
* Fetch the bodyshop row (dealer + per-shop RR json).
|
||||
* No fallback to env for dealer/store/branch.
|
||||
*/
|
||||
async function fetchBodyshopRRRow(bodyshopId) {
|
||||
if (!bodyshopId) throw new Error("Missing bodyshopId for RR config.");
|
||||
|
||||
const endpoint = process.env.GRAPHQL_ENDPOINT;
|
||||
if (!endpoint) throw new Error("GRAPHQL_ENDPOINT env var is required.");
|
||||
|
||||
const headers = {};
|
||||
if (process.env.HASURA_ADMIN_SECRET) {
|
||||
headers["x-hasura-admin-secret"] = process.env.HASURA_ADMIN_SECRET;
|
||||
} else if (process.env.GRAPHQL_BEARER) {
|
||||
headers["authorization"] = `Bearer ${process.env.GRAPHQL_BEARER}`;
|
||||
}
|
||||
|
||||
const client = new GraphQLClient(endpoint, { headers });
|
||||
|
||||
const Q = gql`
|
||||
query BodyshopRR($id: uuid!) {
|
||||
bodyshops_by_pk(id: $id) {
|
||||
rr_dealerid
|
||||
rr_configuration
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const { bodyshops_by_pk: bs } = await client.request(Q, { id: bodyshopId });
|
||||
if (!bs) throw new Error("Bodyshop not found.");
|
||||
if (!bs.rr_dealerid) throw new Error("Bodyshop is not configured for RR (missing rr_dealerid).");
|
||||
|
||||
let cfgJson = bs.rr_configuration || {};
|
||||
if (typeof cfgJson === "string") {
|
||||
try {
|
||||
cfgJson = JSON.parse(cfgJson);
|
||||
} catch {
|
||||
cfgJson = {};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
dealerNumber: bs.rr_dealerid,
|
||||
storeNumber: cfgJson.storeNumber,
|
||||
branchNumber: cfgJson.branchNumber
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the full RR config object used by downstream code.
|
||||
* - Dealer/Store/Branch: from DB (required)
|
||||
* - Transport/BaseURL/creds/PPSysId: from env (deployment-wide)
|
||||
*/
|
||||
async function getRRConfigForBodyshop(bodyshopId) {
|
||||
const { dealerNumber, storeNumber, branchNumber } = await fetchBodyshopRRRow(bodyshopId);
|
||||
|
||||
const rrTransport = (process.env.RR_TRANSPORT || "STAR").toUpperCase();
|
||||
return {
|
||||
// Per-bodyshop (DB)
|
||||
dealerNumber,
|
||||
storeNumber,
|
||||
branchNumber,
|
||||
|
||||
// Duplicate snake_case for legacy call-sites that expect it
|
||||
dealer_number: dealerNumber,
|
||||
store_number: storeNumber,
|
||||
branch_number: branchNumber,
|
||||
|
||||
// Deployment-wide (env)
|
||||
baseUrl: process.env.RR_BASE_URL,
|
||||
username: process.env.RR_USERNAME,
|
||||
password: process.env.RR_PASSWORD,
|
||||
ppsysId: process.env.RR_PPSYSID,
|
||||
rrTransport
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { getRRConfigForBodyshop };
|
||||
Reference in New Issue
Block a user