493 lines
14 KiB
JavaScript
493 lines
14 KiB
JavaScript
const path = require("path");
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
const GraphQLClient = require("graphql-request").GraphQLClient;
|
|
const soap = require("soap");
|
|
const queries = require("../graphql-client/queries");
|
|
const CdkBase = require("../web-sockets/web-socket");
|
|
const CdkWsdl = require("./cdk-wsdl").default;
|
|
const logger = require("../utils/logger");
|
|
const { CDK_CREDENTIALS, CheckCdkResponseForError } = require("./cdk-wsdl");
|
|
|
|
exports.default = async function (socket, { jobid }) {
|
|
socket.logEvents = [];
|
|
socket.recordid = jobid;
|
|
try {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`Received Job export request for id ${jobid}`
|
|
);
|
|
|
|
const JobData = await QueryJobData(socket, jobid);
|
|
const DealerId = JobData.bodyshop.cdk_dealerid;
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`Dealer ID detected: ${JSON.stringify(DealerId)}`
|
|
);
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`{1} Begin Calculate DMS Vehicle ID using VIN: ${JobData.v_vin}`
|
|
);
|
|
socket.DMSVid = await CalculateDmsVid(socket, JobData);
|
|
|
|
if (socket.DMSVid.newId === "N") {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`{2.1} Querying the Vehicle using the DMSVid: ${socket.DmsVid}`
|
|
);
|
|
socket.DMSVeh = await QueryDmsVehicleById(socket, JobData, socket.DMSVid);
|
|
|
|
const DmsVehCustomerId =
|
|
socket.DMSVeh &&
|
|
socket.DMSVeh.owners.find((o) => o.assigningPartyId === "CURRENT");
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`{2.2} Querying the Customer using the ID from DMSVeh: ${DmsVehCustomerId}`
|
|
);
|
|
socket.DmsVehCustomer = await QueryDmsCustomerById(
|
|
socket,
|
|
JobData,
|
|
DmsVehCustomerId
|
|
);
|
|
}
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`{2.3} Querying the Customer using the name.`
|
|
);
|
|
|
|
socket.DmsCustList = await QueryDmsCustomerByName(socket, JobData);
|
|
|
|
socket.emit("cdk-select-customer", [
|
|
...(socket.DmsVehCustomer ? socket.DmsVehCustomer : []),
|
|
...socket.DmsCustList,
|
|
]);
|
|
} catch (error) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error encountered in CdkJobExport. ${error}`
|
|
);
|
|
} finally {
|
|
//Ensure we always insert logEvents
|
|
//GQL to insert logevents.
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`Capturing log events to database.`
|
|
);
|
|
}
|
|
};
|
|
|
|
async function QueryJobData(socket, jobid) {
|
|
CdkBase.createLogEvent(socket, "DEBUG", `Querying job data for id ${jobid}`);
|
|
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
|
|
const result = await client
|
|
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
|
|
.request(queries.QUERY_JOBS_FOR_CDK_EXPORT, { id: jobid });
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`Job data query result ${JSON.stringify(result, null, 2)}`
|
|
);
|
|
return result.jobs_by_pk;
|
|
}
|
|
|
|
async function CalculateDmsVid(socket, JobData) {
|
|
try {
|
|
const soapClientVehicleInsertUpdate = await soap.createClientAsync(
|
|
CdkWsdl.VehicleInsertUpdate
|
|
);
|
|
const soapResponseVehicleInsertUpdate =
|
|
await soapClientVehicleInsertUpdate.getVehIdsAsync({
|
|
arg0: CDK_CREDENTIALS,
|
|
arg1: { id: JobData.bodyshop.cdk_dealerid },
|
|
arg2: { VIN: JobData.v_vin },
|
|
});
|
|
CheckCdkResponseForError(socket, soapResponseVehicleInsertUpdate);
|
|
const [result, rawResponse, soapheader, rawRequest] =
|
|
soapResponseVehicleInsertUpdate;
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawRequest,
|
|
`soapClientVehicleInsertUpdate.getVehIdsAsync reqest.`
|
|
);
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawResponse,
|
|
`soapClientVehicleInsertUpdate.getVehIdsAsync response.`
|
|
);
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`soapClientVehicleInsertUpdate.getVehIdsAsync Result ${JSON.stringify(
|
|
result,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
|
|
const DmsVid = result && result.return && result.return[0];
|
|
return DmsVid;
|
|
} catch (error) {
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
error.request,
|
|
`soapClientVehicleInsertUpdate.getVehIdsAsync reqest.`,
|
|
true
|
|
);
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
error.response.data,
|
|
`soapClientVehicleInsertUpdate.getVehIdsAsync response.`,
|
|
true
|
|
);
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`{1} Error in CalculateDmsVid - ${error}`
|
|
);
|
|
throw new Error(error);
|
|
}
|
|
}
|
|
|
|
async function QueryDmsVehicleById(socket, JobData, DMSVid) {
|
|
try {
|
|
const soapClientVehicleInsertUpdate = await soap.createClientAsync(
|
|
CdkWsdl.VehicleInsertUpdate
|
|
);
|
|
|
|
const soapResponseVehicleInsertUpdate =
|
|
await soapClientVehicleInsertUpdate.readAsync(
|
|
{
|
|
arg0: CDK_CREDENTIALS,
|
|
arg1: { id: JobData.bodyshop.cdk_dealerid },
|
|
arg2: {
|
|
fileType: "VEHICLES",
|
|
vehiclesVehicleId: DMSVid.vehiclesVehId,
|
|
},
|
|
},
|
|
|
|
function (err, result, rawResponse, soapHeader, rawRequest) {
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawRequest,
|
|
`soapClientVehicleInsertUpdate.readAsync reqest.`,
|
|
!!err
|
|
);
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawResponse,
|
|
`soapClientVehicleInsertUpdate.readAsync response.`,
|
|
!!err
|
|
);
|
|
}
|
|
);
|
|
CheckCdkResponseForError(socket, soapResponseVehicleInsertUpdate);
|
|
const [result, rawResponse, soapheader, rawRequest] =
|
|
soapResponseVehicleInsertUpdate;
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawRequest,
|
|
`soapClientVehicleInsertUpdate.readAsync reqest.`
|
|
);
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`soapClientVehicleInsertUpdate.readAsync Result ${JSON.stringify(
|
|
result,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawResponse,
|
|
`soapClientVehicleInsertUpdate.readAsync response.`
|
|
);
|
|
const VehicleFromDMS = result && result.return && result.return.vehicle;
|
|
return VehicleFromDMS;
|
|
} catch (error) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error in QueryDmsVehicleById - ${error}`
|
|
);
|
|
throw new Error(error);
|
|
}
|
|
}
|
|
|
|
async function QueryDmsCustomerById(socket, JobData, CustomerId) {
|
|
try {
|
|
const soapClientCustomerInsertUpdate = await soap.createClientAsync(
|
|
CdkWsdl.CustomerInsertUpdate
|
|
);
|
|
const soapResponseCustomerInsertUpdate =
|
|
await soapClientCustomerInsertUpdate.readAsync({
|
|
arg0: CDK_CREDENTIALS,
|
|
arg1: { dealerId: JobData.bodyshop.cdk_dealerid }, //TODO: Verify why this does not follow the other standards.
|
|
arg2: {
|
|
userId: CustomerId,
|
|
},
|
|
});
|
|
CheckCdkResponseForError(socket, soapResponseCustomerInsertUpdate);
|
|
const [result, rawResponse, soapheader, rawRequest] =
|
|
soapResponseCustomerInsertUpdate;
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawRequest,
|
|
`soapClientCustomerInsertUpdate.readAsync reqest.`
|
|
);
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawResponse,
|
|
`soapClientCustomerInsertUpdate.readAsync response.`
|
|
);
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`soapClientCustomerInsertUpdate.readAsync Result ${JSON.stringify(
|
|
result,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
|
|
const CustomersFromDms =
|
|
result && result.return && result.return.customerParty;
|
|
return CustomersFromDms;
|
|
} catch (error) {
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
error.request,
|
|
`soapClientCustomerInsertUpdate.readAsync reqest.`,
|
|
true
|
|
);
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
error.response.data,
|
|
`soapClientCustomerInsertUpdate.readAsync response.`,
|
|
true
|
|
);
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error in QueryDmsCustomerById - ${error}`
|
|
);
|
|
throw new Error(error);
|
|
}
|
|
}
|
|
|
|
async function QueryDmsCustomerByName(socket, JobData) {
|
|
const ownerName = `${JobData.ownr_ln},${JobData.ownr_fn}`;
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`Begin Query DMS Customer by Name using: ${ownerName}`
|
|
);
|
|
|
|
try {
|
|
const soapClientCustomerSearch = await soap.createClientAsync(
|
|
CdkWsdl.CustomerSearch
|
|
);
|
|
const soapResponseCustomerSearch =
|
|
await soapClientCustomerSearch.executeSearchAsync({
|
|
arg0: CDK_CREDENTIALS,
|
|
arg1: { dealerId: JobData.bodyshop.cdk_dealerid }, //TODO: Verify why this does not follow the other standards.
|
|
arg2: {
|
|
verb: "EXACT",
|
|
key: ownerName,
|
|
},
|
|
});
|
|
CheckCdkResponseForError(socket, soapResponseCustomerSearch);
|
|
const [result, rawResponse, soapheader, rawRequest] =
|
|
soapResponseCustomerSearch;
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawRequest,
|
|
`soapClientCustomerSearch.executeSearchBulkAsync reqest.`
|
|
);
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
rawResponse,
|
|
`soapClientCustomerSearch.executeSearchBulkAsync response.`
|
|
);
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`soapClientCustomerSearch.executeSearchBulkAsync Result ${JSON.stringify(
|
|
result,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
const CustomersFromDms = (result && result.return) || [];
|
|
return CustomersFromDms;
|
|
} catch (error) {
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
error.request,
|
|
`soapClientCustomerSearch.executeSearchBulkAsync reqest.`,
|
|
true
|
|
);
|
|
|
|
CdkBase.createXmlEvent(
|
|
socket,
|
|
error.response.data,
|
|
`soapClientCustomerSearch.executeSearchBulkAsync response.`,
|
|
true
|
|
);
|
|
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error in QueryDmsCustomerByName - ${error}`
|
|
);
|
|
throw new Error(error);
|
|
}
|
|
}
|
|
|
|
// async function CreateCustomerInDms(socket, JobData, newCustomerNumber) {
|
|
// CdkBase.createLogEvent(socket, "DEBUG", `{11} Begin Create Customer in DMS`);
|
|
|
|
// try {
|
|
// const soapClientCustomerInsertUpdate = await soap.createClientAsync(
|
|
// CdkWsdl.CustomerInsertUpdate
|
|
// );
|
|
// const soapResponseCustomerInsertUpdate =
|
|
// await soapClientCustomerInsertUpdate.insertAsync(
|
|
// {
|
|
// arg0: CDK_CREDENTIALS,
|
|
// arg1: { dealerId: JobData.bodyshop.cdk_dealerid }, //TODO: Verify why this does not follow the other standards.
|
|
// arg2: { userId: null },
|
|
// arg3: {
|
|
// //Copied the required fields from the other integration.
|
|
// //TODO: Verify whether we need to bring more information in.
|
|
// id: { value: newCustomerNumber },
|
|
// address: {
|
|
// city: JobData.ownr_city,
|
|
// country: null,
|
|
// postalcode: JobData.ownr_zip,
|
|
// stateOrProvince: JobData.ownr_st,
|
|
// },
|
|
// contactInfo: {
|
|
// mainTelephoneNumber: { main: true, value: JobData.ownr_ph1 },
|
|
// },
|
|
// demographics: null,
|
|
// name1: {
|
|
// companyname: null,
|
|
// firstName: JobData.ownr_fn,
|
|
// fullname: null,
|
|
// lastName: JobData.ownr_ln,
|
|
// middleName: null,
|
|
// nameType: "Person",
|
|
// suffix: null,
|
|
// title: null,
|
|
// },
|
|
// },
|
|
// },
|
|
|
|
// {}
|
|
// );
|
|
// CheckCdkResponseForError(socket, soapResponseCustomerInsertUpdate);
|
|
// const [
|
|
// result, //rawResponse, soapheader, rawRequest
|
|
// ] = soapResponseCustomerInsertUpdate;
|
|
|
|
// CdkBase.createLogEvent(
|
|
// socket,
|
|
// "TRACE",
|
|
// `soapClientCustomerInsertUpdate.insertAsync Result ${JSON.stringify(
|
|
// result,
|
|
// null,
|
|
// 2
|
|
// )}`
|
|
// );
|
|
// const customer = result && result.return;
|
|
// return customer;
|
|
// } catch (error) {
|
|
// CdkBase.createLogEvent(
|
|
// socket,
|
|
// "ERROR",
|
|
// `Error in CreateCustomerInDms - ${error}`
|
|
// );
|
|
// throw new Error(error);
|
|
// }
|
|
// }
|
|
|
|
// async function GenerateCustomerNumberFromDms(socket, JobData) {
|
|
// CdkBase.createLogEvent(
|
|
// socket,
|
|
// "DEBUG",
|
|
// `{10} Begin Generate Customer Number from DMS`
|
|
// );
|
|
|
|
// try {
|
|
// const soapClientCustomerInsertUpdate = await soap.createClientAsync(
|
|
// CdkWsdl.CustomerInsertUpdate
|
|
// );
|
|
// const soapResponseCustomerInsertUpdate =
|
|
// await soapClientCustomerInsertUpdate.getCustomerNumberAsync(
|
|
// {
|
|
// arg0: CDK_CREDENTIALS,
|
|
// arg1: { dealerId: JobData.bodyshop.cdk_dealerid }, //TODO: Verify why this does not follow the other standards.
|
|
// arg2: { userId: null },
|
|
// },
|
|
|
|
// {}
|
|
// );
|
|
// CheckCdkResponseForError(socket, soapResponseCustomerInsertUpdate);
|
|
// const [
|
|
// result, //rawResponse, soapheader, rawRequest
|
|
// ] = soapResponseCustomerInsertUpdate;
|
|
|
|
// CdkBase.createLogEvent(
|
|
// socket,
|
|
// "TRACE",
|
|
// `soapClientCustomerInsertUpdate.getCustomerNumberAsync Result ${JSON.stringify(
|
|
// result,
|
|
// null,
|
|
// 2
|
|
// )}`
|
|
// );
|
|
// const customerNumber =
|
|
// result && result.return && result.return.customerNumber;
|
|
// return customerNumber;
|
|
// } catch (error) {
|
|
// CdkBase.createLogEvent(
|
|
// socket,
|
|
// "ERROR",
|
|
// `Error in GenerateCustomerNumberFromDms - ${JSON.stringify(
|
|
// error,
|
|
// null,
|
|
// 2
|
|
// )}`
|
|
// );
|
|
// throw new Error(error);
|
|
// }
|
|
// }
|