137 lines
4.4 KiB
JavaScript
137 lines
4.4 KiB
JavaScript
/**
|
|
* @file rr-lookup.js
|
|
* @description Rome (Reynolds & Reynolds) lookup operations — Advisors, Parts, and CombinedSearch
|
|
*/
|
|
|
|
const { MakeRRCall, parseRRResponse } = require("./rr-helpers");
|
|
const { mapAdvisorLookup, mapPartsLookup, mapCombinedSearch } = require("./rr-mappers");
|
|
const RRLogger = require("./rr-logger");
|
|
const { RrApiError } = require("./rr-error");
|
|
|
|
/**
|
|
* Get a list of service advisors from Rome.
|
|
*/
|
|
async function getAdvisors(socket, criteria = {}, bodyshopConfig) {
|
|
const action = "GetAdvisors";
|
|
const template = "GetAdvisors";
|
|
|
|
try {
|
|
RRLogger(socket, "info", `Starting RR ${action} lookup`);
|
|
const data = mapAdvisorLookup(criteria, bodyshopConfig);
|
|
|
|
const resultXml = await MakeRRCall({
|
|
action,
|
|
body: { template, data },
|
|
socket,
|
|
dealerConfig: bodyshopConfig
|
|
});
|
|
|
|
const parsed = parseRRResponse(resultXml);
|
|
if (!parsed.success) throw new RrApiError(parsed.message, parsed.code);
|
|
|
|
const advisors = parsed.parsed?.Advisors?.Advisor || parsed.parsed?.AdvisorList?.Advisor || [];
|
|
const advisorList = Array.isArray(advisors) ? advisors : [advisors];
|
|
|
|
RRLogger(socket, "debug", `${action} lookup returned ${advisorList.length} advisors`);
|
|
return { success: true, dms: "Rome", action, advisors: advisorList };
|
|
} catch (error) {
|
|
RRLogger(socket, "error", `Error in ${action} lookup`, {
|
|
message: error.message,
|
|
stack: error.stack
|
|
});
|
|
throw new RrApiError(`RR ${action} failed: ${error.message}`, "GET_ADVISORS_ERROR");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get parts information from Rome.
|
|
*/
|
|
async function getParts(socket, criteria = {}, bodyshopConfig) {
|
|
const action = "GetParts";
|
|
const template = "GetParts";
|
|
|
|
try {
|
|
RRLogger(socket, "info", `Starting RR ${action} lookup`);
|
|
const data = mapPartsLookup(criteria, bodyshopConfig);
|
|
|
|
const resultXml = await MakeRRCall({
|
|
action,
|
|
body: { template, data },
|
|
socket,
|
|
dealerConfig: bodyshopConfig
|
|
});
|
|
|
|
const parsed = parseRRResponse(resultXml);
|
|
if (!parsed.success) throw new RrApiError(parsed.message, parsed.code);
|
|
|
|
const parts = parsed.parsed?.Parts?.Part || parsed.parsed?.PartList?.Part || [];
|
|
const partList = Array.isArray(parts) ? parts : [parts];
|
|
|
|
RRLogger(socket, "debug", `${action} lookup returned ${partList.length} parts`);
|
|
return { success: true, dms: "Rome", action, parts: partList };
|
|
} catch (error) {
|
|
RRLogger(socket, "error", `Error in ${action} lookup`, {
|
|
message: error.message,
|
|
stack: error.stack
|
|
});
|
|
throw new RrApiError(`RR ${action} failed: ${error.message}`, "GET_PARTS_ERROR");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Perform a combined customer / vehicle / company search.
|
|
* Equivalent to Rome CombinedSearchRq / Resp.
|
|
* @param {Socket} socket
|
|
* @param {Object} criteria - { VIN, LicensePlate, CustomerName, Phone, Email }
|
|
* @param {Object} bodyshopConfig
|
|
* @returns {Promise<Object>} { customers, vehicles, companies }
|
|
*/
|
|
async function combinedSearch(socket, criteria = {}, bodyshopConfig) {
|
|
const action = "CombinedSearch";
|
|
const template = "CombinedSearch";
|
|
|
|
try {
|
|
RRLogger(socket, "info", `Starting RR ${action} request`);
|
|
const data = mapCombinedSearch(criteria, bodyshopConfig);
|
|
|
|
const resultXml = await MakeRRCall({
|
|
action,
|
|
body: { template, data },
|
|
socket,
|
|
dealerConfig: bodyshopConfig
|
|
});
|
|
|
|
const parsed = parseRRResponse(resultXml);
|
|
if (!parsed.success) throw new RrApiError(parsed.message, parsed.code);
|
|
|
|
const customers = parsed.parsed?.Customers?.Customer || [];
|
|
const vehicles = parsed.parsed?.Vehicles?.ServiceVehicle || [];
|
|
const companies = parsed.parsed?.Companies?.Company || [];
|
|
|
|
const result = {
|
|
customers: Array.isArray(customers) ? customers : [customers],
|
|
vehicles: Array.isArray(vehicles) ? vehicles : [vehicles],
|
|
companies: Array.isArray(companies) ? companies : [companies]
|
|
};
|
|
|
|
RRLogger(
|
|
socket,
|
|
"debug",
|
|
`${action} returned ${result.customers.length} customers, ${result.vehicles.length} vehicles, ${result.companies.length} companies`
|
|
);
|
|
return { success: true, dms: "Rome", action, ...result };
|
|
} catch (error) {
|
|
RRLogger(socket, "error", `Error in ${action}`, {
|
|
message: error.message,
|
|
stack: error.stack
|
|
});
|
|
throw new RrApiError(`RR ${action} failed: ${error.message}`, "COMBINED_SEARCH_ERROR");
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getAdvisors,
|
|
getParts,
|
|
combinedSearch
|
|
};
|