IO-2776 Add additional redis helpers, restructure some fortellis calls.

This commit is contained in:
Patrick Fic
2025-03-17 10:49:02 -07:00
parent 88c35e8c48
commit e7c4797fef
6 changed files with 1435 additions and 17 deletions

View File

@@ -27,6 +27,7 @@ import DmsCdkMakesRefetch from "../dms-cdk-makes/dms-cdk-makes.refetch.component
import CurrencyInput from "../form-items-formatted/currency-form-item.component";
import LayoutFormRow from "../layout-form-row/layout-form-row.component";
import DateTimePicker from "../form-date-time-picker/form-date-time-picker.component.jsx";
import { useSocket } from "../../contexts/SocketIO/useSocket.jsx";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop
@@ -39,6 +40,7 @@ export default connect(mapStateToProps, mapDispatchToProps)(DmsPostForm);
export function DmsPostForm({ bodyshop, socket, job, logsRef }) {
const [form] = Form.useForm();
const { t } = useTranslation();
const { socket: wsssocket } = useSocket();
const handlePayerSelect = (value, index) => {
form.setFieldsValue({
@@ -59,22 +61,37 @@ export function DmsPostForm({ bodyshop, socket, job, logsRef }) {
};
const handleFinish = (values) => {
socket.emit(`${determineDmsType(bodyshop)}-export-job`, {
jobid: job.id,
txEnvelope: values
});
console.log(logsRef);
if (logsRef) {
console.log("executing", logsRef);
logsRef.curent &&
logsRef.current.scrollIntoView({
behavior: "smooth"
});
//TODO: Add this as a split instead.
if (true) {
wsssocket.emit("fortellis-export-job", { jobid: job.id, txEnvelope: values });
} else {
socket.emit(`${determineDmsType(bodyshop)}-export-job`, {
jobid: job.id,
txEnvelope: values
});
console.log(logsRef);
if (logsRef) {
console.log("executing", logsRef);
logsRef.curent &&
logsRef.current.scrollIntoView({
behavior: "smooth"
});
}
}
};
return (
<Card title={t("jobs.labels.dms.postingform")}>
<Button
onClick={() =>
wsssocket.emit("fortellis-export-job", {
txEnvelope: { test: 1, test2: 2, SubscriptionID: "5b527d7d-baf3-40bc-adae-e7a541e37363" },
jobid: job.id
})
}
>
Test
</Button>
<Form
form={form}
layout="vertical"
@@ -150,7 +167,7 @@ export function DmsPostForm({ bodyshop, socket, job, logsRef }) {
}
]}
>
<Input disabled />
<Input />
</Form.Item>
<Form.Item
name="dms_model"
@@ -161,7 +178,7 @@ export function DmsPostForm({ bodyshop, socket, job, logsRef }) {
}
]}
>
<Input disabled />
<Input />
</Form.Item>
<Form.Item name="inservicedate" label={t("jobs.fields.dms.inservicedate")}>
<DateTimePicker isDateOnly />

View File

@@ -0,0 +1,253 @@
const path = require("path");
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 getTransactionType = (jobid) => `fortellis:${jobid}`;
const defaultFortellisTTL = 60 * 60;
async function GetAuthToken() {
//Done with Authorization Code Flow
//https://docs.fortellis.io/docs/tutorials/solution-integration/authorization-code-flow/
//TODO: This should get stored in the redis cache and only be refreshed when it expires.
const {
data: { access_token, expires_in, token_type }
} = await axios.post(
process.env.FORTELLIS_AUTH_URL,
{},
{
auth: {
username: process.env.FORTELLIS_KEY,
password: process.env.FORTELLIS_SECRET
},
params: {
grant_type: "client_credentials",
scope: "anonymous"
}
}
);
return access_token;
}
async function FetchSubscriptions({ redisHelpers, socket, jobid }) {
try {
const { setSessionTransactionData, getSessionTransactionData } = redisHelpers;
//Get Subscription ID from Transaction Envelope
const { SubscriptionID } = await getSessionTransactionData(socket.id, getTransactionType(jobid), `txEnvelope`);
if (!SubscriptionID) {
throw new Error("Subscription ID not found in transaction envelope.");
}
//Check to See if the subscription meta is in the Redis Cache.
const SubscriptionMetaFromCache = await getSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.SubscriptionMeta
);
// If it is, return it.
if (SubscriptionMetaFromCache) {
return SubscriptionMetaFromCache;
} else {
const access_token = await GetAuthToken();
const subscriptions = await axios.get(`https://subscriptions.fortellis.io/v1/solution/subscriptions`, {
headers: { Authorization: `Bearer ${access_token}` }
});
const SubscriptionMeta = subscriptions.data.subscriptions.find((s) => s.subscriptionId === SubscriptionID);
await setSessionTransactionData(
socket.id,
getTransactionType(jobid),
FortellisCacheEnums.SubscriptionMeta,
SubscriptionMeta,
defaultFortellisTTL
);
return SubscriptionMeta;
}
} catch (error) {
CreateFortellisLogEvent(socket, "ERROR", `Error fetching subscription metadata`, {
error: error.message,
stack: error.stack
});
}
}
async function GetDepartmentId({ apiName, debug = false, SubscriptionMeta }) {
if (!apiName) throw new Error("apiName not provided. Unable to get department without apiName.");
if (debug) {
console.log("API Names & Departments ");
console.log("===========");
console.log(
JSON.stringify(
SubscriptionMeta.apiDmsInfo.map((a) => ({
name: a.name,
departments: a.departments.map((d) => d.id)
})),
null,
4
)
);
console.log("===========");
}
const departmentIds2 = SubscriptionMeta.apiDmsInfo //Get the subscription object.
.find((info) => info.name === apiName)?.departments; //Departments are categorized by API name and have an array of departments.
return departmentIds2[0].id; //TODO: This makes the assumption that there is only 1 department.
}
//Highest level function call to make a call to fortellis. This should be the only call required, and it will handle all the logic for making the call.
async function MakeFortellisCall({
apiName,
url,
headers = {},
body = {},
type = "post",
debug = true,
jobid,
redisHelpers,
socket
}) {
const { setSessionTransactionData, getSessionTransactionData } = redisHelpers;
if (debug) logger.log(`Executing ${type} to ${url}`);
const ReqId = uuid();
const access_token = await GetAuthToken();
const SubscriptionMeta = await FetchSubscriptions({ redisHelpers, socket, jobid });
const DepartmentId = await GetDepartmentId({ apiName, debug, SubscriptionMeta });
if (debug) {
console.log(
`ReqID: ${ReqId} | SubscriptionID: ${SubscriptionMeta.subscriptionId} | DepartmentId: ${DepartmentId}`
);
console.log(`Body Contents: ${JSON.stringify(body, null, 4)}`);
}
try {
let result;
switch (type) {
case "post":
default:
result = await axios.post(url, body, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionMeta.subscriptionId,
"Request-Id": ReqId,
"Department-Id": DepartmentId,
...headers
}
});
break;
case "get":
result = await axios.get(url, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionMeta.subscriptionId,
"Request-Id": ReqId,
"Department-Id": DepartmentId,
...headers
}
});
break;
}
if (debug) {
console.log(`ReqID: ${ReqId} Data`);
console.log(JSON.stringify(result.data, null, 4));
}
if (result.data.checkStatusAfterSeconds) {
return DelayedCallback({
delayMeta: result.data,
access_token,
SubscriptionID: SubscriptionMeta.subscriptionId,
ReqId,
departmentIds: DepartmentId
});
}
return result.data;
} catch (error) {
console.log(`ReqID: ${ReqId} Error`, error.response?.data);
//console.log(`ReqID: ${ReqId} Full Error`, JSON.stringify(error, null, 4));
}
}
//Some Fortellis calls return a batch result that isn't ready immediately.
//This function will check the status of the call and wait until it is ready.
//It will try 5 times before giving up.
async function DelayedCallback({ delayMeta, access_token, SubscriptionID, ReqId, departmentIds }) {
for (let index = 0; index < 5; index++) {
await sleep(delayMeta.checkStatusAfterSeconds * 1000);
//Check to see if the call is ready.
const statusResult = await axios.get(delayMeta._links.status.href, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId,
"Department-Id": departmentIds[0].id
}
});
//TODO: Add a check if the status result is not ready, to try again.
if (statusResult.data.status === "complete") {
//This may have to check again if it isn't ready.
const batchResult = await axios.get(statusResult.data._links.result.href, {
headers: {
Authorization: `Bearer ${access_token}`,
"Subscription-Id": SubscriptionID,
"Request-Id": ReqId
//"Department-Id": departmentIds[0].id
}
});
return batchResult;
} else {
return "Error!!! Still need to implement batch waiting.";
}
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const isProduction = process.env.NODE_ENV === "production";
const FortellisActions = {
QueryVehicles: {
url: isProduction
? "https://api.fortellis.io/cdkdrive/service/v1/vehicles/"
: "https://api.fortellis.io/cdk-test/cdkdrive/service/v1/vehicles/",
type: "get",
apiName: "Service Vehicle - Query Vehicles"
},
GetCOA: {
type: "get",
apiName: "CDK Drive Post Accounts GL WIP",
url: `https://api.fortellis.io/cdk-test/drive/chartofaccounts/v2/bulk`,
waitForResult: true
}
};
const FortellisCacheEnums = {
txEnvelope: "txEnvelope",
SubscriptionMeta: "SubscriptionMeta",
DepartmentId: "DepartmentId"
};
module.exports = {
GetAuthToken,
FortellisCacheEnums,
MakeFortellisCall,
FortellisActions,
getTransactionType,
defaultFortellisTTL
};

