335 lines
8.8 KiB
JavaScript
335 lines
8.8 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 IMEX_CDK_USER = process.env.IMEX_CDK_USER,
|
|
IMEX_CDK_PASSWORD = process.env.IMEX_CDK_PASSWORD;
|
|
const CDK_CREDENTIALS = {
|
|
password: IMEX_CDK_PASSWORD,
|
|
username: IMEX_CDK_USER,
|
|
};
|
|
|
|
exports.default = async function (socket, jobid) {
|
|
socket.logEvents = [];
|
|
try {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`Received Job export request for id ${jobid}`
|
|
);
|
|
socket["cdk-job-export"] = {};
|
|
let clVFV, clADPV, clADPC;
|
|
const JobData = await QueryJobData(socket, jobid);
|
|
console.log(JSON.stringify(JobData, null, 2));
|
|
const DealerId = JobData.bodyshop.cdk_dealerid;
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`Dealer ID detected: ${JSON.stringify(DealerId)}`
|
|
);
|
|
|
|
//{1} Begin Calculate DMS Vehicle Id
|
|
clVFV = await CalculateDmsVid(socket, JobData);
|
|
if (clVFV.newId === "Y") {
|
|
//{1.2} This is a new Vehicle ID
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{1.2} clVFV DMSVid does *not* exist. clVFV: ${JSON.stringify(
|
|
clVFV,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
|
|
//Check if DMSCustId is Empty - which it should always be?
|
|
//{6.6} Should check to see if a customer exists so that we can marry it to the new vehicle.
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{6.6} Trying to find customer ID in DMS.`
|
|
);
|
|
|
|
//Array
|
|
const strIDS = await FindCustomerIdFromDms(socket, JobData);
|
|
if (strIDS.length > 0) {
|
|
CdkBase.createLogEvent(socket, "DEBUG", `{8.2} Customer ID(s) found.`);
|
|
} else {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{8.5} Customer ID(s) *not* found.`
|
|
);
|
|
}
|
|
} else {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{1.2} clVFV DMSVid does exist. clVFV: ${JSON.stringify(
|
|
clVFV,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
|
|
//{2} Begin Find Vehicle in DMS
|
|
clADPV = await FindVehicleInDms(socket, JobData, clVFV); //TODO: Verify that this should always return a result. If an ID was found previously, it should be correct?
|
|
|
|
//{2.2} Check if the vehicle was found in the DMS.
|
|
if (clADPV.AppErrorNo === "0") {
|
|
//Vehicle was found.
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{1.4} Vehicle was found in the DMS. clADPV: ${JSON.stringify(
|
|
clADPV,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
} else {
|
|
//Vehicle was not found.
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{6.4} Vehicle does not exist in DMS. Will have to create one. clVFV: ${JSON.stringify(
|
|
clVFV,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error encountered in CdkJobExport. ${error}`
|
|
);
|
|
} finally {
|
|
//Ensure we always insert logEvents
|
|
//GQL to insert logevents.
|
|
}
|
|
};
|
|
|
|
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 FindCustomerIdFromDms(socket, JobData) {
|
|
const ownerName = `${JobData.ownr_ln},${JobData.ownr_fn}`;
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{8} Begin Read Customer from DMS using OWNER NAME: ${ownerName}`
|
|
);
|
|
|
|
try {
|
|
const soapClientCustomerSearch = await soap.createClientAsync(
|
|
CdkWsdl.CustomerSearch
|
|
);
|
|
const soapResponseCustomerSearch =
|
|
await soapClientCustomerSearch.executeSearchBulkAsync(
|
|
{
|
|
arg0: CDK_CREDENTIALS,
|
|
arg1: { id: JobData.bodyshop.cdk_dealerid },
|
|
arg2: {
|
|
verb: "EXACT",
|
|
key: ownerName,
|
|
},
|
|
},
|
|
|
|
{}
|
|
);
|
|
CheckCdkResponseForError(socket, soapResponseCustomerSearch);
|
|
const [result, rawResponse, soapheader, rawRequest] =
|
|
soapResponseCustomerSearch;
|
|
//result format
|
|
// return: [
|
|
// {
|
|
// code: 'success',
|
|
// carInvStockNo: '',
|
|
// errorLevel: '0',
|
|
// errorMessage: '',
|
|
// newId: 'Y',
|
|
// vehiclesVehId: 'HM263407'
|
|
// }
|
|
// ]
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`soapClientCustomerSearch.executeSearchBulkAsync Result ${JSON.stringify(
|
|
result,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
const CustomersFromDms = result && result.return;
|
|
return CustomersFromDms;
|
|
} catch (error) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error in FindCustomerIdFromDms - ${JSON.stringify(error, null, 2)}`
|
|
);
|
|
}
|
|
}
|
|
|
|
async function FindVehicleInDms(socket, JobData, clVFV) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"DEBUG",
|
|
`{2}/{6} Begin Find Vehicle In DMS using clVFV: ${clVFV}`
|
|
);
|
|
|
|
try {
|
|
const soapClientVehicleInsertUpdate = await soap.createClientAsync(
|
|
CdkWsdl.VehicleInsertUpdate
|
|
);
|
|
const soapResponseVehicleInsertUpdate =
|
|
await soapClientVehicleInsertUpdate.readBulkAsync(
|
|
{
|
|
arg0: CDK_CREDENTIALS,
|
|
arg1: { id: JobData.bodyshop.cdk_dealerid },
|
|
arg2: {
|
|
fileType: "VEHICLES",
|
|
vehiclesVehicleId: clVFV.vehiclesVehId,
|
|
},
|
|
},
|
|
|
|
{}
|
|
);
|
|
CheckCdkResponseForError(socket, soapResponseVehicleInsertUpdate);
|
|
const [result, rawResponse, soapheader, rawRequest] =
|
|
soapResponseVehicleInsertUpdate;
|
|
//result format
|
|
// return: [
|
|
// {
|
|
// code: 'success',
|
|
// carInvStockNo: '',
|
|
// errorLevel: '0',
|
|
// errorMessage: '',
|
|
// newId: 'Y',
|
|
// vehiclesVehId: 'HM263407'
|
|
// }
|
|
// ]
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`soapClientVehicleInsertUpdate.readBulkAsync Result ${JSON.stringify(
|
|
result,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
const VehicleFromDMS = result && result.return && result.return[0];
|
|
return VehicleFromDMS;
|
|
} catch (error) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error in FindVehicleInDms - ${JSON.stringify(error, null, 2)}`
|
|
);
|
|
}
|
|
}
|
|
|
|
async function CalculateDmsVid(socket, JobData) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`{1} Begin Calculate DMS Vehicle ID using VIN: ${JobData.v_vin}`
|
|
);
|
|
|
|
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;
|
|
//result format
|
|
// return: [
|
|
// {
|
|
// code: 'success',
|
|
// carInvStockNo: '',
|
|
// errorLevel: '0',
|
|
// errorMessage: '',
|
|
// newId: 'Y',
|
|
// vehiclesVehId: 'HM263407'
|
|
// }
|
|
// ]
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"TRACE",
|
|
`soapClientVehicleInsertUpdate.searchIDsByVINAsync Result ${JSON.stringify(
|
|
result,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
const DmsVehicle = result && result.return && result.return[0];
|
|
return DmsVehicle;
|
|
} catch (error) {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error in CalculateDmsVid - ${JSON.stringify(error, null, 2)}`
|
|
);
|
|
}
|
|
}
|
|
|
|
function CheckCdkResponseForError(socket, soapResponse) {
|
|
const ResultToCheck = Array.isArray(soapResponse[0].return)
|
|
? soapResponse[0].return[0]
|
|
: soapResponse[0].return;
|
|
|
|
if (ResultToCheck.errorLevel === 0 || ResultToCheck.errorLevel === "0")
|
|
//TODO: Verify that this is the best way to detect errors.
|
|
return;
|
|
else {
|
|
CdkBase.createLogEvent(
|
|
socket,
|
|
"ERROR",
|
|
`Error detected in CDK Response - ${JSON.stringify(
|
|
ResultToCheck,
|
|
null,
|
|
2
|
|
)}`
|
|
);
|
|
|
|
throw {
|
|
errorLevel: ResultToCheck.errorLevel,
|
|
errorMessage: ResultToCheck.errorMessage,
|
|
};
|
|
}
|
|
}
|