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

@@ -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,