Additional WIP Fortellis.

This commit is contained in:
Patrick Fic
2025-09-11 08:45:34 -07:00
parent 1728982b2b
commit 661678eb1c
3 changed files with 92 additions and 100 deletions

View File

@@ -3,11 +3,11 @@ const AxiosLib = require("axios").default;
const axios = AxiosLib.create(); const axios = AxiosLib.create();
const uuid = require("uuid").v4; const uuid = require("uuid").v4;
const FORTELLIS_KEY = "X1FxzLyOk3kjHvMbzdPQXFZShkdbgzuo"; const FORTELLIS_KEY = "X1FxzLyOk3kjHvMbzdPQXFZShkdbgzuo"; //TODO: Regenerate these keys after testing and move to env vars.
const FORTELLIS_SECRET = "7Yvs0wpQeHcUS5r95ht8pqOaAvBq7dHV"; const FORTELLIS_SECRET = "7Yvs0wpQeHcUS5r95ht8pqOaAvBq7dHV";
const FORTELLIS_AUTH_URL = "https://identity.fortellis.io/oauth2/aus1p1ixy7YL8cMq02p7/v1/token"; const FORTELLIS_AUTH_URL = "https://identity.fortellis.io/oauth2/aus1p1ixy7YL8cMq02p7/v1/token";
const FORTELLIS_URL = "https://api.fortellis.io"; const FORTELLIS_URL = "https://api.fortellis.io";
const ENVSubscriptionID = "5b527d7d-baf3-40bc-adae-e7a541e37363"; //Given to us by CDK it seems. const ENVSubscriptionID = "5b527d7d-baf3-40bc-adae-e7a541e37363"; //TODO: Replace with the bodyshop.cdk_dealerid
let SubscriptionMeta = null; let SubscriptionMeta = null;
//const ENVSubscriptionID = 'cb59fa04-e53e-4b57-b071-80a48ebc346c'; //const ENVSubscriptionID = 'cb59fa04-e53e-4b57-b071-80a48ebc346c';

View File

@@ -87,7 +87,7 @@ async function FetchSubscriptions({ redisHelpers, socket, jobid }) {
return SubscriptionMeta; return SubscriptionMeta;
} }
} catch (error) { } catch (error) {
CreateFortellisLogEvent(socket, "ERROR", `Error fetching subscription metadata`, { CreateFortellisLogEvent(socket, "ERROR", `Error fetching subscription metadata.`, {
error: error.message, error: error.message,
stack: error.stack stack: error.stack
}); });
@@ -315,22 +315,22 @@ const FortellisActions = {
url: isProduction url: isProduction
? "https://api.fortellis.io/cdk/drive/glpost/startWIP" ? "https://api.fortellis.io/cdk/drive/glpost/startWIP"
: "https://api.fortellis.io/cdk-test/drive/glpost/startWIP", : "https://api.fortellis.io/cdk-test/drive/glpost/startWIP",
type: "put", type: "post",
apiName: "CDK Drive Post Accounting GL", apiName: "CDK Drive Post Accounts GL WIP",
}, },
TranBatchWip: { TranBatchWip: {
url: isProduction url: isProduction
? "https://api.fortellis.io/cdk/drive/glpost/transBatchWIP" ? "https://api.fortellis.io/cdk/drive/glpost/transBatchWIP"
: "https://api.fortellis.io/cdk-test/drive/glpost/transBatchWIP", : "https://api.fortellis.io/cdk-test/drive/glpost/transBatchWIP",
type: "put", type: "post",
apiName: "CDK Drive Post Accounting GL", apiName: "CDK Drive Post Accounts GL WIP",
}, },
PostBatchWip: { PostBatchWip: {
url: isProduction url: isProduction
? "https://api.fortellis.io/cdk/drive/glpost/transBatchWIP" ? "https://api.fortellis.io/cdk/drive/glpost/postBatchWIP"
: "https://api.fortellis.io/cdk-test/drive/glpost/postBatchWIP", : "https://api.fortellis.io/cdk-test/drive/glpost/postBatchWIP",
type: "put", type: "post",
apiName: "CDK Drive Post Accounting GL", apiName: "CDK Drive Post Accounts GL WIP",
}, },
}; };

View File

