feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -1,76 +1,144 @@
|
||||
// server/rr/rr-repair-orders.js
|
||||
// -----------------------------------------------------------------------------
|
||||
// RR Repair Order create/update wired through MakeRRCall.
|
||||
// Mapping comes from rr-mappers.js and response validation via rr-error.js.
|
||||
//
|
||||
// What’s still missing (complete when you wire to the PDFs):
|
||||
// - Final RR request envelopes & field names in rr-mappers.js
|
||||
// (Create: “RepairOrderAddRq”, Update: “RepairOrderChgRq”, etc.)
|
||||
// - Definitive success/error envelope checks in rr-error.js (assertRrOk)
|
||||
// - Any RR-required headers (dealer/tenant/site/location ids) in rr-helpers
|
||||
// - If RR requires path params for update (e.g., /repair-orders/{id}),
|
||||
// either add requestPathParams here or move id into RRActions.UpdateRepairOrder
|
||||
// -----------------------------------------------------------------------------
|
||||
/**
|
||||
* @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 { mapRepairOrderAddFromJob, mapRepairOrderChangeFromJob } = require("./rr-mappers");
|
||||
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");
|
||||
|
||||
/**
|
||||
* Create a Repair Order in RR.
|
||||
* 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} deps
|
||||
* @param {Socket|ExpressRequest} deps.socket
|
||||
* @param {Object} deps.redisHelpers
|
||||
* @param {Object} deps.JobData - Rome job (used for mapping)
|
||||
* @param {Object} deps.txEnvelope - Posting/GL context if needed in mapping
|
||||
* @returns {Promise<any>} - RR response (envelope TBD)
|
||||
* @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 }) {
|
||||
// Map JobData (+ optional txEnvelope) -> RR "Repair Order Add" request body
|
||||
const body = mapRepairOrderAddFromJob({ ...JobData, txEnvelope });
|
||||
const bodyshopId = socket?.bodyshopId || JobData?.bodyshopid;
|
||||
const logger = socket?.logger || console;
|
||||
|
||||
const data = await MakeRRCall({
|
||||
...RRActions.CreateRepairOrder, // POST /repair-orders/v1
|
||||
body,
|
||||
redisHelpers,
|
||||
socket,
|
||||
jobid: JobData?.id
|
||||
});
|
||||
try {
|
||||
RRLogger(socket, "info", "RR Create Repair Order started", {
|
||||
jobid: JobData?.id,
|
||||
bodyshopId
|
||||
});
|
||||
|
||||
// TODO: Update assertRrOk once RR’s success envelope is finalized
|
||||
return assertRrOk(data, { apiName: "RR Create Repair Order" });
|
||||
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 a Repair Order in RR.
|
||||
* UPDATE REPAIR ORDER
|
||||
* Based on "Rome Update Body Shop Management Repair Order Specification"
|
||||
*
|
||||
* NOTE: If RR requires the repair order id in the URL (PUT /repair-orders/{id}),
|
||||
* pass it via requestPathParams here once you have it:
|
||||
* requestPathParams: repairOrderId
|
||||
* and ensure RRActions.UpdateRepairOrder.url ends with a trailing slash.
|
||||
*
|
||||
* @param {Object} deps
|
||||
* @param {Socket|ExpressRequest} deps.socket
|
||||
* @param {Object} deps.redisHelpers
|
||||
* @param {Object} deps.JobData - Rome job (used for mapping)
|
||||
* @param {Object} deps.txEnvelope - Posting/GL context if needed in mapping
|
||||
* @param {string|number} [deps.repairOrderId] - If RR expects a path param
|
||||
* @returns {Promise<any>} - RR response (envelope TBD)
|
||||
* @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, repairOrderId }) {
|
||||
const body = mapRepairOrderChangeFromJob({ ...JobData, txEnvelope });
|
||||
async function UpdateRepairOrder({ socket, redisHelpers, JobData, txEnvelope }) {
|
||||
const bodyshopId = socket?.bodyshopId || JobData?.bodyshopid;
|
||||
const logger = socket?.logger || console;
|
||||
|
||||
const data = await MakeRRCall({
|
||||
...RRActions.UpdateRepairOrder, // PUT /repair-orders/v1 (or /v1/{id})
|
||||
...(repairOrderId ? { requestPathParams: String(repairOrderId) } : {}),
|
||||
body,
|
||||
redisHelpers,
|
||||
socket,
|
||||
jobid: JobData?.id
|
||||
});
|
||||
try {
|
||||
RRLogger(socket, "info", "RR Update Repair Order started", {
|
||||
jobid: JobData?.id,
|
||||
bodyshopId,
|
||||
rr_ro_id: JobData?.rr_ro_id
|
||||
});
|
||||
|
||||
return assertRrOk(data, { apiName: "RR Update Repair Order" });
|
||||
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 };
|
||||
module.exports = {
|
||||
CreateRepairOrder,
|
||||
UpdateRepairOrder,
|
||||
getDealerConfigFromDB
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user