feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const dotenv = require("dotenv");
|
||||
const { GraphQLClient, gql } = require("graphql-request");
|
||||
const { MakeRRCall, renderXmlTemplate, buildStarEnvelope } = require("./rr-helpers");
|
||||
const { getBaseRRConfig } = require("./rr-constants");
|
||||
|
||||
@@ -20,7 +21,7 @@ if (fs.existsSync(defaultEnvPath)) {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse CLI args
|
||||
// ---- CLI args parsing ----
|
||||
const argv = process.argv.slice(2);
|
||||
const args = { _: [] };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
@@ -37,13 +38,12 @@ for (let i = 0; i < argv.length; i++) {
|
||||
const next = argv[i + 1];
|
||||
if (next && !next.startsWith("-")) {
|
||||
args[k] = next;
|
||||
i++; // consume value
|
||||
i++;
|
||||
} else {
|
||||
args[k] = true; // boolean flag
|
||||
args[k] = true;
|
||||
}
|
||||
}
|
||||
} else if (a.startsWith("-") && a.length > 1) {
|
||||
// simple short flag handling: -a value
|
||||
const k = a.slice(1);
|
||||
const next = argv[i + 1];
|
||||
if (next && !next.startsWith("-")) {
|
||||
@@ -62,7 +62,65 @@ function toIntOr(defaultVal, maybe) {
|
||||
return Number.isFinite(n) ? n : defaultVal;
|
||||
}
|
||||
|
||||
// ✅ fixed guard clause
|
||||
// ---------------- GraphQL helpers ----------------
|
||||
|
||||
function buildGqlClient() {
|
||||
const endpoint = process.env.GRAPHQL_ENDPOINT;
|
||||
if (!endpoint) throw new Error("GRAPHQL_ENDPOINT env var is required when using --bodyshopId.");
|
||||
|
||||
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}`;
|
||||
}
|
||||
|
||||
return new GraphQLClient(endpoint, { headers });
|
||||
}
|
||||
|
||||
const Q_BODYSHOPS_BY_PK = gql`
|
||||
query BodyshopRR($id: uuid!) {
|
||||
bodyshops_by_pk(id: $id) {
|
||||
rr_dealerid
|
||||
rr_configuration
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
function normalizeConfigRecord(rec) {
|
||||
if (!rec) return null;
|
||||
let cfg = rec.rr_configuration || {};
|
||||
if (typeof cfg === "string") {
|
||||
try {
|
||||
cfg = JSON.parse(cfg);
|
||||
} catch {
|
||||
cfg = {};
|
||||
}
|
||||
}
|
||||
return {
|
||||
dealerNumber: rec.rr_dealerid || undefined,
|
||||
storeNumber: cfg.storeNumber || undefined,
|
||||
branchNumber: cfg.branchNumber || undefined
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load RR config overrides from DB (bodyshops_by_pk only).
|
||||
*/
|
||||
async function loadBodyshopRRConfig(bodyshopId) {
|
||||
if (!bodyshopId) return null;
|
||||
|
||||
const client = buildGqlClient();
|
||||
const { bodyshops_by_pk: bs } = await client.request(Q_BODYSHOPS_BY_PK, { 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).");
|
||||
|
||||
return normalizeConfigRecord(bs);
|
||||
}
|
||||
|
||||
// ---------------- rr-test logic ----------------
|
||||
|
||||
function pickActionName(raw) {
|
||||
if (!raw || typeof raw !== "string") return "ping";
|
||||
const x = raw.toLowerCase();
|
||||
@@ -142,6 +200,8 @@ function buildBodyForAction(action, args, cfg) {
|
||||
|
||||
async function main() {
|
||||
const action = pickActionName(args.action || args.a || args._[0]);
|
||||
const bodyshopId = args.bodyshopId || args.bodyshop || args.b;
|
||||
|
||||
const rrAction =
|
||||
action === "ping"
|
||||
? "GetAdvisors"
|
||||
@@ -153,12 +213,28 @@ async function main() {
|
||||
? "GetParts"
|
||||
: action;
|
||||
|
||||
// Start with env-based defaults…
|
||||
const cfg = getBaseRRConfig();
|
||||
|
||||
// …then override with per-bodyshop values if provided.
|
||||
if (bodyshopId) {
|
||||
try {
|
||||
const overrides = await loadBodyshopRRConfig(bodyshopId);
|
||||
if (overrides?.dealerNumber) cfg.dealerNumber = overrides.dealerNumber;
|
||||
if (overrides?.storeNumber) cfg.storeNumber = overrides.storeNumber;
|
||||
if (overrides?.branchNumber) cfg.branchNumber = overrides.branchNumber;
|
||||
console.log("ℹ️ RR config loaded from DB via: bodyshops_by_pk");
|
||||
} catch (e) {
|
||||
console.error("❌ Failed to load per-bodyshop RR config:", e.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const body = buildBodyForAction(action, args, cfg);
|
||||
const templateName = body.template || rrAction;
|
||||
|
||||
try {
|
||||
const xml = await renderXmlTemplate(templateName, body.data);
|
||||
await renderXmlTemplate(templateName, body.data);
|
||||
console.log("✅ Templates verified.");
|
||||
} catch (e) {
|
||||
console.error("❌ Template verification failed:", e.message);
|
||||
@@ -186,6 +262,4 @@ async function main() {
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
process.exit(1);
|
||||
});
|
||||
main().catch(() => process.exit(1));
|
||||
|
||||
Reference in New Issue
Block a user