From 661678eb1c2a4a82acbbb768f29ed52c907fb0f8 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Thu, 11 Sep 2025 08:45:34 -0700 Subject: [PATCH] Additional WIP Fortellis. --- fortellis.js | 4 +- server/fortellis/fortellis-helpers.js | 16 +-- server/fortellis/fortellis.js | 172 ++++++++++++-------------- 3 files changed, 92 insertions(+), 100 deletions(-) diff --git a/fortellis.js b/fortellis.js index 62494680e..4c6a7553a 100644 --- a/fortellis.js +++ b/fortellis.js @@ -3,11 +3,11 @@ const AxiosLib = require("axios").default; const axios = AxiosLib.create(); 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_AUTH_URL = "https://identity.fortellis.io/oauth2/aus1p1ixy7YL8cMq02p7/v1/token"; 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; //const ENVSubscriptionID = 'cb59fa04-e53e-4b57-b071-80a48ebc346c'; diff --git a/server/fortellis/fortellis-helpers.js b/server/fortellis/fortellis-helpers.js index 64c590fdc..9eed6669c 100644 --- a/server/fortellis/fortellis-helpers.js +++ b/server/fortellis/fortellis-helpers.js @@ -87,7 +87,7 @@ async function FetchSubscriptions({ redisHelpers, socket, jobid }) { return SubscriptionMeta; } } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error fetching subscription metadata`, { + CreateFortellisLogEvent(socket, "ERROR", `Error fetching subscription metadata.`, { error: error.message, stack: error.stack }); @@ -315,22 +315,22 @@ const FortellisActions = { url: isProduction ? "https://api.fortellis.io/cdk/drive/glpost/startWIP" : "https://api.fortellis.io/cdk-test/drive/glpost/startWIP", - type: "put", - apiName: "CDK Drive Post Accounting GL", + type: "post", + apiName: "CDK Drive Post Accounts GL WIP", }, TranBatchWip: { url: isProduction ? "https://api.fortellis.io/cdk/drive/glpost/transBatchWIP" : "https://api.fortellis.io/cdk-test/drive/glpost/transBatchWIP", - type: "put", - apiName: "CDK Drive Post Accounting GL", + type: "post", + apiName: "CDK Drive Post Accounts GL WIP", }, PostBatchWip: { 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", - type: "put", - apiName: "CDK Drive Post Accounting GL", + type: "post", + apiName: "CDK Drive Post Accounts GL WIP", }, }; diff --git a/server/fortellis/fortellis.js b/server/fortellis/fortellis.js index ed0cbe165..587d6d52f 100644 --- a/server/fortellis/fortellis.js +++ b/server/fortellis/fortellis.js @@ -95,23 +95,24 @@ async function FortellisJobExport({ 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 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 { + // setSessionData, + // getSessionData, + // 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; @@ -181,7 +182,7 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele CreateFortellisLogEvent( socket, "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 }); @@ -191,17 +192,17 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele //something 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.`); socket.DMSVehHistory = await InsertServiceVehicleHistory(socket); - socket.emit("export-success", socket.JobData.id); + socket.emit("export-success", JobData.id); } else { //Get the error code CreateFortellisLogEvent( socket, "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); // //Delete the transaction @@ -219,7 +220,11 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele } } catch (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 { //Ensure we always insert logEvents //GQL to insert logevents. @@ -872,7 +877,7 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust ...DMSVehToSend, dealer: { ...DMSVehToSend.dealer, //TODO: Check why company is blank on a queried record. - comapny: "77", + company: "77", ...((txEnvelope.inservicedate || DMSVehToSend.dealer.inServiceDate) && { inServiceDate: txEnvelope.dms_unsold === true @@ -1095,6 +1100,8 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust async function InsertDmsStartWip({ socket, redisHelpers, JobData }) { try { + const txEnvelope = await redisHelpers.getSessionTransactionData(socket.id, getTransactionType(JobData.id), FortellisCacheEnums.txEnvelope); + const result = await MakeFortellisCall({ ...FortellisActions.StartWip, headers: {}, @@ -1102,18 +1109,18 @@ async function InsertDmsStartWip({ socket, redisHelpers, JobData }) { socket, jobid: JobData.id, body: { - "acctgDate": moment().tz(socket.JobData.bodyshop.timezone).format("YYYY-MM-DD"), - "desc": socket.txEnvelope.story && socket.txEnvelope.story.replace(replaceSpecialRegex, ""), + "acctgDate": moment().tz(JobData.bodyshop.timezone).format("YYYY-MM-DD"), + "desc": txEnvelope.story && txEnvelope.story.replace(replaceSpecialRegex, ""), "docType": "10", "m13Flag": "0", - "refer": socket.JobData.ro_number, + "refer": JobData.ro_number, // "rtnCode": "", // "sendline": "", // "groupName": "", - "srcCo": socket.JobData.bodyshop.cdk_configuration.srcco, - "srcJrnl": socket.txEnvelope.journal, + "srcCo": JobData.bodyshop.cdk_configuration.srcco, + "srcJrnl": txEnvelope.journal, "transID": "", - "userID": socket.JobData.bodyshop.cdk_configuration.cashierid, + "userID": JobData.bodyshop.cdk_configuration.cashierid, "userName": "BSMS" }, @@ -1136,7 +1143,7 @@ async function InsertDmsBatchWip({ socket, redisHelpers, JobData }) { }); return result; } 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 }) { - 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 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.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) { const item2 = { @@ -1421,53 +1412,54 @@ async function PostDmsBatchWip({ socket, redisHelpers, JobData }) { // } // } -// 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() -// } -// }); +async function MarkJobExported({ socket, jobid }) { -// 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) { -// 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; +} -// return result; -// } catch (error2) { -// CdkBase.createLogEvent(socket, "ERROR", `Error in InsertFailedExportLog - ${error} - ${JSON.stringify(error2)}`); -// } -// } +async function InsertFailedExportLog({ socket, JobData, error }) { + 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: 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.FortellisJobExport = FortellisJobExport;