Additional Fortellis functionality on delete.

This commit is contained in:
Patrick Fic
2025-11-14 08:36:42 -08:00
parent a8dcc542cc
commit 7af70f7512
8 changed files with 91 additions and 624 deletions

View File

@@ -157,7 +157,7 @@ async function QueryJobData(socket, jobid) {
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
.request(queries.QUERY_JOBS_FOR_PBS_EXPORT, { id: jobid });
WsLogger.createLogEvent(socket, "DEBUG", `Job data query result ${JSON.stringify(result, null, 2)}`);
//WsLogger.createLogEvent(socket, "DEBUG", `Job data query result ${JSON.stringify(result, null, 2)}`);
return result.jobs_by_pk;
}
@@ -687,13 +687,13 @@ async function InsertFailedExportLog(socket, error) {
const result = await client
.setHeaders({ Authorization: `Bearer ${socket.handshake.auth.token}` })
.request(queries.INSERT_EXPORT_LOG, {
log: {
logs: [{
bodyshopid: socket.JobData.bodyshop.id,
jobid: socket.JobData.id,
successful: false,
message: JSON.stringify(error),
useremail: socket.user.email
}
}]
});
return result;

View File

@@ -44,7 +44,7 @@ async function QueryJobData(connectionData, token, jobid, isFortellis) {
loggingFunction(connectionData, "DEBUG", `Querying job data for id ${jobid}`);
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
const result = await client.setHeaders({ Authorization: token }).request(queries.GET_CDK_ALLOCATIONS, { id: jobid });
loggingFunction(connectionData, "DEBUG", `Job data query result ${JSON.stringify(result, null, 2)}`);
//loggingFunction(connectionData, "DEBUG", `Job data query result ${JSON.stringify(result, null, 2)}`);
return result.jobs_by_pk;
}

View File

@@ -159,7 +159,7 @@ async function QueryJobData(socket, jobid) {
.setHeaders({ Authorization: `Bearer ${currentToken}` })
.request(queries.QUERY_JOBS_FOR_CDK_EXPORT, { id: jobid });
WsLogger.createLogEvent(socket, "SILLY", `Job data query result ${JSON.stringify(result, null, 2)}`);
//WsLogger.createLogEvent(socket, "SILLY", `Job data query result ${JSON.stringify(result, null, 2)}`);
return result.jobs_by_pk;
}
@@ -993,13 +993,13 @@ async function InsertFailedExportLog(socket, error) {
const result = await client
.setHeaders({ Authorization: `Bearer ${currentToken}` })
.request(queries.INSERT_EXPORT_LOG, {
log: {
logs: [{
bodyshopid: socket.JobData.bodyshop.id,
jobid: socket.JobData.id,
successful: false,
message: JSON.stringify(error),
useremail: socket.user.email
}
}]
});
return result;

View File

@@ -27,9 +27,6 @@ class FortellisApiError extends Error {
}
axiosCurlirize(axios, (result, err) => {
const { command } = result;
console.log("*** ~ axiosCurlirize ~ command:", command);
// if (err) {
// use your logger here
// } else {
@@ -88,7 +85,8 @@ async function FetchSubscriptions({ redisHelpers, socket, jobid, SubscriptionObj
} else {
const access_token = await GetAuthToken();
const subscriptions = await axios.get(`https://subscriptions.fortellis.io/v1/solution/subscriptions`, {
headers: { Authorization: `Bearer ${access_token}` }
headers: { Authorization: `Bearer ${access_token}` },
logRequest: false
});
const SubscriptionMeta = subscriptions.data.subscriptions.find((s) => s.subscriptionId === SubscriptionID);
if (setSessionTransactionData) {
@@ -137,7 +135,7 @@ async function MakeFortellisCall({
headers = {},
body = {},
type = "post",
debug = true,
debug = false,
requestPathParams,
requestSearchParams = [], //Array of key/value strings like [["key", "value"]]
jobid,
@@ -209,7 +207,7 @@ async function MakeFortellisCall({
if (debug) {
console.log(`ReqID: ${ReqId} Data`);
console.log(JSON.stringify(result.data, null, 4));
//console.log(JSON.stringify(result.data, null, 4));
}
if (result.data.checkStatusAfterSeconds) {
@@ -221,6 +219,21 @@ async function MakeFortellisCall({
departmentIds: DepartmentId
});
}
logger.log(
"fortellis-log-event-json",
"DEBUG",
socket?.user?.email,
jobid,
{
requestcurl: result.config.curlCommand,
reqid: result.config.headers["Request-Id"] || null,
subscriptionId: result.config.headers["Subscription-Id"] || null,
resultdata: result.data,
resultStatus: result.status
},
);
return result.data;
} catch (error) {
console.log(`ReqID: ${ReqId} Error`, error.response?.data);
@@ -236,6 +249,20 @@ async function MakeFortellisCall({
originalError: error
};
logger.log(
"fortellis-log-event-error",
"ERROR",
socket?.user?.email,
socket?.recordid,
{
wsmessage: "",//message,
curl: error.config.curl.curlCommand,
reqid: error.request.headers["Request-Id"] || null,
subscriptionId: error.request.headers["Subscription-Id"] || null,
},
true
);
// CreateFortellisLogEvent(socket, "ERROR", `Error in MakeFortellisCall for ${apiName}: ${error.message}`, {
// ...errorDetails,
// errorStack: error.stack
@@ -376,6 +403,13 @@ const FortellisActions = {
type: "post",
apiName: "CDK Drive Post Accounts GL"
},
DeleteTranWip: {
url: isProduction
? "https://api.fortellis.io/cdk/drive/glpost/postWIP"
: "https://api.fortellis.io/cdk-test/drive/glpost/postWIP",
type: "post",
apiName: "CDK Drive Post Accounts GL"
},
QueryErrorWip: {
url: isProduction
? "https://api.fortellis.io/cdk/drive/glpost/errWIP/" //Get requests should have the trailing slash/

View File

@@ -252,80 +252,68 @@ async function FortellisSelectedCustomer({ socket, redisHelpers, selectedCustome
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
// );
if (DMSTransHeader.rtnCode === "0") {
CreateFortellisLogEvent(socket, "DEBUG", `{6} Attempting to post Transaction with ID ${DMSTransHeader.transID}`);
try {
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
);
if (DmsBatchTxnPost.rtnCode === "0") {
//TODO: Validate this is a string and not #
//something
CreateFortellisLogEvent(socket, "DEBUG", `{6} Successfully posted transaction to DMS.`);
await MarkJobExported({ socket, jobid: JobData.id });
CreateFortellisLogEvent(socket, "DEBUG", `{5} Updating Service Vehicle History.`);
const DMSVehHistory = await InsertServiceVehicleHistory({ socket, redisHelpers, JobData });
const DmsBatchTxnPost = await PostDmsBatchWip({ socket, redisHelpers, JobData }); // 2 in 1 call that includes a post and the transactions.
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.DMSVehHistory,
DMSVehHistory,
FortellisCacheEnums.DmsBatchTxnPost,
DmsBatchTxnPost,
defaultFortellisTTL
);
socket.emit("export-success", JobData.id);
} else {
//Get the error code
if (DmsBatchTxnPost.rtnCode === "0") {
//TODO: Validate this is a string and not #
//something
CreateFortellisLogEvent(socket, "DEBUG", `{6} Successfully posted transaction to DMS.`);
await MarkJobExported({ socket, jobid: JobData.id, JobData });
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
);
socket.emit("export-success", JobData.id);
} else {
//There was something wrong. Throw an error to trigger clean up.
throw new Error("Error posting DMS Batch Transaction");
}
} catch (error) {
//Clean up the transaction and insert a faild error code
// //Get the error code
CreateFortellisLogEvent(socket, "DEBUG", `{6.1} Getting errors for Transaction ID ${DMSTransHeader.transID}`);
await QueryDmsErrWip({ socket, redisHelpers, JobData });
const DmsError = await QueryDmsErrWip({ socket, redisHelpers, JobData });
// //Delete the transaction
CreateFortellisLogEvent(socket, "DEBUG", `{{ 6.2 } Deleting Transaction ID ${DMSTransHeader.transID}`);
CreateFortellisLogEvent(socket, "DEBUG", `{6.2} Deleting Transaction ID ${DMSTransHeader.transID}`);
const DmsBatchTxnDelete = await DeleteDmsWip({ socket, redisHelpers, JobData });
await DeleteDmsWip({ socket, redisHelpers, JobData });
DmsError.errMsg.map(
DmsError.errLine.map(
(e) =>
e !== null &&
e !== "" &&
CreateFortellisLogEvent(socket, "ERROR", `Error(s) encountered in posting transaction.${e} `)
CreateFortellisLogEvent(socket, "ERROR", `Error encountered in posting transaction => ${e} `)
);
await InsertFailedExportLog({
socket,
JobData,
error: DmsError.errMsg
error: DmsError.errLine
});
}
} else {
//Creating transaction failed.
CreateFortellisLogEvent(
socket,
"ERROR",
`DMS Batch Return code was not successful: ${DMSTransHeader.rtnCode} - ${DMSTransHeader.sendline}`
);
await InsertFailedExportLog({
socket,
JobData,
error: `DMS Batch Return code was not successful: ${DMSTransHeader.rtnCode} - ${DMSTransHeader.sendline}`
});
}
} catch (error) {
CreateFortellisLogEvent(socket, "ERROR", `Error in FortellisSelectedCustomer - ${error} `, {
@@ -1231,7 +1219,6 @@ async function PostDmsBatchWip({ socket, redisHelpers, JobData }) {
transID: DMSTransHeader.transID,
transWipReqList: await GenerateTransWips({ socket, redisHelpers, JobData })
},
overrideDepartmentId: "D100152198"
});
return result;
} catch (error) {
@@ -1260,7 +1247,6 @@ async function QueryDmsErrWip({ socket, redisHelpers, JobData }) {
jobid: JobData.id,
requestPathParams: DMSTransHeader.transID,
body: {},
overrideDepartmentId: "D100152198"
});
return result;
} catch (error) {
@@ -1282,7 +1268,7 @@ async function DeleteDmsWip({ socket, redisHelpers, JobData }) {
);
const result = await MakeFortellisCall({
...FortellisActions.PostBatchWip,
...FortellisActions.DeleteTranWip,
headers: {},
redisHelpers,
socket,
@@ -1291,7 +1277,6 @@ async function DeleteDmsWip({ socket, redisHelpers, JobData }) {
opCode: "D",
transID: DMSTransHeader.transID
},
overrideDepartmentId: "D100152198"
});
return result;
} catch (error) {
@@ -1303,7 +1288,7 @@ async function DeleteDmsWip({ socket, redisHelpers, JobData }) {
}
}
async function MarkJobExported({ socket, jobid }) {
async function MarkJobExported({ socket, jobid, JobData }) {
CreateFortellisLogEvent(socket, "ERROR", `Marking job as exported for id ${jobid}`);
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
@@ -1315,11 +1300,11 @@ async function MarkJobExported({ socket, jobid }) {
.request(queries.MARK_JOB_EXPORTED, {
jobId: jobid,
job: {
status: socket.JobData.bodyshop.md_ro_statuses.default_exported || "Exported*",
status: JobData.bodyshop.md_ro_statuses.default_exported || "Exported*",
date_exported: new Date()
},
log: {
bodyshopid: socket.JobData.bodyshop.id,
bodyshopid: JobData.bodyshop.id,
jobid: jobid,
successful: true,
useremail: socket.user.email,
@@ -1343,13 +1328,13 @@ async function InsertFailedExportLog({ socket, JobData, error }) {
const result = await client
.setHeaders({ Authorization: `Bearer ${currentToken}` })
.request(queries.INSERT_EXPORT_LOG, {
log: {
logs: [{
bodyshopid: JobData.bodyshop.id,
jobid: JobData.id,
successful: false,
message: JSON.stringify(error),
useremail: socket.user.email
}
}]
});
return result;

View File

@@ -2173,19 +2173,7 @@ mutation UPDATE_BILLS($billids: [uuid!]!, $bill: bills_set_input!, $logs: [expor
}
}`;
exports.INSERT_EXPORT_LOG = `
mutation INSERT_EXPORT_LOG($log: exportlog_insert_input!) {
insert_exportlog_one(object: $log) {
id
}
}`;
exports.QUERY_EXISTING_TRANSITION = `
mutation INSERT_EXPORT_LOG($log: exportlog_insert_input!) {
insert_exportlog_one(object: $log) {
id
}
}`;
exports.UPDATE_OLD_TRANSITION = `mutation UPDATE_OLD_TRANSITION($jobid: uuid!, $existingTransition: transitions_set_input!){
update_transitions(where:{jobid:{_eq:$jobid}, end:{_is_null:true