IO-117 WIP PBS
This commit is contained in:
@@ -30,16 +30,52 @@ exports.default = async function (socket, { txEnvelope, jobid }) {
|
||||
const JobData = await QueryJobData(socket, jobid);
|
||||
socket.JobData = JobData;
|
||||
|
||||
socket.DmsVeh = await QueryVehicleFromDms(socket);
|
||||
//Query for the Vehicle record to get the associated customer.
|
||||
// socket.DmsVeh = await QueryVehicleFromDms(socket);
|
||||
//Todo: Need to validate the lines and methods below.
|
||||
if (socket.DmsVeh && socket.DmsVeh.CustomerRef) {
|
||||
//Get the associated customer from the Vehicle Record.
|
||||
socket.DMSVehCustomer = await QueryCustomerBycodeFromDms(
|
||||
socket,
|
||||
socket.DmsVeh.CustomerRef
|
||||
);
|
||||
}
|
||||
socket.DMSCustList = await QueryCustomersFromDms(socket);
|
||||
|
||||
socket.emit("pbs-select-customer", [
|
||||
...(socket.DMSVehCustomer
|
||||
? [{ ...socket.DMSVehCustomer, vinOwner: true }]
|
||||
: []),
|
||||
...socket.DMSCustList,
|
||||
]);
|
||||
} catch (error) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"ERROR",
|
||||
`Error encountered in PbsJobExport. ${error}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
exports.PbsSelectedCustomer = async function PbsSelectedCustomer(
|
||||
socket,
|
||||
selectedCustomerId
|
||||
) {
|
||||
try {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
`User selected customer ${selectedCustomerId || "NEW"}`
|
||||
);
|
||||
|
||||
socket.DmsCustList = await QueryCustomersFromDms(socket);
|
||||
//Upsert the contact information as per Wafaa's Email.
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
`Upserting contact information to DMS for ${socket.JobData.ownr_fn} ${socket.JobData.ownr_ln} ${socket.JobData.ownr_co_nm}`
|
||||
);
|
||||
const ownerRef = await UpsertContactData(socket);
|
||||
const ownerRef = await UpsertContactData(socket, selectedCustomerId);
|
||||
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
@@ -56,28 +92,29 @@ exports.default = async function (socket, { txEnvelope, jobid }) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"ERROR",
|
||||
`Error encountered in PbsJobExport. ${error}`
|
||||
`Error encountered in CdkSelectedCustomer. ${error}`
|
||||
);
|
||||
await InsertFailedExportLog(socket, error);
|
||||
}
|
||||
};
|
||||
|
||||
async function CheckForErrors(socket, response) {
|
||||
if (response.WasSuccessful) {
|
||||
if (response.WasSuccessful === undefined || response.WasSuccessful === true) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
`Succesful response from DMS:. ${response.Message}`
|
||||
`Succesful response from DMS. ${response.Message || ""}`
|
||||
);
|
||||
} else {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"ERROR",
|
||||
`Error received from DMS:. ${response.Message}`
|
||||
`Error received from DMS: ${response.Message}`
|
||||
);
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"TRACE",
|
||||
`Error received from DMS:. ${JSON.stringify(response)}`
|
||||
`Error received from DMS: ${JSON.stringify(response)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -144,7 +181,9 @@ async function QueryCustomersFromDms(socket) {
|
||||
SerialNumber: socket.JobData.bodyshop.pbs_serialnumber,
|
||||
//ContactId: "00000000000000000000000000000000",
|
||||
ContactCode: socket.JobData.owner.accountingid,
|
||||
FirstName: socket.JobData.ownr_fn,
|
||||
FirstName: socket.JobData.ownr_co_nm
|
||||
? socket.JobData.ownr_co_nm
|
||||
: socket.JobData.ownr_fn,
|
||||
LastName: socket.JobData.ownr_ln,
|
||||
PhoneNumber: socket.JobData.ownr_ph1,
|
||||
// EmailAddress: "String",
|
||||
@@ -160,7 +199,43 @@ async function QueryCustomersFromDms(socket) {
|
||||
{ auth: PBS_CREDENTIALS }
|
||||
);
|
||||
CheckForErrors(socket, CustomerGetResponse);
|
||||
return CustomerGetResponse;
|
||||
return CustomerGetResponse && CustomerGetResponse.Contacts;
|
||||
} catch (error) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
"ERROR",
|
||||
`Error in QueryCustomersFromDms - ${error}`
|
||||
);
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
async function QueryCustomerBycodeFromDms(socket, CustomerRef) {
|
||||
try {
|
||||
const { data: CustomerGetResponse } = await axios.post(
|
||||
PBS_ENDPOINTS.ContactGet,
|
||||
{
|
||||
SerialNumber: socket.JobData.bodyshop.pbs_serialnumber,
|
||||
ContactId: CustomerRef,
|
||||
//ContactCode: socket.JobData.owner.accountingid,
|
||||
//FirstName: socket.JobData.ownr_co_nm
|
||||
// ? socket.JobData.ownr_co_nm
|
||||
// : socket.JobData.ownr_fn,
|
||||
//LastName: socket.JobData.ownr_ln,
|
||||
//PhoneNumber: socket.JobData.ownr_ph1,
|
||||
// EmailAddress: "String",
|
||||
// ModifiedSince: "0001-01-01T00:00:00.0000000Z",
|
||||
// ModifiedUntil: "0001-01-01T00:00:00.0000000Z",
|
||||
// ContactIdList: ["00000000000000000000000000000000"],
|
||||
// IncludeInactive: false,
|
||||
// PayableAccount: "String",
|
||||
// ReceivableAccount: "String",
|
||||
// DriverLicense: "String",
|
||||
// ZipCode: "String",
|
||||
},
|
||||
{ auth: PBS_CREDENTIALS }
|
||||
);
|
||||
CheckForErrors(socket, CustomerGetResponse);
|
||||
return CustomerGetResponse && CustomerGetResponse.Contacts;
|
||||
} catch (error) {
|
||||
CdkBase.createLogEvent(
|
||||
socket,
|
||||
@@ -171,14 +246,14 @@ async function QueryCustomersFromDms(socket) {
|
||||
}
|
||||
}
|
||||
|
||||
async function UpsertContactData(socket) {
|
||||
async function UpsertContactData(socket, selectedCustomerId) {
|
||||
try {
|
||||
const { data: ContactChangeResponse } = await axios.post(
|
||||
PBS_ENDPOINTS.ContactChange,
|
||||
{
|
||||
ContactInfo: {
|
||||
// Id: socket.JobData.owner.id,
|
||||
ContactId: socket.JobData.owner.id,
|
||||
...(selectedCustomerId ? { ContactId: selectedCustomerId } : {}),
|
||||
SerialNumber: socket.JobData.bodyshop.pbs_serialnumber,
|
||||
Code: socket.JobData.owner.accountingid,
|
||||
...(socket.JobData.ownr_co_nm
|
||||
@@ -531,3 +606,20 @@ async function MarkJobExported(socket, jobid) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function InsertFailedExportLog(socket, error) {
|
||||
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
|
||||
.request(queries.INSERT_EXPORT_LOG, {
|
||||
log: {
|
||||
bodyshopid: socket.JobData.bodyshop.id,
|
||||
jobid: socket.JobData.id,
|
||||
successful: false,
|
||||
message: [error],
|
||||
useremail: socket.user.email,
|
||||
},
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ const CdkCalculateAllocations =
|
||||
require("../cdk/cdk-calculate-allocations").default;
|
||||
const { isArray } = require("lodash");
|
||||
const logger = require("../utils/logger");
|
||||
const PbsExportJob = require("../accounting/pbs/pbs-job-export").default;
|
||||
const {default: PbsExportJob, PbsSelectedCustomer} = require("../accounting/pbs/pbs-job-export");
|
||||
|
||||
io.use(function (socket, next) {
|
||||
try {
|
||||
@@ -113,6 +113,15 @@ io.on("connection", (socket) => {
|
||||
socket.on("pbs-export-job", (jobid) => {
|
||||
PbsExportJob(socket, jobid);
|
||||
});
|
||||
socket.on("pbs-selected-customer", (selectedCustomerId) => {
|
||||
createLogEvent(
|
||||
socket,
|
||||
"DEBUG",
|
||||
`User selected customer ID ${selectedCustomerId}`
|
||||
);
|
||||
socket.selectedCustomerId = selectedCustomerId;
|
||||
PbsSelectedCustomer(socket, selectedCustomerId);
|
||||
});
|
||||
//End PBS
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
|
||||
Reference in New Issue
Block a user