feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -1,144 +1,99 @@
|
||||
/**
|
||||
* @file rr-repair-orders.js
|
||||
* @description Reynolds & Reynolds (Rome) Repair Order Create & Update.
|
||||
* Implements the "Create Body Shop Management Repair Order" and
|
||||
* "Update Body Shop Management Repair Order" specifications.
|
||||
* @description Rome (Reynolds & Reynolds) Repair Order Integration.
|
||||
* Handles creation and updates of repair orders (BSMRepairOrderRq/Resp).
|
||||
*/
|
||||
|
||||
const { MakeRRCall, RRActions } = require("./rr-helpers");
|
||||
const { assertRrOk } = require("./rr-error");
|
||||
const { MakeRRCall } = require("./rr-helpers");
|
||||
const { mapRepairOrderCreate, mapRepairOrderUpdate } = require("./rr-mappers");
|
||||
const RRLogger = require("./rr-logger");
|
||||
const { client } = require("../graphql-client/graphql-client");
|
||||
const { GET_BODYSHOP_BY_ID } = require("../graphql-client/queries");
|
||||
const { RrApiError } = require("./rr-error");
|
||||
|
||||
/**
|
||||
* Fetch rr_configuration for the current bodyshop directly from DB.
|
||||
* Dealer-specific configuration is mandatory for RR operations.
|
||||
* Create a new repair order in Rome.
|
||||
* @param {Socket} socket - active socket connection
|
||||
* @param {Object} job - Hasura job object (including vehicle, customer, joblines)
|
||||
* @param {Object} bodyshopConfig - DMS config for current bodyshop
|
||||
* @returns {Promise<Object>} normalized result
|
||||
*/
|
||||
async function getDealerConfigFromDB(bodyshopId, logger) {
|
||||
async function createRepairOrder(socket, job, bodyshopConfig) {
|
||||
const action = "CreateRepairOrder";
|
||||
const template = "CreateRepairOrder"; // maps to xml-templates/CreateRepairOrder.xml
|
||||
|
||||
try {
|
||||
const result = await client.request(GET_BODYSHOP_BY_ID, { id: bodyshopId });
|
||||
const config = result?.bodyshops_by_pk?.rr_configuration || null;
|
||||
RRLogger(socket, "info", `Starting RR ${action} for job ${job.id}`);
|
||||
|
||||
if (!config) {
|
||||
throw new Error(`No rr_configuration found for bodyshop ID ${bodyshopId}`);
|
||||
}
|
||||
const data = mapRepairOrderCreate(job, bodyshopConfig);
|
||||
|
||||
logger?.debug?.(`Fetched rr_configuration for bodyshop ${bodyshopId}`, config);
|
||||
return config;
|
||||
const resultXml = await MakeRRCall({
|
||||
action,
|
||||
body: { template, data },
|
||||
socket,
|
||||
dealerConfig: bodyshopConfig,
|
||||
jobid: job.id
|
||||
});
|
||||
|
||||
RRLogger(socket, "debug", `${action} completed successfully`, { jobid: job.id });
|
||||
|
||||
return {
|
||||
success: true,
|
||||
dms: "Rome",
|
||||
jobid: job.id,
|
||||
action,
|
||||
xml: resultXml
|
||||
};
|
||||
} catch (error) {
|
||||
logger?.log?.("rr-get-dealer-config", "ERROR", "rr", null, {
|
||||
bodyshopId,
|
||||
RRLogger(socket, "error", `Error in ${action} for job ${job.id}`, {
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
throw error;
|
||||
throw new RrApiError(`RR CreateRepairOrder failed: ${error.message}`, "CREATE_RO_ERROR");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CREATE REPAIR ORDER
|
||||
* Based on "Rome Create Body Shop Management Repair Order Specification"
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {object} options.socket - socket or express request
|
||||
* @param {object} options.redisHelpers
|
||||
* @param {object} options.JobData - internal job object
|
||||
* @param {object} [options.txEnvelope] - transaction metadata (advisor, timestamps, etc.)
|
||||
* Update an existing repair order in Rome.
|
||||
* @param {Socket} socket
|
||||
* @param {Object} job
|
||||
* @param {Object} bodyshopConfig
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
async function CreateRepairOrder({ socket, redisHelpers, JobData, txEnvelope }) {
|
||||
const bodyshopId = socket?.bodyshopId || JobData?.bodyshopid;
|
||||
const logger = socket?.logger || console;
|
||||
async function updateRepairOrder(socket, job, bodyshopConfig) {
|
||||
const action = "UpdateRepairOrder";
|
||||
const template = "UpdateRepairOrder";
|
||||
|
||||
try {
|
||||
RRLogger(socket, "info", "RR Create Repair Order started", {
|
||||
jobid: JobData?.id,
|
||||
bodyshopId
|
||||
});
|
||||
RRLogger(socket, "info", `Starting RR ${action} for job ${job.id}`);
|
||||
|
||||
const dealerConfig = await getDealerConfigFromDB(bodyshopId, logger);
|
||||
const data = mapRepairOrderUpdate(job, bodyshopConfig);
|
||||
|
||||
// Build Mustache variables for server/rr/xml-templates/CreateRepairOrder.xml
|
||||
const vars = mapRepairOrderCreate({ JobData, txEnvelope, dealerConfig });
|
||||
|
||||
const data = await MakeRRCall({
|
||||
action: RRActions.CreateRepairOrder, // resolves SOAPAction+URL
|
||||
body: { template: "CreateRepairOrder", data: vars }, // render XML template
|
||||
redisHelpers,
|
||||
const resultXml = await MakeRRCall({
|
||||
action,
|
||||
body: { template, data },
|
||||
socket,
|
||||
jobid: JobData.id
|
||||
dealerConfig: bodyshopConfig,
|
||||
jobid: job.id
|
||||
});
|
||||
|
||||
const response = assertRrOk(data, { apiName: "RR Create Repair Order" });
|
||||
RRLogger(socket, "debug", `${action} completed successfully`, { jobid: job.id });
|
||||
|
||||
RRLogger(socket, "debug", "RR Create Repair Order success", {
|
||||
jobid: JobData?.id,
|
||||
dealer: dealerConfig?.dealer_code || dealerConfig?.dealerCode
|
||||
});
|
||||
|
||||
return response;
|
||||
return {
|
||||
success: true,
|
||||
dms: "Rome",
|
||||
jobid: job.id,
|
||||
action,
|
||||
xml: resultXml
|
||||
};
|
||||
} catch (error) {
|
||||
RRLogger(socket, "error", `RR Create Repair Order failed: ${error.message}`, {
|
||||
jobid: JobData?.id
|
||||
RRLogger(socket, "error", `Error in ${action} for job ${job.id}`, {
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UPDATE REPAIR ORDER
|
||||
* Based on "Rome Update Body Shop Management Repair Order Specification"
|
||||
*
|
||||
* @param {object} options
|
||||
* @param {object} options.socket
|
||||
* @param {object} options.redisHelpers
|
||||
* @param {object} options.JobData
|
||||
* @param {object} [options.txEnvelope]
|
||||
*/
|
||||
async function UpdateRepairOrder({ socket, redisHelpers, JobData, txEnvelope }) {
|
||||
const bodyshopId = socket?.bodyshopId || JobData?.bodyshopid;
|
||||
const logger = socket?.logger || console;
|
||||
|
||||
try {
|
||||
RRLogger(socket, "info", "RR Update Repair Order started", {
|
||||
jobid: JobData?.id,
|
||||
bodyshopId,
|
||||
rr_ro_id: JobData?.rr_ro_id
|
||||
});
|
||||
|
||||
const dealerConfig = await getDealerConfigFromDB(bodyshopId, logger);
|
||||
|
||||
// Build Mustache variables for server/rr/xml-templates/UpdateRepairOrder.xml
|
||||
const vars = mapRepairOrderUpdate({ JobData, txEnvelope, dealerConfig });
|
||||
|
||||
const data = await MakeRRCall({
|
||||
action: RRActions.UpdateRepairOrder, // resolves SOAPAction+URL
|
||||
body: { template: "UpdateRepairOrder", data: vars }, // render XML template
|
||||
redisHelpers,
|
||||
socket,
|
||||
jobid: JobData.id
|
||||
});
|
||||
|
||||
const response = assertRrOk(data, { apiName: "RR Update Repair Order" });
|
||||
|
||||
RRLogger(socket, "debug", "RR Update Repair Order success", {
|
||||
jobid: JobData?.id,
|
||||
rr_ro_id: JobData?.rr_ro_id
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
RRLogger(socket, "error", `RR Update Repair Order failed: ${error.message}`, {
|
||||
jobid: JobData?.id,
|
||||
rr_ro_id: JobData?.rr_ro_id
|
||||
});
|
||||
throw error;
|
||||
throw new RrApiError(`RR UpdateRepairOrder failed: ${error.message}`, "UPDATE_RO_ERROR");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CreateRepairOrder,
|
||||
UpdateRepairOrder,
|
||||
getDealerConfigFromDB
|
||||
createRepairOrder,
|
||||
updateRepairOrder
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user