145 lines
4.5 KiB
JavaScript
145 lines
4.5 KiB
JavaScript
/**
|
|
* @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.
|
|
*/
|
|
|
|
const { MakeRRCall, RRActions } = require("./rr-helpers");
|
|
const { assertRrOk } = require("./rr-error");
|
|
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");
|
|
|
|
/**
|
|
* Fetch rr_configuration for the current bodyshop directly from DB.
|
|
* Dealer-specific configuration is mandatory for RR operations.
|
|
*/
|
|
async function getDealerConfigFromDB(bodyshopId, logger) {
|
|
try {
|
|
const result = await client.request(GET_BODYSHOP_BY_ID, { id: bodyshopId });
|
|
const config = result?.bodyshops_by_pk?.rr_configuration || null;
|
|
|
|
if (!config) {
|
|
throw new Error(`No rr_configuration found for bodyshop ID ${bodyshopId}`);
|
|
}
|
|
|
|
logger?.debug?.(`Fetched rr_configuration for bodyshop ${bodyshopId}`, config);
|
|
return config;
|
|
} catch (error) {
|
|
logger?.log?.("rr-get-dealer-config", "ERROR", "rr", null, {
|
|
bodyshopId,
|
|
message: error.message,
|
|
stack: error.stack
|
|
});
|
|
throw 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.)
|
|
*/
|
|
async function CreateRepairOrder({ socket, redisHelpers, JobData, txEnvelope }) {
|
|
const bodyshopId = socket?.bodyshopId || JobData?.bodyshopid;
|
|
const logger = socket?.logger || console;
|
|
|
|
try {
|
|
RRLogger(socket, "info", "RR Create Repair Order started", {
|
|
jobid: JobData?.id,
|
|
bodyshopId
|
|
});
|
|
|
|
const dealerConfig = await getDealerConfigFromDB(bodyshopId, logger);
|
|
|
|
// 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,
|
|
socket,
|
|
jobid: JobData.id
|
|
});
|
|
|
|
const response = assertRrOk(data, { apiName: "RR Create Repair Order" });
|
|
|
|
RRLogger(socket, "debug", "RR Create Repair Order success", {
|
|
jobid: JobData?.id,
|
|
dealer: dealerConfig?.dealer_code || dealerConfig?.dealerCode
|
|
});
|
|
|
|
return response;
|
|
} catch (error) {
|
|
RRLogger(socket, "error", `RR Create Repair Order failed: ${error.message}`, {
|
|
jobid: JobData?.id
|
|
});
|
|
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;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
CreateRepairOrder,
|
|
UpdateRepairOrder,
|
|
getDealerConfigFromDB
|
|
};
|