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:
Dave
2025-10-29 10:42:32 -04:00
parent 319f3220ed
commit e06f0f9918
36 changed files with 2468 additions and 3736 deletions

View File

@@ -1,81 +1,63 @@
// server/rr/rr-config.js
/**
* Loads per-bodyshop RR routing from Hasura.
* Expected table fields (adapt if your schema differs):
* - bodyshops.id = $bodyshopId
* - bodyshops.rr_dealerid (string)
* - bodyshops.rr_configuration JSON { storeNumber?, branchNumber? }
*
* Requires env:
* HASURA_GRAPHQL_ENDPOINT, HASURA_ADMIN_SECRET
*/
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 HASURA_URL = process.env.HASURA_GRAPHQL_ENDPOINT || process.env.HASURA_URL;
const HASURA_SECRET = process.env.HASURA_ADMIN_SECRET || process.env.HASURA_GRAPHQL_ADMIN_SECRET;
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
};
if (!HASURA_URL || !HASURA_SECRET) {
// Warn loudly at startup; you can hard fail if you prefer
console.warn("[RR] HASURA env not set (HASURA_GRAPHQL_ENDPOINT / HASURA_ADMIN_SECRET).");
}
const client = HASURA_URL
? new GraphQLClient(HASURA_URL, {
headers: { "x-hasura-admin-secret": HASURA_SECRET }
})
: null;
const Q_BODYSHOP_RR = gql`
query RR_Config($id: uuid!) {
bodyshops_by_pk(id: $id) {
id
rr_dealerid
rr_configuration
}
}
`;
/**
* Build the full RR config object used by downstream code.
* - Dealer/Store/Branch: from DB (required)
* - Transport/BaseURL/creds/PPSysId: from env (deployment-wide)
* @param {string} bodyshopId
* @returns {Promise<{dealerNumber:string, storeNumber?:string, areaNumber?:string}>}
*/
async function getRRConfigForBodyshop(bodyshopId) {
const { dealerNumber, storeNumber, branchNumber } = await fetchBodyshopRRRow(bodyshopId);
if (!client) throw new Error("Hasura client not configured.");
const rrTransport = (process.env.RR_TRANSPORT || "STAR").toUpperCase();
return {
// Per-bodyshop (DB)
dealerNumber,
storeNumber,
branchNumber,
const data = await client.request(Q_BODYSHOP_RR, { id: bodyshopId });
const row = data?.bodyshops_by_pk;
if (!row) throw new Error(`Bodyshop not found: ${bodyshopId}`);
// Duplicate snake_case for legacy call-sites that expect it
dealer_number: dealerNumber,
store_number: storeNumber,
branch_number: branchNumber,
const dealerNumber = row.rr_dealerid;
// 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
};
const cfg = row.rr_configuration || {};
if (!dealerNumber) {
throw new Error(`RR not configured for bodyshop ${bodyshopId} (missing rr_dealerid).`);
}
// The RR client expects "areaNumber" (Rome "branch")
const storeNumber = cfg.storeNumber || cfg.store_no || cfg.store || null;
const areaNumber = cfg.branchNumber || cfg.branch_no || cfg.branch || null;
return { dealerNumber, storeNumber, areaNumber };
}
module.exports = { getRRConfigForBodyshop };