138 lines
4.6 KiB
JavaScript
138 lines
4.6 KiB
JavaScript
/**
|
|
* @file rr-customer.js
|
|
* @description Reynolds & Reynolds (Rome) Customer Insert/Update integration.
|
|
* Builds request payloads using rr-mappers and executes via rr-helpers.
|
|
* All dealer-specific data (DealerNumber, LocationId, etc.) is read from the DB (bodyshop.rr_configuration).
|
|
*/
|
|
|
|
const { MakeRRCall, RRActions } = require("./rr-helpers");
|
|
const { assertRrOk } = require("./rr-error");
|
|
const { mapCustomerInsert, mapCustomerUpdate } = 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.
|
|
* This ensures we always have the latest Dealer/Location mapping.
|
|
*/
|
|
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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* CUSTOMER INSERT (Rome Customer Insert Specification 1.2)
|
|
* Creates a new customer record in the DMS.
|
|
*
|
|
* @param {object} options
|
|
* @param {object} options.socket - socket.io connection or express req
|
|
* @param {object} options.redisHelpers
|
|
* @param {object} options.JobData - normalized job record
|
|
*/
|
|
async function RrCustomerInsert({ socket, redisHelpers, JobData }) {
|
|
const bodyshopId = socket?.bodyshopId || JobData?.bodyshopid;
|
|
const logger = socket?.logger || console;
|
|
|
|
try {
|
|
RRLogger(socket, "info", "RR Customer Insert started", { jobid: JobData?.id, bodyshopId });
|
|
|
|
const dealerConfig = await getDealerConfigFromDB(bodyshopId, logger);
|
|
|
|
// Build Mustache variables for the InsertCustomer.xml template
|
|
const vars = mapCustomerInsert(JobData, dealerConfig);
|
|
|
|
const data = await MakeRRCall({
|
|
action: RRActions.CreateCustomer, // resolves to SOAPAction + URL
|
|
body: { template: "InsertCustomer", data: vars }, // render server/rr/xml-templates/InsertCustomer.xml
|
|
redisHelpers,
|
|
socket,
|
|
jobid: JobData.id
|
|
});
|
|
|
|
const response = assertRrOk(data, { apiName: "RR Create Customer" });
|
|
RRLogger(socket, "debug", "RR Customer Insert success", {
|
|
jobid: JobData?.id,
|
|
dealer: dealerConfig?.dealerCode || dealerConfig?.dealer_code
|
|
});
|
|
|
|
return response;
|
|
} catch (error) {
|
|
RRLogger(socket, "error", `RR Customer Insert failed: ${error.message}`, { jobid: JobData?.id });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* CUSTOMER UPDATE (Rome Customer Update Specification 1.2)
|
|
* Updates an existing RR customer record.
|
|
*
|
|
* @param {object} options
|
|
* @param {object} options.socket
|
|
* @param {object} options.redisHelpers
|
|
* @param {object} options.JobData
|
|
* @param {object} options.existingCustomer - current RR customer record (from Combined Search)
|
|
* @param {object} options.patch - updated fields from frontend
|
|
*/
|
|
async function RrCustomerUpdate({ socket, redisHelpers, JobData, existingCustomer, patch }) {
|
|
const bodyshopId = socket?.bodyshopId || JobData?.bodyshopid;
|
|
const logger = socket?.logger || console;
|
|
|
|
try {
|
|
RRLogger(socket, "info", "RR Customer Update started", {
|
|
jobid: JobData?.id,
|
|
bodyshopId,
|
|
existingCustomerId: existingCustomer?.CustomerId
|
|
});
|
|
|
|
const dealerConfig = await getDealerConfigFromDB(bodyshopId, logger);
|
|
|
|
// Build Mustache variables for the UpdateCustomer.xml template
|
|
const vars = mapCustomerUpdate(existingCustomer, patch, dealerConfig);
|
|
|
|
const data = await MakeRRCall({
|
|
action: RRActions.UpdateCustomer, // resolves to SOAPAction + URL
|
|
body: { template: "UpdateCustomer", data: vars }, // render server/rr/xml-templates/UpdateCustomer.xml
|
|
redisHelpers,
|
|
socket,
|
|
jobid: JobData.id
|
|
});
|
|
|
|
const response = assertRrOk(data, { apiName: "RR Update Customer" });
|
|
RRLogger(socket, "debug", "RR Customer Update success", {
|
|
jobid: JobData?.id,
|
|
customerId: existingCustomer?.CustomerId
|
|
});
|
|
|
|
return response;
|
|
} catch (error) {
|
|
RRLogger(socket, "error", `RR Customer Update failed: ${error.message}`, {
|
|
jobid: JobData?.id,
|
|
customerId: existingCustomer?.CustomerId
|
|
});
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
RrCustomerInsert,
|
|
RrCustomerUpdate,
|
|
getDealerConfigFromDB
|
|
};
|