feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
* - Parses XML response (faults + STAR payload result)
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs/promises");
|
||||
const path = require("path");
|
||||
const axios = require("axios");
|
||||
@@ -68,7 +70,7 @@ async function loadTemplate(templateName) {
|
||||
async function renderXmlTemplate(templateName, data) {
|
||||
const tpl = await loadTemplate(templateName);
|
||||
// Render and strip any XML declaration to keep a single root element for the BOD
|
||||
const rendered = mustache.render(tpl, data || {});
|
||||
const rendered = mustache.render(tpl, { STAR_NS: RR_NS.STAR_BUSINESS, ...(data || {}) });
|
||||
return rendered.replace(/^\s*<\?xml[^>]*\?>\s*/i, "");
|
||||
}
|
||||
|
||||
@@ -202,12 +204,26 @@ function wrapWithApplicationArea(innerXml, { CreationDateTime, BODId, Sender, De
|
||||
return xml;
|
||||
}
|
||||
|
||||
async function buildStarEnvelope(innerBusinessXml, creds, appArea = {}) {
|
||||
const TASK_BY_ACTION = {
|
||||
CreateRepairOrder: { Task: "BSMRO", ReferenceId: "Insert" },
|
||||
UpdateRepairOrder: { Task: "BSMRO", ReferenceId: "Update" },
|
||||
InsertCustomer: { Task: "CU", ReferenceId: "Insert" },
|
||||
UpdateCustomer: { Task: "CU", ReferenceId: "Update" },
|
||||
InsertServiceVehicle: { Task: "SV", ReferenceId: "Insert" }
|
||||
};
|
||||
|
||||
async function buildStarEnvelope(innerBusinessXml, creds, appArea = {}, action) {
|
||||
const now = new Date().toISOString();
|
||||
|
||||
// Derive sensible defaults for Sender from action, unless caller provided explicit values
|
||||
const senderDefaults =
|
||||
appArea?.Sender ||
|
||||
(action && TASK_BY_ACTION[action] ? { Component: "Rome", ...TASK_BY_ACTION[action] } : { Component: "Rome" });
|
||||
|
||||
const payloadWithAppArea = wrapWithApplicationArea(innerBusinessXml, {
|
||||
CreationDateTime: appArea.CreationDateTime || now,
|
||||
BODId: appArea.BODId || `BOD-${Date.now()}`,
|
||||
Sender: appArea.Sender || { Component: "Rome", Task: "SV", ReferenceId: "Update" },
|
||||
Sender: senderDefaults,
|
||||
Destination: appArea.Destination || {
|
||||
DealerNumber: creds.dealerNumber,
|
||||
StoreNumber: String(creds.storeNumber ?? "").padStart(2, "0"),
|
||||
@@ -241,6 +257,7 @@ async function buildStarEnvelope(innerBusinessXml, creds, appArea = {}) {
|
||||
async function MakeRRCall({
|
||||
action,
|
||||
body,
|
||||
appArea, // <-- allow explicit ApplicationArea overrides at the top level
|
||||
socket,
|
||||
dealerConfig, // required in runtime code; rr-test.js can still pass env-inflated cfg
|
||||
retries = 1,
|
||||
@@ -259,8 +276,9 @@ async function MakeRRCall({
|
||||
const templateName = body?.template || action;
|
||||
const renderedBusiness = await renderXmlTemplate(templateName, body?.data || {});
|
||||
|
||||
// Build STAR envelope
|
||||
const envelope = await buildStarEnvelope(renderedBusiness, cfg, body?.appArea);
|
||||
// Build STAR envelope (use explicit appArea if provided; else accept body.appArea; else derive from action)
|
||||
const selectedAppArea = appArea || body?.appArea || {};
|
||||
const envelope = await buildStarEnvelope(renderedBusiness, cfg, selectedAppArea, action);
|
||||
const formattedEnvelope = prettyPrintXml(envelope);
|
||||
|
||||
// Guardrails
|
||||
@@ -310,6 +328,7 @@ async function MakeRRCall({
|
||||
return MakeRRCall({
|
||||
action,
|
||||
body,
|
||||
appArea: selectedAppArea,
|
||||
socket,
|
||||
dealerConfig: cfg,
|
||||
retries: retries - 1,
|
||||
|
||||
Reference in New Issue
Block a user