feature/IO-2776-cdk-fortellis - Cleanup + Auth Socket stuff

This commit is contained in:
Dave
2025-09-24 18:12:57 -04:00
parent 57baa3d9fd
commit f7799ffd03
6 changed files with 492 additions and 387 deletions

View File

@@ -29,17 +29,20 @@ function handleFortellisApiError(socket, error, functionName, additionalDetails
functionName,
...additionalDetails
});
CreateFortellisLogEvent(socket, "DEBUG", `${functionName} failed. , ${JSON.stringify({
reqId: error.reqId,
url: error.url,
apiName: error.apiName,
errorData: error.errorData,
errorStatus: error.errorStatus,
errorStatusText: error.errorStatusText,
functionName,
...additionalDetails
})}`);
CreateFortellisLogEvent(
socket,
"DEBUG",
`${functionName} failed. , ${JSON.stringify({
reqId: error.reqId,
url: error.url,
apiName: error.apiName,
errorData: error.errorData,
errorStatus: error.errorStatus,
errorStatusText: error.errorStatusText,
functionName,
...additionalDetails
})}`
);
} else {
CreateFortellisLogEvent(socket, "ERROR", `Error in ${functionName} - ${error.message}`, {
error: error.message,
@@ -50,13 +53,7 @@ function handleFortellisApiError(socket, error, functionName, additionalDetails
}
}
async function FortellisJobExport({
socket,
redisHelpers,
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom },
txEnvelope,
jobid
}) {
async function FortellisJobExport({ socket, redisHelpers, txEnvelope, jobid }) {
const {
// setSessionData,
// getSessionData,
@@ -64,7 +61,7 @@ async function FortellisJobExport({
// removeUserSocketMapping,
// refreshUserSocketTTL,
// getUserSocketMappingByBodyshop,
setSessionTransactionData,
setSessionTransactionData
// getSessionTransactionData,
// clearSessionTransactionData
} = redisHelpers;
@@ -78,47 +75,78 @@ async function FortellisJobExport({
defaultFortellisTTL
);
const JobData = await QueryJobData({ socket, jobid }); //TODO: Need to remove unnecessary stuff here to reduce the payload.
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.JobData, JobData, defaultFortellisTTL);
//TODO: Need to remove unnecessary stuff here to reduce the payload.
const JobData = await QueryJobData({ socket, jobid });
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.JobData,
JobData,
defaultFortellisTTL
);
CreateFortellisLogEvent(socket, "DEBUG", `{1} Begin Calculate DMS Vehicle ID using VIN: ${JobData.v_vin}`);
const DMSVid = (await CalculateDmsVid({ socket, JobData, redisHelpers }))[0];
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSVid, DMSVid, defaultFortellisTTL);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSVid,
DMSVid,
defaultFortellisTTL
);
let DMSVehCustomer;
if (!DMSVid.newId) {
CreateFortellisLogEvent(
socket,
"DEBUG",
`{2.1} Querying the Vehicle using the DMSVid: ${DMSVid.vehiclesVehId}`
);
CreateFortellisLogEvent(socket, "DEBUG", `{2.1} Querying the Vehicle using the DMSVid: ${DMSVid.vehiclesVehId}`);
const DMSVeh = await QueryDmsVehicleById({ socket, redisHelpers, JobData, DMSVid });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSVeh, DMSVeh, defaultFortellisTTL);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSVeh,
DMSVeh,
defaultFortellisTTL
);
const DMSVehCustomerFromVehicle =
DMSVeh && DMSVeh.owners && DMSVeh.owners.find((o) => o.id.assigningPartyId === "CURRENT");
DMSVeh?.owners && DMSVeh.owners.find((o) => o.id.assigningPartyId === "CURRENT");
if (DMSVehCustomerFromVehicle && DMSVehCustomerFromVehicle.id && DMSVehCustomerFromVehicle.id.value) {
if (DMSVehCustomerFromVehicle?.id && DMSVehCustomerFromVehicle.id.value) {
CreateFortellisLogEvent(
socket,
"DEBUG",
`{2.2} Querying the Customer using the ID from DMSVeh: ${DMSVehCustomerFromVehicle.id.value}`
);
DMSVehCustomer = await QueryDmsCustomerById({ socket, redisHelpers, JobData, CustomerId: DMSVehCustomerFromVehicle.id.value });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSVehCustomer, DMSVehCustomer, defaultFortellisTTL);
DMSVehCustomer = await QueryDmsCustomerById({
socket,
redisHelpers,
JobData,
CustomerId: DMSVehCustomerFromVehicle.id.value
});
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSVehCustomer,
DMSVehCustomer,
defaultFortellisTTL
);
}
}
CreateFortellisLogEvent(socket, "DEBUG", `{2.3} Querying the Customer using the name.`);
const DMSCustList = await QueryDmsCustomerByName({ socket, redisHelpers, JobData });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSCustList, DMSCustList, defaultFortellisTTL);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSCustList,
DMSCustList,
defaultFortellisTTL
);
socket.emit("fortellis-select-customer", [
...(DMSVehCustomer ? [{ ...DMSVehCustomer, vinOwner: true }] : []),
...DMSCustList
]);
} catch (error) {
CreateFortellisLogEvent(socket, "ERROR", `Error in FortellisJobExport - ${error} `, {
error: error.message,
@@ -127,10 +155,7 @@ async function FortellisJobExport({
}
}
async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, selectedCustomerId, jobid }) {
async function FortellisSelectedCustomer({ socket, redisHelpers, selectedCustomerId, jobid }) {
const {
// setSessionData,
// getSessionData,
@@ -139,93 +164,117 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele
// refreshUserSocketTTL,
// getUserSocketMappingByBodyshop,
setSessionTransactionData,
getSessionTransactionData,
getSessionTransactionData
//clearSessionTransactionData
} = redisHelpers;
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.selectedCustomerId, selectedCustomerId, defaultFortellisTTL);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.selectedCustomerId,
selectedCustomerId,
defaultFortellisTTL
);
const JobData = await getSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.JobData);
const txEnvelope = await getSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.txEnvelope);
const DMSVid = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.DMSVid);
const txEnvelope = await getSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.txEnvelope
);
const DMSVid = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.DMSVid
);
try {
let DMSCust;
if (selectedCustomerId) {
CreateFortellisLogEvent(
socket,
"DEBUG",
`{3.1} Querying the Customer using Customer ID: ${selectedCustomerId}`
);
CreateFortellisLogEvent(socket, "DEBUG", `{3.1} Querying the Customer using Customer ID: ${selectedCustomerId}`);
DMSCust = await QueryDmsCustomerById({ socket, redisHelpers, JobData, CustomerId: selectedCustomerId }); //TODO: Optimize here - if selected ID, we already have the customer information. Prevent an extra call to Fortellis.
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSCust, DMSCust, defaultFortellisTTL);
} else {
CreateFortellisLogEvent(
socket,
"DEBUG",
`{3.2} Creating new customer.`
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSCust,
DMSCust,
defaultFortellisTTL
);
} else {
CreateFortellisLogEvent(socket, "DEBUG", `{3.2} Creating new customer.`);
const DMSCustomerInsertResponse = await InsertDmsCustomer({ socket, redisHelpers, JobData });
DMSCust = { customerId: DMSCustomerInsertResponse.data }
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSCust, DMSCust, defaultFortellisTTL);
DMSCust = { customerId: DMSCustomerInsertResponse.data };
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSCust,
DMSCust,
defaultFortellisTTL
);
}
let DMSVeh;
if (DMSVid.newId) {
CreateFortellisLogEvent(
socket,
"DEBUG",
`{4.1} Inserting new vehicle with ID: ID ${DMSVid.vehiclesVehId}`
);
CreateFortellisLogEvent(socket, "DEBUG", `{4.1} Inserting new vehicle with ID: ID ${DMSVid.vehiclesVehId}`);
DMSVeh = await InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMSVid, DMSCust });
} else {
CreateFortellisLogEvent(
socket,
"DEBUG",
`{4.2} Querying Existing Vehicle using ID ${DMSVid.vehiclesVehId}`
);
CreateFortellisLogEvent(socket, "DEBUG", `{4.2} Querying Existing Vehicle using ID ${DMSVid.vehiclesVehId}`);
DMSVeh = await QueryDmsVehicleById({ socket, redisHelpers, JobData, DMSVid });
CreateFortellisLogEvent(
CreateFortellisLogEvent(socket, "DEBUG", `{4.3} Updating Existing Vehicle to associate to owner.`);
DMSVeh = await UpdateDmsVehicle({
socket,
"DEBUG",
`{4.3} Updating Existing Vehicle to associate to owner.`
);
DMSVeh = await UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust, selectedCustomerId, txEnvelope });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSVeh, DMSVeh, defaultFortellisTTL);//TODO: This should be immutable probably.F
redisHelpers,
JobData,
DMSVeh,
DMSCust,
selectedCustomerId,
txEnvelope
});
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSVeh,
DMSVeh,
defaultFortellisTTL
); //TODO: This should be immutable probably.F
}
// const DMSVehHistory = await InsertServiceVehicleHistory({ socket, redisHelpers, JobData });
// await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSVehHistory, DMSVehHistory, defaultFortellisTTL);
CreateFortellisLogEvent(
socket,
"DEBUG",
`{5} Creating Transaction header with Dms Start WIP`
);
CreateFortellisLogEvent(socket, "DEBUG", `{5} Creating Transaction header with Dms Start WIP`);
const DMSTransHeader = await InsertDmsStartWip({ socket, redisHelpers, JobData });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSTransHeader, DMSTransHeader, defaultFortellisTTL);
CreateFortellisLogEvent(
socket,
"DEBUG",
`{5.1} Creating Transaction with ID ${DMSTransHeader.transID}`
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSTransHeader,
DMSTransHeader,
defaultFortellisTTL
);
CreateFortellisLogEvent(socket, "DEBUG", `{5.1} Creating Transaction with ID ${DMSTransHeader.transID}`);
const DMSBatchTxn = await InsertDmsBatchWip({ socket, redisHelpers, JobData });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSBatchTxn, DMSBatchTxn, defaultFortellisTTL);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSBatchTxn,
DMSBatchTxn,
defaultFortellisTTL
);
if (DMSBatchTxn.rtnCode === "0") {
CreateFortellisLogEvent(
socket,
"DEBUG",
`{6} Attempting to post Transaction with ID ${DMSTransHeader.transID}`
);
CreateFortellisLogEvent(socket, "DEBUG", `{6} Attempting to post Transaction with ID ${DMSTransHeader.transID}`);
const DmsBatchTxnPost = await PostDmsBatchWip({ socket, redisHelpers, JobData });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DmsBatchTxnPost, DmsBatchTxnPost, defaultFortellisTTL);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DmsBatchTxnPost,
DmsBatchTxnPost,
defaultFortellisTTL
);
if (DmsBatchTxnPost.rtnCode === "0") { //TODO: Validate this is a string and not #
if (DmsBatchTxnPost.rtnCode === "0") {
//TODO: Validate this is a string and not #
//something
CreateFortellisLogEvent(socket, "DEBUG", `{6} Successfully posted transaction to DMS.`);
@@ -233,17 +282,21 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele
CreateFortellisLogEvent(socket, "DEBUG", `{5} Updating Service Vehicle History.`);
const DMSVehHistory = await InsertServiceVehicleHistory({ socket, redisHelpers, JobData });
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSVehHistory, DMSVehHistory, defaultFortellisTTL);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSVehHistory,
DMSVehHistory,
defaultFortellisTTL
);
socket.emit("export-success", JobData.id);
} else {
//Get the error code
CreateFortellisLogEvent(
socket,
"DEBUG",
`{6.1} Getting errors for Transaction ID ${DMSTransHeader.transID}`
);
CreateFortellisLogEvent(socket, "DEBUG", `{6.1} Getting errors for Transaction ID ${DMSTransHeader.transID}`);
const DmsError = await QueryDmsErrWip({ socket, redisHelpers, JobData });
await QueryDmsErrWip({ socket, redisHelpers, JobData });
// const DmsError = await QueryDmsErrWip({ socket, redisHelpers, JobData });
// socket.DmsError = await QueryDmsErrWip(socket);
// //Delete the transaction
// CdkBase.createLogEvent(socket, "DEBUG", `{ 6.2 } Deleting Transaction ID ${ socket.DMSTransHeader.transID } `);
@@ -265,7 +318,6 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele
"ERROR",
`DMS Batch Return code was not successful: ${DMSBatchTxn.rtnCode} - ${DMSBatchTxn.sendline}`
);
}
} catch (error) {
// CdkBase.createLogEvent(socket, "ERROR", `Error encountered in CdkSelectedCustomer.${ error } `);
@@ -277,7 +329,6 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele
} finally {
//Ensure we always insert logEvents
//GQL to insert logevents.
//CdkBase.createLogEvent(socket, "DEBUG", `Capturing log events to database.`);
}
}
@@ -301,8 +352,7 @@ async function CalculateDmsVid({ socket, JobData, redisHelpers }) {
redisHelpers,
socket,
jobid: JobData.id,
body: {},
body: {}
});
return result;
} catch (error) {
@@ -323,7 +373,7 @@ async function QueryDmsVehicleById({ socket, redisHelpers, JobData, DMSVid }) {
redisHelpers,
socket,
jobid: JobData.id,
body: {},
body: {}
});
return result;
} catch (error) {
@@ -344,8 +394,7 @@ async function QueryDmsCustomerById({ socket, redisHelpers, JobData, CustomerId
redisHelpers,
socket,
jobid: JobData.id,
body: {},
body: {}
});
return result.data;
} catch (error) {
@@ -361,14 +410,12 @@ async function QueryDmsCustomerByName({ socket, redisHelpers, JobData }) {
const ownerName =
JobData.ownr_co_nm && JobData.ownr_co_nm.trim() !== ""
? [["lastName", JobData.ownr_co_nm.replace(replaceSpecialRegex, "")]]
: [["firstName", JobData.ownr_fn.replace(replaceSpecialRegex, "")], ["lastName", JobData.ownr_ln.replace(replaceSpecialRegex, "")]];
: [
["firstName", JobData.ownr_fn.replace(replaceSpecialRegex, "")],
["lastName", JobData.ownr_ln.replace(replaceSpecialRegex, "")]
];
CreateFortellisLogEvent(
socket,
"DEBUG",
`Begin query DMS Customer by Name using ${JSON.stringify(ownerName)
} `
);
CreateFortellisLogEvent(socket, "DEBUG", `Begin query DMS Customer by Name using ${JSON.stringify(ownerName)} `);
try {
const result = await MakeFortellisCall({
@@ -378,8 +425,7 @@ async function QueryDmsCustomerByName({ socket, redisHelpers, JobData }) {
redisHelpers,
socket,
jobid: JobData.id,
body: {},
body: {}
});
return result.data;
} catch (error) {
@@ -400,33 +446,28 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) {
socket,
jobid: JobData.id,
body: {
"customerType": "INDIVIDUAL",
"customerName": {
customerType: "INDIVIDUAL",
customerName: {
//"suffix": "Mr.",
"firstName": JobData.ownr_fn && JobData.ownr_fn.replace(replaceSpecialRegex, "").toUpperCase(),
firstName: JobData.ownr_fn && JobData.ownr_fn.replace(replaceSpecialRegex, "").toUpperCase(),
//"middleName": "",
"lastName": JobData.ownr_ln && JobData.ownr_ln.replace(replaceSpecialRegex, "").toUpperCase(),
lastName: JobData.ownr_ln && JobData.ownr_ln.replace(replaceSpecialRegex, "").toUpperCase()
//"title": "",
//"nickName": ""
},
"companyName": JobData.ownr_co_nm && JobData.ownr_co_nm.replace(replaceSpecialRegex, "").toUpperCase(),
"postalAddress": {
"addressLine1": JobData.ownr_addr1?.replace(replaceSpecialRegex, "").trim(),
"addressLine2": JobData.ownr_addr2?.replace(replaceSpecialRegex, "").trim(),
"city": JobData.ownr_city?.replace(replaceSpecialRegex, "").trim(),
"state": JobData.ownr_state?.replace(replaceSpecialRegex, "").trim(),
"postalCode": InstanceMgr({
imex:
JobData.ownr_zip &&
JobData.ownr_zip
.toUpperCase()
.replace(/\W/g, "")
.replace(/(...)/, "$1 "),
companyName: JobData.ownr_co_nm && JobData.ownr_co_nm.replace(replaceSpecialRegex, "").toUpperCase(),
postalAddress: {
addressLine1: JobData.ownr_addr1?.replace(replaceSpecialRegex, "").trim(),
addressLine2: JobData.ownr_addr2?.replace(replaceSpecialRegex, "").trim(),
city: JobData.ownr_city?.replace(replaceSpecialRegex, "").trim(),
state: JobData.ownr_state?.replace(replaceSpecialRegex, "").trim(),
postalCode: InstanceMgr({
imex: JobData.ownr_zip && JobData.ownr_zip.toUpperCase().replace(/\W/g, "").replace(/(...)/, "$1 "),
rome: JobData.ownr_zip
}),
//"county": JobData.ownr_county?.trim(),
"country": JobData.ownr_ctry?.replace(replaceSpecialRegex, "").trim(),
"province": JobData.ownr_st?.replace(replaceSpecialRegex, "").trim(),
country: JobData.ownr_ctry?.replace(replaceSpecialRegex, "").trim(),
province: JobData.ownr_st?.replace(replaceSpecialRegex, "").trim()
//"territory": ""
},
// "birthDate": {
@@ -436,12 +477,12 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) {
// },
//"gender": "M",
//"language": "English",
"contactMethods": {
"phones": [
contactMethods: {
phones: [
{
//"uuid": "",
"number": JobData.ownr_ph1?.replace(replaceSpecialRegex, ""),
"type": "HOME",
number: JobData.ownr_ph1?.replace(replaceSpecialRegex, ""),
type: "HOME"
// "doNotCallIndicator": true,
// "doNotCallIndicatorDate": `null,
// "doNotCallRegistrySource": "",
@@ -454,7 +495,7 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) {
// "optInRequestedDate": null,
// "preferredDay": "",
// "preferredTime": ""
},
}
// {
// "uuid": "",
// "number": "6666666666",
@@ -473,20 +514,22 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) {
// "preferredTime": ""
// }
],
"emailAddresses": [
...!_.isEmpty(JobData.ownr_ea) ?
[{
//"uuid": "",
"address": JobData.ownr_ea,
"type": "PERSONAL",
// "doNotEmailSource": "",
// "doNotEmail": false,
// "isPreferred": true,
// "transactionEmailNotificationOptIn": false,
// "optInRequestDate": null,
// "optInDate": null
}] : [],
emailAddresses: [
...(!_.isEmpty(JobData.ownr_ea)
? [
{
//"uuid": "",
address: JobData.ownr_ea,
type: "PERSONAL"
// "doNotEmailSource": "",
// "doNotEmail": false,
// "isPreferred": true,
// "transactionEmailNotificationOptIn": false,
// "optInRequestDate": null,
// "optInDate": null
}
]
: [])
// {
// "uuid": "",
// "address": "jilldoe@test.com",
@@ -499,7 +542,7 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) {
// "optInDate": null
// }
]
},
}
// // "doNotContact": false,
// // "optOutDate": "",
// // "optOutTime": "",
@@ -581,8 +624,7 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) {
// "dealerField": "Custom dealer field value 3"
// }
// ]
},
}
});
return result;
} catch (error) {
@@ -600,7 +642,6 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) {
async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMSVid, DMSCust }) {
try {
const result = await MakeFortellisCall({
...FortellisActions.InsertVehicle,
requestSearchParams: {},
@@ -609,8 +650,8 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS
socket,
jobid: JobData.id,
body: {
"dealer": {
"company": JobData.bodyshop.cdk_configuration.srcco || "77",
dealer: {
company: JobData.bodyshop.cdk_configuration.srcco || "77",
// "dealNumber": "",
// "dealerAssignedNumber": "82268",
// "dealerDefined1": "2WDSP",
@@ -627,12 +668,12 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS
txEnvelope.dms_unsold === true
? ""
: moment(txEnvelope.inservicedate)
//.tz(JobData.bodyshop.timezone)
.startOf("day")
.toISOString()
//.tz(JobData.bodyshop.timezone)
.startOf("day")
.toISOString()
}),
//"lastServiceDate": "2011-11-23",
"vehicleId": DMSVid.vehiclesVehId,
vehicleId: DMSVid.vehiclesVehId
// "vehicleLocation": "",
// "vehicleSoldDate": "2021-04-06",
// "wholesaleVehicleInd": false
@@ -656,7 +697,7 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS
// "vehicleOrderPriority": "",
// "vehicleOrderType": "TRE RETAIL - STOCK"
// },
"vehicle": {
vehicle: {
// "axleCode": "GU6/3.42 REAR AXLE RATIO",
// "axleCount": 2,
// "bodyStyle": "PU",
@@ -671,8 +712,8 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS
txEnvelope.dms_unsold === true
? ""
: moment()
// .tz(JobData.bodyshop.timezone)
.format("YYYY-MM-DD"),
// .tz(JobData.bodyshop.timezone)
.format("YYYY-MM-DD"),
// "deliveryMileage": 4,
// "doorsQuantity": 4,
// "engineNumber": "",
@@ -689,8 +730,8 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS
: String(JobData.plate_no).replace(/([^\w]|_)/g, "").length === 0
? null
: String(JobData.plate_no)
.replace(/([^\w]|_)/g, "")
.toUpperCase(),
.replace(/([^\w]|_)/g, "")
.toUpperCase(),
make: txEnvelope.dms_make,
// "model": "CC10753",
modelAbrev: txEnvelope.dms_model,
@@ -781,18 +822,16 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS
// "warrantyExpDate": "2015-01-12",
// "wheelbase": ""
},
"owners": [
owners: [
{
"id": {
id: {
assigningPartyId: "CURRENT",
value: DMSCust.customerId
},
}
}
],
]
//"inventoryAccount": "237"
},
}
});
return result.data;
} catch (error) {
@@ -808,18 +847,14 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS
async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust, selectedCustomerId, txEnvelope }) {
try {
let ids = [];
//if it's a generic customer, don't update the vehicle owners.
if (selectedCustomerId === JobData.bodyshop.cdk_configuration.generic_customer_number) {
ids = DMSVeh && DMSVeh.owners && DMSVeh.owners;
ids = DMSVeh?.owners && DMSVeh.owners;
} else {
const existingOwnerinVeh =
DMSVeh &&
DMSVeh.owners &&
DMSVeh.owners.find((o) => o.id.value === DMSCust.customerId);
const existingOwnerinVeh = DMSVeh?.owners && DMSVeh.owners.find((o) => o.id.value === DMSCust.customerId);
if (existingOwnerinVeh) {
ids = DMSVeh.owners.map((o) => {
@@ -831,10 +866,7 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust
};
});
} else {
const oldOwner =
DMSVeh &&
DMSVeh.owners &&
DMSVeh.owners.find((o) => o.id.assigningPartyId === "CURRENT");
const oldOwner = DMSVeh?.owners && DMSVeh.owners.find((o) => o.id.assigningPartyId === "CURRENT");
ids = [
{
@@ -845,13 +877,13 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust
},
...(oldOwner
? [
{
id: {
assigningPartyId: "PREVIOUS",
value: oldOwner.id
{
id: {
assigningPartyId: "PREVIOUS",
value: oldOwner.id
}
}
}
]
]
: [])
];
}
@@ -864,7 +896,6 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust
delete DMSVehToSend.invoice;
delete DMSVehToSend.inventoryAccount;
const result = await MakeFortellisCall({
...FortellisActions.UpdateVehicle,
requestSearchParams: {},
@@ -882,28 +913,27 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust
txEnvelope.dms_unsold === true
? ""
: moment(DMSVehToSend.dealer.inServiceDate || txEnvelope.inservicedate)
// .tz(JobData.bodyshop.timezone)
.toISOString()
// .tz(JobData.bodyshop.timezone)
.toISOString()
})
},
vehicle: {
...DMSVehToSend.vehicle,
...(txEnvelope.dms_model_override
? {
make: txEnvelope.dms_make,
modelAbrev: txEnvelope.dms_model
}
make: txEnvelope.dms_make,
modelAbrev: txEnvelope.dms_model
}
: {}),
deliveryDate:
txEnvelope.dms_unsold === true
? ""
: moment(DMSVehToSend.vehicle.deliveryDate)
//.tz(JobData.bodyshop.timezone)
.toISOString()
//.tz(JobData.bodyshop.timezone)
.toISOString()
},
owners: ids
},
}
});
return result;
} catch (error) {
@@ -918,12 +948,18 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust
}
}
async function InsertServiceVehicleHistory({ socket, redisHelpers, JobData, }) {
async function InsertServiceVehicleHistory({ socket, redisHelpers, JobData }) {
try {
const txEnvelope = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.txEnvelope);
const DMSVid = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.DMSVid);
const txEnvelope = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.txEnvelope
);
const DMSVid = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.DMSVid
);
const result = await MakeFortellisCall({
...FortellisActions.ServiceHistoryInsert,
@@ -940,12 +976,11 @@ async function InsertServiceVehicleHistory({ socket, redisHelpers, JobData, }) {
openTime: moment(JobData.actual_in).tz(JobData.bodyshop.timezone).format("HH:mm:ss"),
closeDate: moment(JobData.invoice_date).tz(JobData.bodyshop.timezone).format("YYYY-MM-DD"),
closeTime: moment(JobData.invoice_date).tz(JobData.bodyshop.timezone).format("HH:mm:ss"),
comments: txEnvelope.story, // has to be between 0 and 40.
comments: txEnvelope.story, // has to be between 0 and 40.
cashierId: JobData.bodyshop.cdk_configuration.cashierid,
referenceNumber: JobData.bodyshop.cdk_configuration.cashierid
}
},
}
});
return result;
} catch (error) {
@@ -957,10 +992,13 @@ async function InsertServiceVehicleHistory({ socket, redisHelpers, JobData, }) {
}
}
async function InsertDmsStartWip({ socket, redisHelpers, JobData }) {
try {
const txEnvelope = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.txEnvelope);
const txEnvelope = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.txEnvelope
);
const result = await MakeFortellisCall({
...FortellisActions.StartWip,
@@ -969,21 +1007,20 @@ async function InsertDmsStartWip({ socket, redisHelpers, JobData }) {
socket,
jobid: JobData.id,
body: {
"acctgDate": moment().tz(JobData.bodyshop.timezone).format("YYYY-MM-DD"),
"desc": txEnvelope.story && txEnvelope.story.replace(replaceSpecialRegex, ""),
"docType": "10",
"m13Flag": "0",
"refer": JobData.ro_number,
acctgDate: moment().tz(JobData.bodyshop.timezone).format("YYYY-MM-DD"),
desc: txEnvelope.story && txEnvelope.story.replace(replaceSpecialRegex, ""),
docType: "10",
m13Flag: "0",
refer: JobData.ro_number,
// "rtnCode": "",
// "sendline": "",
// "groupName": "",
"srcCo": JobData.bodyshop.cdk_configuration.srcco,
"srcJrnl": txEnvelope.journal,
"transID": "",
"userID": JobData.bodyshop.cdk_configuration.cashierid,
"userName": "BSMS"
},
srcCo: JobData.bodyshop.cdk_configuration.srcco,
srcJrnl: txEnvelope.journal,
transID: "",
userID: JobData.bodyshop.cdk_configuration.cashierid,
userName: "BSMS"
}
});
return result;
} catch (error) {
@@ -1016,9 +1053,15 @@ async function InsertDmsBatchWip({ socket, redisHelpers, JobData }) {
}
async function GenerateTransWips({ socket, redisHelpers, JobData }) {
const allocations = await CalculateAllocations(socket, JobData.id, true); //3rd prop sets fortellis to true to maintain logging.
//3rd prop sets fortellis to true to maintain logging.
const allocations = await CalculateAllocations(socket, JobData.id, true);
const wips = [];
const DMSTransHeader = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.DMSTransHeader);
const DMSTransHeader = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.DMSTransHeader
);
allocations.forEach((alloc) => {
//Add the sale item from each allocation.
@@ -1027,9 +1070,9 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
acct: alloc.profitCenter.dms_acctnumber,
cntl:
alloc.profitCenter.dms_control_override &&
alloc.profitCenter.dms_control_override !== null &&
alloc.profitCenter.dms_control_override !== undefined &&
alloc.profitCenter.dms_control_override?.trim() !== ""
alloc.profitCenter.dms_control_override !== null &&
alloc.profitCenter.dms_control_override !== undefined &&
alloc.profitCenter.dms_control_override?.trim() !== ""
? alloc.profitCenter.dms_control_override
: JobData.ro_number,
cntl2: null,
@@ -1050,9 +1093,9 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
acct: alloc.costCenter.dms_acctnumber,
cntl:
alloc.costCenter.dms_control_override &&
alloc.costCenter.dms_control_override !== null &&
alloc.costCenter.dms_control_override !== undefined &&
alloc.costCenter.dms_control_override?.trim() !== ""
alloc.costCenter.dms_control_override !== null &&
alloc.costCenter.dms_control_override !== undefined &&
alloc.costCenter.dms_control_override?.trim() !== ""
? alloc.costCenter.dms_control_override
: JobData.ro_number,
cntl2: null,
@@ -1070,9 +1113,9 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
acct: alloc.costCenter.dms_wip_acctnumber,
cntl:
alloc.costCenter.dms_control_override &&
alloc.costCenter.dms_control_override !== null &&
alloc.costCenter.dms_control_override !== undefined &&
alloc.costCenter.dms_control_override?.trim() !== ""
alloc.costCenter.dms_control_override !== null &&
alloc.costCenter.dms_control_override !== undefined &&
alloc.costCenter.dms_control_override?.trim() !== ""
? alloc.costCenter.dms_control_override
: JobData.ro_number,
cntl2: null,
@@ -1089,15 +1132,14 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
}
if (alloc.tax) {
if (alloc.sale.getAmount() > 0) {
const item2 = {
acct: alloc.profitCenter.dms_acctnumber,
cntl:
alloc.profitCenter.dms_control_override &&
alloc.profitCenter.dms_control_override !== null &&
alloc.profitCenter.dms_control_override !== undefined &&
alloc.profitCenter.dms_control_override?.trim() !== ""
alloc.profitCenter.dms_control_override !== null &&
alloc.profitCenter.dms_control_override !== undefined &&
alloc.profitCenter.dms_control_override?.trim() !== ""
? alloc.profitCenter.dms_control_override
: JobData.ro_number,
cntl2: null,
@@ -1113,7 +1155,11 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
}
}
});
const txEnvelope = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.txEnvelope);
const txEnvelope = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.txEnvelope
);
txEnvelope.payers.forEach((payer) => {
const item = {
@@ -1131,7 +1177,13 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
wips.push(item);
});
await redisHelpers.setSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.transWips, wips, defaultFortellisTTL);
await redisHelpers.setSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.transWips,
wips,
defaultFortellisTTL
);
return wips;
}
@@ -1139,8 +1191,11 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
async function PostDmsBatchWip({ socket, redisHelpers, JobData }) {
let DMSTransHeader;
try {
DMSTransHeader = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.DMSTransHeader);
DMSTransHeader = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.DMSTransHeader
);
const result = await MakeFortellisCall({
...FortellisActions.PostBatchWip,
@@ -1149,8 +1204,8 @@ async function PostDmsBatchWip({ socket, redisHelpers, JobData }) {
socket,
jobid: JobData.id,
body: {
"opCode": "P",
"transID": DMSTransHeader.transID
opCode: "P",
transID: DMSTransHeader.transID
}
});
return result;
@@ -1166,8 +1221,11 @@ async function PostDmsBatchWip({ socket, redisHelpers, JobData }) {
async function QueryDmsErrWip({ socket, redisHelpers, JobData }) {
let DMSTransHeader;
try {
DMSTransHeader = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.DMSTransHeader);
DMSTransHeader = await redisHelpers.getSessionTransactionData(
socket.id,
getTransactionType(JobData.id),
FortellisCacheEnums.DMSTransHeader
);
const result = await MakeFortellisCall({
...FortellisActions.QueryErrorWip,
@@ -1176,9 +1234,7 @@ async function QueryDmsErrWip({ socket, redisHelpers, JobData }) {
socket,
jobid: JobData.id,
requestPathParams: DMSTransHeader.transID,
body: {
}
body: {}
});
return result;
} catch (error) {
@@ -1222,11 +1278,14 @@ async function QueryDmsErrWip({ socket, redisHelpers, JobData }) {
// }
async function MarkJobExported({ socket, jobid }) {
CreateFortellisLogEvent(socket, "ERROR", `Marking job as exported for id ${jobid}`);
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
const currentToken =
(socket?.data && socket.data.authToken) || (socket?.handshake?.auth && socket.handshake.auth.token);
const result = await client
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
.setHeaders({ Authorization: `Bearer ${currentToken}` })
.request(queries.MARK_JOB_EXPORTED, {
jobId: jobid,
job: {
@@ -1252,8 +1311,11 @@ async function MarkJobExported({ socket, jobid }) {
async function InsertFailedExportLog({ socket, JobData, error }) {
try {
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
const currentToken =
(socket?.data && socket.data.authToken) || (socket?.handshake?.auth && socket.handshake.auth.token);
const result = await client
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
.setHeaders({ Authorization: `Bearer ${currentToken}` })
.request(queries.INSERT_EXPORT_LOG, {
log: {
bodyshopid: JobData.bodyshop.id,
@@ -1266,7 +1328,10 @@ async function InsertFailedExportLog({ socket, JobData, error }) {
return result;
} catch (error2) {
CreateFortellisLogEvent(socket, "ERROR", `Error in InsertFailedExportLog - ${error}`, { message: error2.message, stack: error2.stack });
CreateFortellisLogEvent(socket, "ERROR", `Error in InsertFailedExportLog - ${error}`, {
message: error2.message,
stack: error2.stack
});
}
}