@@ -95,23 +95,24 @@ async function FortellisJobExport({
async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, selectedCustomerId, jobid }) { async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, selectedCustomerId, jobid }) {
try {
const {
// setSessionData,
// getSessionData,
// addUserSocketMapping,
// removeUserSocketMapping,
// refreshUserSocketTTL,
// getUserSocketMappingByBodyshop,
setSessionTransactionData,
getSessionTransactionData,
//clearSessionTransactionData
} = redisHelpers;
await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.selectedCustomerId, selectedCustomerId, defaultFortellisTTL); const {
const JobData = await getSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.JobData); // setSessionData,
const txEnvelope = await getSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.txEnvelope); // getSessionData,
const DMSVid = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.DMSVid); // addUserSocketMapping,
// removeUserSocketMapping,
// refreshUserSocketTTL,
// getUserSocketMappingByBodyshop,
setSessionTransactionData,
getSessionTransactionData,
//clearSessionTransactionData
} = redisHelpers;
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);
try {
let DMSCust; let DMSCust;
@@ -181,7 +182,7 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele
CreateFortellisLogEvent( CreateFortellisLogEvent(
socket, socket,
"DEBUG", "DEBUG",
`{6} Attempting to post Transaction with ID ${socket.DMSTransHeader.transID}` `{6} Attempting to post Transaction with ID ${DMSTransHeader.transID}`
); );
const DmsBatchTxnPost = await PostDmsBatchWip({ socket, redisHelpers, JobData }); const DmsBatchTxnPost = await PostDmsBatchWip({ socket, redisHelpers, JobData });
@@ -191,17 +192,17 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele
//something //something
CreateFortellisLogEvent(socket, "DEBUG", `{6} Successfully posted sransaction to DMS.`); CreateFortellisLogEvent(socket, "DEBUG", `{6} Successfully posted sransaction to DMS.`);
await MarkJobExported(socket, socket.JobData.id); await MarkJobExported({ socket, jobid: JobData.id });
CreateFortellisLogEvent(socket, "DEBUG", `{5} Updating Service Vehicle History.`); CreateFortellisLogEvent(socket, "DEBUG", `{5} Updating Service Vehicle History.`);
socket.DMSVehHistory = await InsertServiceVehicleHistory(socket); socket.DMSVehHistory = await InsertServiceVehicleHistory(socket);
socket.emit("export-success", socket.JobData.id); socket.emit("export-success", JobData.id);
} else { } else {
//Get the error code //Get the error code
CreateFortellisLogEvent( CreateFortellisLogEvent(
socket, socket,
"DEBUG", "DEBUG",
`{6.1} Getting errors for Transaction ID ${socket.DMSTransHeader.transID}` `{6.1} Getting errors for Transaction ID ${DMSTransHeader.transID}`
); );
// socket.DmsError = await QueryDmsErrWip(socket); // socket.DmsError = await QueryDmsErrWip(socket);
// //Delete the transaction // //Delete the transaction
@@ -219,7 +220,11 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele
} }
} catch (error) { } catch (error) {
// CdkBase.createLogEvent(socket, "ERROR", `Error encountered in CdkSelectedCustomer. ${error}`); // CdkBase.createLogEvent(socket, "ERROR", `Error encountered in CdkSelectedCustomer. ${error}`);
// await InsertFailedExportLog(socket, error); CreateFortellisLogEvent(socket, "ERROR", `Error in FortellisSelectedCustomer - ${error}`, {
error: error.message,
stack: error.stack
});
await InsertFailedExportLog({ socket, JobData, error });
} finally { } finally {
//Ensure we always insert logEvents //Ensure we always insert logEvents
//GQL to insert logevents. //GQL to insert logevents.
@@ -872,7 +877,7 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust
...DMSVehToSend, ...DMSVehToSend,
dealer: { dealer: {
...DMSVehToSend.dealer, //TODO: Check why company is blank on a queried record. ...DMSVehToSend.dealer, //TODO: Check why company is blank on a queried record.
comapny: "77", company: "77",
...((txEnvelope.inservicedate || DMSVehToSend.dealer.inServiceDate) && { ...((txEnvelope.inservicedate || DMSVehToSend.dealer.inServiceDate) && {
inServiceDate: inServiceDate:
txEnvelope.dms_unsold === true txEnvelope.dms_unsold === true
@@ -1095,6 +1100,8 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust
async function InsertDmsStartWip({ socket, redisHelpers, JobData }) { async function InsertDmsStartWip({ socket, redisHelpers, JobData }) {
try { try {
const txEnvelope = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.txEnvelope);
const result = await MakeFortellisCall({ const result = await MakeFortellisCall({
...FortellisActions.StartWip, ...FortellisActions.StartWip,
headers: {}, headers: {},
@@ -1102,18 +1109,18 @@ async function InsertDmsStartWip({ socket, redisHelpers, JobData }) {
socket, socket,
jobid: JobData.id, jobid: JobData.id,
body: { body: {
"acctgDate": moment().tz(socket.JobData.bodyshop.timezone).format("YYYY-MM-DD"), "acctgDate": moment().tz(JobData.bodyshop.timezone).format("YYYY-MM-DD"),
"desc": socket.txEnvelope.story && socket.txEnvelope.story.replace(replaceSpecialRegex, ""), "desc": txEnvelope.story && txEnvelope.story.replace(replaceSpecialRegex, ""),
"docType": "10", "docType": "10",
"m13Flag": "0", "m13Flag": "0",
"refer": socket.JobData.ro_number, "refer": JobData.ro_number,
// "rtnCode": "", // "rtnCode": "",
// "sendline": "", // "sendline": "",
// "groupName": "", // "groupName": "",
"srcCo": socket.JobData.bodyshop.cdk_configuration.srcco, "srcCo": JobData.bodyshop.cdk_configuration.srcco,
"srcJrnl": socket.txEnvelope.journal, "srcJrnl": txEnvelope.journal,
"transID": "", "transID": "",
"userID": socket.JobData.bodyshop.cdk_configuration.cashierid, "userID": JobData.bodyshop.cdk_configuration.cashierid,
"userName": "BSMS" "userName": "BSMS"
}, },
@@ -1136,7 +1143,7 @@ async function InsertDmsBatchWip({ socket, redisHelpers, JobData }) {
}); });
return result; return result;
} catch (error) { } catch (error) {
CreateFortellisLogEvent(socket, "ERROR", `Error in InsertDmsBatchWip - ${error}`, { request: error.request }); CreateFortellisLogEvent(socket, "ERROR", `Error in InsertDmsBatchWip - ${error}`, { request: error.request, stack: error.stack });
} }
} }
@@ -1172,7 +1179,7 @@ async function InsertDmsBatchWip({ socket, redisHelpers, JobData }) {
// } // }
async function GenerateTransWips({ socket, redisHelpers, JobData }) { async function GenerateTransWips({ socket, redisHelpers, JobData }) {
const allocations = await CalculateAllocations(socket, socket.JobData.id, true); //3rd prop sets fortellis to true to maintain logging. const allocations = await CalculateAllocations(socket, JobData.id, true); //3rd prop sets fortellis to true to maintain logging.
const wips = []; 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);
@@ -1245,22 +1252,6 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) {
} }
if (alloc.tax) { if (alloc.tax) {
// if (alloc.cost.getAmount() > 0) {
// const item = {
// acct: alloc.costCenter.dms_acctnumber,
// cntl: JobData.ro_number,
// cntl2: null,
// credtMemoNo: null,
// postAmt: alloc.cost.getAmount(),
// postDesc: null,
// prod: null,
// statCnt: 1,
// transID: DMSTransHeader.transID,
// trgtCoID: JobData.bodyshop.cdk_configuration.srcco,
// };
// wips.push(item);
// }
if (alloc.sale.getAmount() > 0) { if (alloc.sale.getAmount() > 0) {
const item2 = { const item2 = {
@@ -1421,53 +1412,54 @@ async function PostDmsBatchWip({ socket, redisHelpers, JobData }) {
// } // }
// } // }
// async function MarkJobExported(socket, jobid) { async function MarkJobExported({ socket, jobid }) {
// CdkBase.createLogEvent(socket, "DEBUG", `Marking job as exported for id ${jobid}`);
// const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
// const result = await client
// .setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
// .request(queries.MARK_JOB_EXPORTED, {
// jobId: jobid,
// job: {
// status: socket.JobData.bodyshop.md_ro_statuses.default_exported || "Exported*",
// date_exported: new Date()
// },
// log: {
// bodyshopid: socket.JobData.bodyshop.id,
// jobid: jobid,
// successful: true,
// useremail: socket.user.email,
// metadata: socket.transWips
// },
// bill: {
// exported: true,
// exported_at: new Date()
// }
// });
// return result; CreateFortellisLogEvent(socket, "ERROR", `Marking job as exported for id ${jobid}`);
// } const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
const result = await client
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
.request(queries.MARK_JOB_EXPORTED, {
jobId: jobid,
job: {
status: socket.JobData.bodyshop.md_ro_statuses.default_exported || "Exported*",
date_exported: new Date()
},
log: {
bodyshopid: socket.JobData.bodyshop.id,
jobid: jobid,
successful: true,
useremail: socket.user.email,
metadata: socket.transWips
},
bill: {
exported: true,
exported_at: new Date()
}
});
// async function InsertFailedExportLog(socket, error) { return result;
// try { }
// 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: JSON.stringify(error),
// useremail: socket.user.email
// }
// });
// return result; async function InsertFailedExportLog({ socket, JobData, error }) {
// } catch (error2) { try {
// CdkBase.createLogEvent(socket, "ERROR", `Error in InsertFailedExportLog - ${error} - ${JSON.stringify(error2)}`); 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: JobData.bodyshop.id,
jobid: JobData.id,
successful: false,
message: JSON.stringify(error),
useremail: socket.user.email
}
});
return result;
} catch (error2) {
CreateFortellisLogEvent(socket, "ERROR", `Error in InsertFailedExportLog - ${error}`, { message: error2.message, stack: error2.stack });
}
}
exports.getTransactionType = getTransactionType; exports.getTransactionType = getTransactionType;
exports.FortellisJobExport = FortellisJobExport; exports.FortellisJobExport = FortellisJobExport;