View File

@@ -0,0 +1,9 @@
const logger = require("../utils/logger");
const CreateFortellisLogEvent = (socket, level, message, txnDetails) => {
//TODO: Add detaisl to track the whole transaction between Fortellis and the server.
logger.log("fortellis-log-event", level, socket?.user?.email, null, { wsmessage: message, txnDetails });
socket.emit("fortellis-log-event", { level, message, txnDetails });
};
module.exports = CreateFortellisLogEvent;

File diff suppressed because it is too large Load Diff

View File

@@ -19,6 +19,7 @@ const getBodyshopCacheKey = (bodyshopId) => `bodyshop-cache:${bodyshopId}`;
const getUserSocketMappingKey = (email) =>
`user:${process.env?.NODE_ENV === "production" ? "prod" : "dev"}:${email}:socketMapping`;
const getSocketTransactionkey = ({ socketId, transactionType }) => `socket:${socketId}:${transactionType}`;
/**
* Fetch bodyshop data from the database
* @param bodyshopId
@@ -51,9 +52,12 @@ const fetchBodyshopFromDB = async (bodyshopId, logger) => {
*/
const applyRedisHelpers = ({ pubClient, app, logger }) => {
// Store session data in Redis
const setSessionData = async (socketId, key, value) => {
const setSessionData = async (socketId, key, value, ttl) => {
try {
await pubClient.hset(`socket:${socketId}`, key, JSON.stringify(value)); // Use Redis pubClient
await pubClient.hset(`socket:${socketId}`, key, JSON.stringify(value), ttl); // Use Redis pubClient
if (ttl && typeof ttl === "number") {
await pubClient.expire(`socket:${socketId}`, ttl);
}
} catch (error) {
logger.log(`Error Setting Session Data for socket ${socketId}: ${error}`, "ERROR", "redis");
}
@@ -69,6 +73,35 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
}
};
const setSessionTransactionData = async (socketId, transactionType, key, value, ttl) => {
try {
await pubClient.hset(getSocketTransactionkey({ socketId, transactionType }), key, JSON.stringify(value)); // Use Redis pubClient
if (ttl && typeof ttl === "number") {
await pubClient.expire(getSocketTransactionkey({ socketId, transactionType }), ttl);
}
} catch (error) {
logger.log(
`Error Setting Session Data for socket transaction ${socketId}:${transactionType}: ${error}`,
"ERROR",
"redis"
);
}
};
// Retrieve session transaction data from Redis
const getSessionTransactionData = async (socketId, transactionType, key) => {
try {
const data = await pubClient.hget(getSocketTransactionkey({ socketId, transactionType }), key);
return data ? JSON.parse(data) : null;
} catch (error) {
logger.log(
`Error Getting Session Data for socket transaction ${socketId}:${transactionType}: ${error}`,
"ERROR",
"redis"
);
}
};
// Clear session data from Redis
const clearSessionData = async (socketId) => {
try {
@@ -77,6 +110,18 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
logger.log(`Error Clearing Session Data for socket ${socketId}: ${error}`, "ERROR", "redis");
}
};
// Clear session data from Redis
const clearSessionTransactionData = async (socketId, transactionType) => {
try {
await pubClient.del(getSocketTransactionkey({ socketId, transactionType }));
} catch (error) {
logger.log(
`Error Clearing Session Transaction Data for socket ${socketId}:${transactionType}: ${error}`,
"ERROR",
"redis"
);
}
};
/**
* Add a socket mapping for a user
@@ -394,7 +439,10 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
getUserSocketMapping,
refreshUserSocketTTL,
getBodyshopFromRedis,
updateOrInvalidateBodyshopFromRedis
updateOrInvalidateBodyshopFromRedis,
setSessionTransactionData,
getSessionTransactionData,
clearSessionTransactionData
// setMultipleSessionData,
// getMultipleSessionData,
// setMultipleFromArraySessionData,

View File

@@ -1,8 +1,20 @@
const { admin } = require("../firebase/firebase-handler");
const FortellisJobExport = require("../fortellis/fortellis").default;
const FortellisLogger = require("../fortellis/fortellis-logger");
const redisSocketEvents = ({
io,
redisHelpers: { addUserSocketMapping, removeUserSocketMapping, refreshUserSocketTTL, getUserSocketMappingByBodyshop },
redisHelpers: {
setSessionData,
getSessionData,
addUserSocketMapping,
removeUserSocketMapping,
refreshUserSocketTTL,
getUserSocketMappingByBodyshop,
setSessionTransactionData,
getSessionTransactionData,
clearSessionTransactionData
},
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom },
logger
}) => {
@@ -231,12 +243,44 @@ const redisSocketEvents = ({
});
};
//Fortellis/CDK Handlers
const registerFortellisEvents = (socket) => {
socket.on("fortellis-export-job", async ({ jobid, txEnvelope }) => {
try {
await FortellisJobExport({
socket,
redisHelpers: {
setSessionData,
getSessionData,
addUserSocketMapping,
removeUserSocketMapping,
refreshUserSocketTTL,
getUserSocketMappingByBodyshop,
setSessionTransactionData,
getSessionTransactionData,
clearSessionTransactionData
},
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom },
jobid,
txEnvelope
});
} catch (error) {
FortellisLogger(socket, "error", `Error during Fortellis export : ${error.message}`);
logger.log("fortellis-job-export-error", "error", null, null, {
message: error.message,
stack: error.stack
});
}
});
};
// Call Handlers
registerRoomAndBroadcastEvents(socket);
registerUpdateEvents(socket);
registerMessagingEvents(socket);
registerDisconnectEvents(socket);
registerSyncEvents(socket);
registerFortellisEvents(socket);
};
// Associate Middleware and Handlers