diff --git a/server/fortellis/fortellis-helpers.js b/server/fortellis/fortellis-helpers.js index 9eed6669c..3cc66fc3a 100644 --- a/server/fortellis/fortellis-helpers.js +++ b/server/fortellis/fortellis-helpers.js @@ -3,17 +3,29 @@ require("dotenv").config({ path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`) }); -const GraphQLClient = require("graphql-request").GraphQLClient; // const CalcualteAllocations = require("../cdk/cdk-calculate-allocations").default; -const InstanceMgr = require("../utils/instanceMgr").default; const CreateFortellisLogEvent = require("./fortellis-logger"); -const queries = require("../graphql-client/queries"); const logger = require("../utils/logger"); const uuid = require("uuid").v4; const AxiosLib = require("axios").default; const axios = AxiosLib.create(); const axiosCurlirize = require('axios-curlirize').default; +// Custom error class for Fortellis API errors +class FortellisApiError extends Error { + constructor(message, details) { + super(message); + this.name = 'FortellisApiError'; + this.reqId = details.reqId; + this.url = details.url; + this.apiName = details.apiName; + this.errorData = details.errorData; + this.errorStatus = details.errorStatus; + this.errorStatusText = details.errorStatusText; + this.originalError = details.originalError; + } +} + axiosCurlirize(axios, (result, err) => { const { command } = result; console.log("*** ~ axiosCurlirize ~ command:", command); @@ -206,6 +218,24 @@ async function MakeFortellisCall({ } catch (error) { console.log(`ReqID: ${ReqId} Error`, error.response?.data); //console.log(`ReqID: ${ReqId} Full Error`, JSON.stringify(error, null, 4)); + + const errorDetails = { + reqId: ReqId, + url: fullUrl, + apiName, + errorData: error.response?.data, + errorStatus: error.response?.status, + errorStatusText: error.response?.statusText, + originalError: error + }; + + // CreateFortellisLogEvent(socket, "ERROR", `Error in MakeFortellisCall for ${apiName}: ${error.message}`, { + // ...errorDetails, + // errorStack: error.stack + // }); + + // Throw custom error with all the details + throw new FortellisApiError(`Fortellis API call failed for ${apiName}: ${error.message}`, errorDetails); } } @@ -332,6 +362,13 @@ const FortellisActions = { type: "post", apiName: "CDK Drive Post Accounts GL WIP", }, + ServiceHistoryInsert: { + url: isProduction + ? "https://api.fortellis.io/cdk/drive/post/service-vehicle-history-mgmt/v2/" + : "https://api.fortellis.io/cdk-test/drive/post/service-vehicle-history-mgmt/v2/", + type: "post", + apiName: "CDK Drive Post Service Vehicle History", + }, }; @@ -349,7 +386,8 @@ const FortellisCacheEnums = { selectedCustomerId: "selectedCustomerId", DMSTransHeader: "DMSTransHeader", transWips: "transWips", - DmsBatchTxnPost: "DmsBatchTxnPost" + DmsBatchTxnPost: "DmsBatchTxnPost", + DMSVehHistory: "DMSVehHistory", }; function constructFullUrl({ url, pathParams = "", requestSearchParams = [] }) { @@ -369,5 +407,6 @@ module.exports = { MakeFortellisCall, FortellisActions, getTransactionType, - defaultFortellisTTL + defaultFortellisTTL, + FortellisApiError }; diff --git a/server/fortellis/fortellis.js b/server/fortellis/fortellis.js index 587d6d52f..ba698cb7a 100644 --- a/server/fortellis/fortellis.js +++ b/server/fortellis/fortellis.js @@ -8,13 +8,48 @@ const { FortellisActions, getTransactionType, defaultFortellisTTL, - FortellisCacheEnums + FortellisCacheEnums, + FortellisApiError } = require("./fortellis-helpers"); const _ = require("lodash"); const moment = require("moment-timezone"); const replaceSpecialRegex = /[^a-zA-Z0-9 .,\n #]+/g; +// Helper function to handle FortellisApiError logging +function handleFortellisApiError(socket, error, functionName, additionalDetails = {}) { + if (error instanceof FortellisApiError) { + CreateFortellisLogEvent(socket, "ERROR", `${functionName} failed: ${error.message}`, { + 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, + stack: error.stack, + functionName, + ...additionalDetails + }); + } +} + async function FortellisJobExport({ socket, redisHelpers, @@ -34,7 +69,7 @@ async function FortellisJobExport({ // clearSessionTransactionData } = redisHelpers; try { - CreateFortellisLogEvent(socket, "debug", `Received Job export request for id ${jobid}`); + CreateFortellisLogEvent(socket, "DEBUG", `Received Job export request for id ${jobid}`); await setSessionTransactionData( socket.id, getTransactionType(jobid), @@ -85,7 +120,7 @@ async function FortellisJobExport({ ]); } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in FortellisJobExport - ${error}`, { + CreateFortellisLogEvent(socket, "ERROR", `Error in FortellisJobExport - ${error} `, { error: error.message, stack: error.stack }); @@ -160,6 +195,8 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele 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, @@ -190,12 +227,13 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele if (DmsBatchTxnPost.rtnCode === "0") { //TODO: Validate this is a string and not # //something - CreateFortellisLogEvent(socket, "DEBUG", `{6} Successfully posted sransaction to DMS.`); + CreateFortellisLogEvent(socket, "DEBUG", `{6} Successfully posted transaction to DMS.`); await MarkJobExported({ socket, jobid: JobData.id }); CreateFortellisLogEvent(socket, "DEBUG", `{5} Updating Service Vehicle History.`); - socket.DMSVehHistory = await InsertServiceVehicleHistory(socket); + const DMSVehHistory = await InsertServiceVehicleHistory({ socket, redisHelpers, JobData }); + await setSessionTransactionData(socket.id, getTransactionType(jobid), FortellisCacheEnums.DMSVehHistory, DMSVehHistory, defaultFortellisTTL); socket.emit("export-success", JobData.id); } else { //Get the error code @@ -206,7 +244,7 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele ); // socket.DmsError = await QueryDmsErrWip(socket); // //Delete the transaction - // CdkBase.createLogEvent(socket, "DEBUG", `{6.2} Deleting Transaction ID ${socket.DMSTransHeader.transID}`); + // CdkBase.createLogEvent(socket, "DEBUG", `{ 6.2 } Deleting Transaction ID ${ socket.DMSTransHeader.transID } `); // socket.DmsBatchTxnPost = await DeleteDmsWip(socket); // socket.DmsError.errMsg @@ -215,12 +253,12 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, ioHelpers, sele // (e) => // e !== null && // e !== "" && - // CdkBase.createLogEvent(socket, "ERROR", `Error(s) encountered in posting transaction. ${e}`) + // CdkBase.createLogEvent(socket, "ERROR", `Error(s) encountered in posting transaction.${ e } `) // ); } } catch (error) { - // CdkBase.createLogEvent(socket, "ERROR", `Error encountered in CdkSelectedCustomer. ${error}`); - CreateFortellisLogEvent(socket, "ERROR", `Error in FortellisSelectedCustomer - ${error}`, { + // CdkBase.createLogEvent(socket, "ERROR", `Error encountered in CdkSelectedCustomer.${ error } `); + CreateFortellisLogEvent(socket, "ERROR", `Error in FortellisSelectedCustomer - ${error} `, { error: error.message, stack: error.stack }); @@ -257,7 +295,11 @@ async function CalculateDmsVid({ socket, JobData, redisHelpers }) { }); return result; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in CalculateDmsVid - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "CalculateDmsVid", { + vin: JobData.v_vin, + jobId: JobData.id + }); + throw error; // Re-throw to maintain existing error handling flow } } @@ -274,7 +316,11 @@ async function QueryDmsVehicleById({ socket, redisHelpers, JobData, DMSVid }) { }); return result; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in QueryDmsVehicleById - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "QueryDmsVehicleById", { + vehicleId: DMSVid.vehiclesVehId, + jobId: JobData.id + }); + throw error; } } @@ -292,7 +338,11 @@ async function QueryDmsCustomerById({ socket, redisHelpers, JobData, CustomerId }); return result.data; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in QueryDmsCustomerById - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "QueryDmsCustomerById", { + customerId: CustomerId, + jobId: JobData.id + }); + throw error; } } @@ -305,7 +355,8 @@ async function QueryDmsCustomerByName({ socket, redisHelpers, JobData }) { CreateFortellisLogEvent( socket, "DEBUG", - `Begin query DMS Customer by Name using ${JSON.stringify(ownerName)}` + `Begin query DMS Customer by Name using ${JSON.stringify(ownerName) + } ` ); try { @@ -321,7 +372,11 @@ async function QueryDmsCustomerByName({ socket, redisHelpers, JobData }) { }); return result.data; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in QueryDmsCustomerByName - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "QueryDmsCustomerByName", { + searchParams: ownerName, + jobId: JobData.id + }); + throw error; } } @@ -520,89 +575,16 @@ async function InsertDmsCustomer({ socket, redisHelpers, JobData }) { }); return result; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in InsertDmsCustomer - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "InsertDmsCustomer", { + customerData: { + firstName: JobData.ownr_fn, + lastName: JobData.ownr_ln, + companyName: JobData.ownr_co_nm + }, + jobId: JobData.id + }); + throw error; } - - // try { - // const soapClientCustomerInsertUpdate = await soap.createClientAsync(CdkWsdl.CustomerInsertUpdate); - // const soapResponseCustomerInsertUpdate = await soapClientCustomerInsertUpdate.insertAsync( - // { - // arg0: CDK_CREDENTIALS, - // arg1: { dealerId: socket.JobData.bodyshop.cdk_dealerid }, - // arg2: { userId: null }, - // arg3: { - // //Copied the required fields from the other integration. - // //TODO: Verify whether we need to bring more information in. - // id: { value: newCustomerNumber }, - // address: { - // addressLine: socket.JobData.ownr_addr1 && socket.JobData.ownr_addr1.replace(replaceSpecialRegex, ""), - // city: socket.JobData.ownr_city && socket.JobData.ownr_city.replace(replaceSpecialRegex, ""), - // country: socket.JobData.ownr_ctry && socket.JobData.ownr_ctry.replace(replaceSpecialRegex, ""), - // postalCode: InstanceMgr({ - // imex: - // socket.JobData.ownr_zip && - // socket.JobData.ownr_zip //TODO Need to remove for US Based customers. - // .toUpperCase() - // .replace(/\W/g, "") - // .replace(/(...)/, "$1 "), - // rome: socket.JobData.ownr_zip - // }), - // stateOrProvince: socket.JobData.ownr_st && socket.JobData.ownr_st.replace(replaceSpecialRegex, "") - // }, - // contactInfo: { - // mainTelephoneNumber: { - // main: true, - // value: socket.JobData.ownr_ph1 && socket.JobData.ownr_ph1.replace(replaceSpecialRegex, "") - // }, - // email: { - // desc: socket.JobData.ownr_ea ? "Other" : "CustomerDeclined", - // value: socket.JobData.ownr_ea ? socket.JobData.ownr_ea : null - // } - // }, - // demographics: null, - // name1: { - // companyName: - // socket.JobData.ownr_co_nm && socket.JobData.ownr_co_nm.replace(replaceSpecialRegex, "").toUpperCase(), - - // firstName: socket.JobData.ownr_fn && socket.JobData.ownr_fn.replace(replaceSpecialRegex, "").toUpperCase(), - // fullname: null, - // lastName: socket.JobData.ownr_ln && socket.JobData.ownr_ln.replace(replaceSpecialRegex, "").toUpperCase(), - // middleName: null, - // nameType: - // socket.JobData.ownr_co_nm && String(socket.JobData.ownr_co_nm).trim() !== "" ? "Business" : "Person", - // suffix: null, - // title: null - // } - // } - // }, - - // {} - // ); - - // const [result, rawResponse, , rawRequest] = soapResponseCustomerInsertUpdate; - // CdkBase.createXmlEvent(socket, rawRequest, `soapClientCustomerInsertUpdate.insertAsync request.`); - - // CdkBase.createXmlEvent(socket, rawResponse, `soapClientCustomerInsertUpdate.insertAsync response.`); - // CdkBase.createLogEvent( - // socket, - // "SILLY", - // `soapClientCustomerInsertUpdate.insertAsync Result ${JSON.stringify(result, null, 2)}` - // ); - // CheckCdkResponseForError(socket, soapResponseCustomerInsertUpdate); - // const customer = result && result.return && result.return.customerParty; - // return customer; - // } catch (error) { - // CdkBase.createXmlEvent(socket, error.request, `soapClientCustomerInsertUpdate.insertAsync request.`, true); - - // CdkBase.createXmlEvent( - // socket, - // error.response && error.response.data, - // `soapClientCustomerInsertUpdate.insertAsync response.`, - // true - // ); - // CdkBase.createLogEvent(socket, "ERROR", `Error in InsertDmsCustomer - ${error}`); - // throw new Error(error); - // } } async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMSVid, DMSCust }) { @@ -803,7 +785,13 @@ async function InsertDmsVehicle({ socket, redisHelpers, JobData, txEnvelope, DMS }); return result.data; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in InsertDmsVehicle - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "InsertDmsVehicle", { + vin: JobData.v_vin, + vehicleId: DMSVid.vehiclesVehId, + customerId: DMSCust.customerId, + jobId: JobData.id + }); + throw error; } } @@ -877,7 +865,7 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust ...DMSVehToSend, dealer: { ...DMSVehToSend.dealer, //TODO: Check why company is blank on a queried record. - company: "77", + //company: "77", ...((txEnvelope.inservicedate || DMSVehToSend.dealer.inServiceDate) && { inServiceDate: txEnvelope.dms_unsold === true @@ -908,194 +896,55 @@ async function UpdateDmsVehicle({ socket, redisHelpers, JobData, DMSVeh, DMSCust }); return result; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in UpdateDmsVehicle - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "UpdateDmsVehicle", { + vin: JobData.v_vin, + vehicleId: DMSVeh?.dealer?.vehicleId, + customerId: DMSCust?.customerId, + selectedCustomerId, + jobId: JobData.id + }); + throw error; } - - // - - // try { - // const soapClientVehicleInsertUpdate = await soap.createClientAsync(CdkWsdl.VehicleInsertUpdate); - - // let ids = []; - - // //if it's a generic customer, don't update the vehicle owners. - - // if (socket.selectedCustomerId === socket.JobData.bodyshop.cdk_configuration.generic_customer_number) { - // ids = socket.DMSVeh && socket.DMSVeh.owners && socket.DMSVeh.owners; - // } else { - // const existingOwnerinVeh = - // socket.DMSVeh && - // socket.DMSVeh.owners && - // socket.DMSVeh.owners.find((o) => o.id.value === socket.DMSCust.id.value); - - // if (existingOwnerinVeh) { - // ids = socket.DMSVeh.owners.map((o) => { - // return { - // id: { - // assigningPartyId: o.id.value === socket.DMSCust.id.value ? "CURRENT" : "PREVIOUS", - // value: o.id.value - // } - // }; - // }); - // } else { - // const oldOwner = - // socket.DMSVeh && - // socket.DMSVeh.owners && - // socket.DMSVeh.owners.find((o) => o.id.assigningPartyId === "CURRENT"); - - // ids = [ - // { - // id: { - // assigningPartyId: "CURRENT", - // value: socket.DMSCust.id.value - // } - // }, - // ...(oldOwner - // ? [ - // { - // id: { - // assigningPartyId: "PREVIOUS", - // value: oldOwner.id.value - // } - // } - // ] - // : []) - // ]; - // } - // } - - // const soapResponseVehicleInsertUpdate = await soapClientVehicleInsertUpdate.updateAsync({ - // arg0: CDK_CREDENTIALS, - // arg1: { id: socket.JobData.bodyshop.cdk_dealerid }, - // arg2: { - // ...socket.DMSVeh, - // dealer: { - // ...socket.DMSVeh.dealer, - // ...((socket.txEnvelope.inservicedate || socket.DMSVeh.dealer.inServiceDate) && { - // inServiceDate: - // socket.txEnvelope.dms_unsold === true - // ? "" - // : moment(socket.DMSVeh.dealer.inServiceDate || socket.txEnvelope.inservicedate) - // // .tz(socket.JobData.bodyshop.timezone) - // .toISOString() - // }) - // }, - // vehicle: { - // ...socket.DMSVeh.vehicle, - // ...(socket.txEnvelope.dms_model_override - // ? { - // make: socket.txEnvelope.dms_make, - // modelAbrev: socket.txEnvelope.dms_model - // } - // : {}), - // deliveryDate: - // socket.txEnvelope.dms_unsold === true - // ? "" - // : moment(socket.DMSVeh.vehicle.deliveryDate) - // //.tz(socket.JobData.bodyshop.timezone) - // .toISOString() - // }, - // owners: ids - // }, - // arg3: "VEHICLES" - // }); - // const [result, rawResponse, , rawRequest] = soapResponseVehicleInsertUpdate; - - // CdkBase.createXmlEvent(socket, rawRequest, `soapClientVehicleInsertUpdate.updateAsync request.`); - - // CdkBase.createLogEvent( - // socket, - // "DEBUG", - // `soapClientVehicleInsertUpdate.updateAsync Result ${JSON.stringify(result, null, 2)}` - // ); - // CdkBase.createXmlEvent(socket, rawResponse, `soapClientVehicleInsertUpdate.updateAsync response.`); - // CheckCdkResponseForError(socket, soapResponseVehicleInsertUpdate); - // const VehicleFromDMS = result && result.return && result.return.vehicle; - // return VehicleFromDMS; - // } catch (error) { - // CdkBase.createLogEvent(socket, "ERROR", `Error in UpdateDmsVehicle - ${error}`); - // throw new Error(error); - // } } -// async function InsertServiceVehicleHistory(socket) { -// try { -// const soapClientServiceHistoryInsert = await soap.createClientAsync(CdkWsdl.ServiceHistoryInsert); -// const soapResponseServiceHistoryInsert = await soapClientServiceHistoryInsert.serviceHistoryHeaderInsertAsync({ -// authToken: CDK_CREDENTIALS, -// dealerId: { dealerId: socket.JobData.bodyshop.cdk_dealerid }, -// serviceHistoryHeader: { -// vehID: socket.DMSVid.vehiclesVehId, -// roNumber: socket.JobData.ro_number.match(/\d+/g), -// mileage: socket.txEnvelope.kmout, -// openDate: moment(socket.JobData.actual_in).tz(socket.JobData.bodyshop.timezone).format("YYYY-MM-DD"), -// openTime: moment(socket.JobData.actual_in).tz(socket.JobData.bodyshop.timezone).format("HH:mm:ss"), -// closeDate: moment(socket.JobData.invoice_date).tz(socket.JobData.bodyshop.timezone).format("YYYY-MM-DD"), -// closeTime: moment(socket.JobData.invoice_date).tz(socket.JobData.bodyshop.timezone).format("HH:mm:ss"), -// comments: socket.txEnvelope.story, -// cashierID: socket.JobData.bodyshop.cdk_configuration.cashierid -// } -// }); +async function InsertServiceVehicleHistory({ socket, redisHelpers, JobData, }) { -// const [result, rawResponse, , rawRequest] = soapResponseServiceHistoryInsert; + 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); -// CdkBase.createXmlEvent(socket, rawRequest, `soapClientServiceHistoryInsert.serviceHistoryHeaderInsert request.`); + const result = await MakeFortellisCall({ + ...FortellisActions.ServiceHistoryInsert, + headers: {}, + redisHelpers, + socket, + jobid: JobData.id, + body: { + header: { + vehId: DMSVid.vehiclesVehId, + roNumber: JobData.ro_number.match(/\d+/g)[0], + mileage: txEnvelope.kmout, + openDate: moment(JobData.actual_in).tz(JobData.bodyshop.timezone).format("YYYY-MM-DD"), + 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. + cashierId: JobData.bodyshop.cdk_configuration.cashierid, + referenceNumber: JobData.bodyshop.cdk_configuration.cashierid + } + }, -// CdkBase.createLogEvent( -// socket, -// "SILLY", -// `soapClientServiceHistoryInsert.serviceHistoryHeaderInsert Result ${JSON.stringify(result, null, 2)}` -// ); -// CdkBase.createXmlEvent(socket, rawResponse, `soapClientServiceHistoryInsert.serviceHistoryHeaderInsert response.`); -// CheckCdkResponseForError(socket, soapResponseServiceHistoryInsert); -// return result && result.return; -// } catch (error) { -// CdkBase.createLogEvent(socket, "ERROR", `Error in InsertServiceVehicleHistory - ${error}`); -// throw new Error(error); -// } -// } - -// async function InsertDmsStartWip(socket) { -// try { -// const soapClientAccountingGLInsertUpdate = await soap.createClientAsync(CdkWsdl.AccountingGLInsertUpdate); - -// const soapResponseAccountingGLInsertUpdate = await soapClientAccountingGLInsertUpdate.doStartWIPAsync({ -// arg0: CDK_CREDENTIALS, -// arg1: { dealerId: socket.JobData.bodyshop.cdk_dealerid }, -// arg2: { -// acctgDate: moment().tz(socket.JobData.bodyshop.timezone).format("YYYY-MM-DD"), -// //socket.JobData.invoice_date -// desc: socket.txEnvelope.story && socket.txEnvelope.story.replace(replaceSpecialRegex, ""), -// docType: 10 || 7, //Need to check what this usually would be? Apparently it is almost always 10 or 7. -// //1 Cash Receipt , 2 Check, 3 Journal Voucher, 4 Parts invoice, 5 Payable Invoice, 6 Recurring Entry, 7 Repair Order Invoice, 8 Vehicle Purchase Invoice, 9 Vehicle Sale Invoice, 10 Other, 11 Payroll, 12 Finance Charge, 13 FMLR Invoice, 14 Parts Credit Memo, 15 Manufacturer Document, 16 FMLR Credit Memo -// m13Flag: 0, -// refer: socket.JobData.ro_number, -// srcCo: socket.JobData.bodyshop.cdk_configuration.srcco, -// srcJrnl: socket.txEnvelope.journal, -// userID: socket.JobData.bodyshop.cdk_configuration.cashierid //Where is this coming from? -// //userName: "IMEX", -// } -// }); - -// const [result, rawResponse, , rawRequest] = soapResponseAccountingGLInsertUpdate; - -// CdkBase.createXmlEvent(socket, rawRequest, `soapClientAccountingGLInsertUpdate.doStartWIPAsync request.`); - -// CdkBase.createLogEvent( -// socket, -// "SILLY", -// `soapClientAccountingGLInsertUpdate.doStartWIPAsync Result ${JSON.stringify(result, null, 2)}` -// ); -// CdkBase.createXmlEvent(socket, rawResponse, `soapClientAccountingGLInsertUpdate.doStartWIPAsync response.`); -// CheckCdkResponseForError(socket, soapResponseAccountingGLInsertUpdate); -// const TransactionHeader = result && result.return; -// return TransactionHeader; -// } catch (error) { -// CdkBase.createLogEvent(socket, "ERROR", `Error in InsertDmsStartWip - ${error}`); -// throw new Error(error); -// } -// } + }); + return result; + } catch (error) { + handleFortellisApiError(socket, error, "InsertServiceVehicleHistory", { + roNumber: JobData.ro_number, + jobId: JobData.id + }); + throw error; + } +} async function InsertDmsStartWip({ socket, redisHelpers, JobData }) { @@ -1127,7 +976,11 @@ async function InsertDmsStartWip({ socket, redisHelpers, JobData }) { }); return result; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in InsertDmsStartWip - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "InsertDmsStartWip", { + roNumber: JobData.ro_number, + jobId: JobData.id + }); + throw error; } } @@ -1143,41 +996,14 @@ async function InsertDmsBatchWip({ socket, redisHelpers, JobData }) { }); return result; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in InsertDmsBatchWip - ${error}`, { request: error.request, stack: error.stack }); + handleFortellisApiError(socket, error, "InsertDmsBatchWip", { + jobId: JobData.id, + errorStack: error.stack + }); + throw error; } } -// async function InsertDmsBatchWip(socket) { -// try { -// const soapClientAccountingGLInsertUpdate = await soap.createClientAsync(CdkWsdl.AccountingGLInsertUpdate); - -// const soapResponseAccountingGLInsertUpdate = await soapClientAccountingGLInsertUpdate.doTransBatchWIPAsync({ -// arg0: CDK_CREDENTIALS, -// arg1: { dealerId: socket.JobData.bodyshop.cdk_dealerid }, -// arg2: { -// transWIPs: await GenerateTransWips(socket) -// } -// }); - -// const [result, rawResponse, , rawRequest] = soapResponseAccountingGLInsertUpdate; - -// CdkBase.createXmlEvent(socket, rawRequest, `soapClientAccountingGLInsertUpdate.doTransBatchWIPAsync request.`); - -// CdkBase.createLogEvent( -// socket, -// "SILLY", -// `soapClientAccountingGLInsertUpdate.doTransBatchWIPAsync Result ${JSON.stringify(result, null, 2)}` -// ); -// CdkBase.createXmlEvent(socket, rawResponse, `soapClientAccountingGLInsertUpdate.doTransBatchWIPAsync response.`); -// CheckCdkResponseForError(socket, soapResponseAccountingGLInsertUpdate); -// const BatchWipResult = result && result.return; -// return BatchWipResult; -// } catch (error) { -// CdkBase.createLogEvent(socket, "ERROR", `Error in InsertDmsBatchWip - ${error}`); -// throw new Error(error); -// } -// } - async function GenerateTransWips({ socket, redisHelpers, JobData }) { const allocations = await CalculateAllocations(socket, JobData.id, true); //3rd prop sets fortellis to true to maintain logging. const wips = []; @@ -1300,8 +1126,9 @@ async function GenerateTransWips({ socket, redisHelpers, JobData }) { } async function PostDmsBatchWip({ socket, redisHelpers, JobData }) { + let DMSTransHeader; try { - const 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({ @@ -1317,41 +1144,14 @@ async function PostDmsBatchWip({ socket, redisHelpers, JobData }) { }); return result; } catch (error) { - CreateFortellisLogEvent(socket, "ERROR", `Error in PostDmsBatchWip - ${error}`, { request: error.request }); + handleFortellisApiError(socket, error, "PostDmsBatchWip", { + transId: DMSTransHeader?.transID, + jobId: JobData.id + }); + throw error; } } -// async function PostDmsBatchWip(socket) { -// try { -// const soapClientAccountingGLInsertUpdate = await soap.createClientAsync(CdkWsdl.AccountingGLInsertUpdate); - -// const soapResponseAccountingGLInsertUpdate = await soapClientAccountingGLInsertUpdate.doPostBatchWIPAsync({ -// arg0: CDK_CREDENTIALS, -// arg1: { dealerId: socket.JobData.bodyshop.cdk_dealerid }, -// arg2: { -// postWIP: { opCode: "P", transID: socket.DMSTransHeader.transID } -// } -// }); - -// const [result, rawResponse, , rawRequest] = soapResponseAccountingGLInsertUpdate; - -// CdkBase.createXmlEvent(socket, rawRequest, `soapClientAccountingGLInsertUpdate.doPostBatchWIPAsync request.`); - -// CdkBase.createLogEvent( -// socket, -// "SILLY", -// `soapClientAccountingGLInsertUpdate.doPostBatchWIPAsync Result ${JSON.stringify(result, null, 2)}` -// ); -// CdkBase.createXmlEvent(socket, rawResponse, `soapClientAccountingGLInsertUpdate.doPostBatchWIPAsync response.`); -// // CheckCdkResponseForError(socket, soapResponseAccountingGLInsertUpdate); -// const PostResult = result && result.return; -// return PostResult; -// } catch (error) { -// CdkBase.createLogEvent(socket, "ERROR", `Error in PostDmsBatchWip - ${error}`); -// throw new Error(error); -// } -// } - // async function QueryDmsErrWip(socket) { // try { // const soapClientAccountingGLInsertUpdate = await soap.createClientAsync(CdkWsdl.AccountingGLInsertUpdate);