From 5e14258839df49a83bce123917cafd15702d306b Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Tue, 24 Sep 2024 14:20:09 -0400 Subject: [PATCH] IO-2742-redis - PR Changes Signed-off-by: Dave Richer --- server.js | 39 ++++++++++++++++++++++++------------ server/cdk/cdk-job-export.js | 39 ++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/server.js b/server.js index e7a82c507..659a53b6b 100644 --- a/server.js +++ b/server.js @@ -204,7 +204,16 @@ const applyRedisHelpers = (pubClient, app) => { } }; - Object.assign(module.exports, { + // Helper function to clear a list in Redis + const clearList = async (socketId, key) => { + try { + await pubClient.del(`socket:${socketId}:${key}`); + } catch (error) { + console.error(`Error clearing list for socket ${socketId}:`, error); + } + }; + + const api = { setSessionData, getSessionData, clearSessionData, @@ -212,20 +221,14 @@ const applyRedisHelpers = (pubClient, app) => { getMultipleSessionData, setMultipleFromArraySessionData, addItemToEndOfList, - addItemToBeginningOfList - }); + addItemToBeginningOfList, + clearList + }; + + Object.assign(module.exports, api); app.use((req, res, next) => { - req.sessionUtils = { - setSessionData, - getSessionData, - clearSessionData, - setMultipleSessionData, - getMultipleSessionData, - setMultipleFromArraySessionData, - addItemToEndOfList, - addItemToBeginningOfList - }; + req.sessionUtils = api; next(); }); @@ -274,6 +277,16 @@ const applyRedisHelpers = (pubClient, app) => { // const logEvents = await pubClient.lRange(`socket:${socketId}:logEvents`, 0, -1); // console.log("Log Events List:", logEvents.map(JSON.parse)); // + // // **Add the new code below to test clearList** + // + // // Clear the list using clearList + // await exports.clearList(socketId, "logEvents"); + // console.log("Log Events List cleared."); + // + // // Retrieve the list after clearing to confirm it's empty + // const logEventsAfterClear = await pubClient.lRange(`socket:${socketId}:logEvents`, 0, -1); + // console.log("Log Events List after clearing:", logEventsAfterClear); // Should be an empty array + // // // Clear session data // await exports.clearSessionData(socketId); // console.log("Session data cleared."); diff --git a/server/cdk/cdk-job-export.js b/server/cdk/cdk-job-export.js index b9d58cfda..1c62828dc 100644 --- a/server/cdk/cdk-job-export.js +++ b/server/cdk/cdk-job-export.js @@ -12,7 +12,13 @@ const CalcualteAllocations = require("./cdk-calculate-allocations").default; const InstanceMgr = require("../utils/instanceMgr").default; const moment = require("moment-timezone"); -const { setSessionData, getSessionData, getMultipleSessionData, setMultipleSessionData } = require("../../server"); +const { + setSessionData, + getSessionData, + getMultipleSessionData, + setMultipleSessionData, + clearList +} = require("../../server"); const replaceSpecialRegex = /[^a-zA-Z0-9 .,\n #]+/g; @@ -22,6 +28,8 @@ exports.default = async function (socket, { txEnvelope, jobid }) { txEnvelope }); + await clearList(socket.id, "logEvents"); + try { await CdkBase.createLogEvent(socket, "DEBUG", `Received Job export request for id ${jobid}`); @@ -52,19 +60,24 @@ exports.default = async function (socket, { txEnvelope, jobid }) { "DEBUG", `{2.2} Querying the Customer using the ID from DMSVeh: ${DMSVehCustomer.id.value}` ); - const DMSVehCustomerData = await QueryDmsCustomerById(socket, JobData, DMSVehCustomer.id.value); - await setSessionData(socket.id, "DMSVehCustomer", DMSVehCustomerData); + await setSessionData( + socket.id, + "DMSVehCustomer", + await QueryDmsCustomerById(socket, JobData, DMSVehCustomer.id.value) + ); } } await CdkBase.createLogEvent(socket, "DEBUG", `{2.3} Querying the Customer using the name.`); + /// const DMSCustList = await QueryDmsCustomerByName(socket, JobData); await setSessionData(socket.id, "DMSCustList", DMSCustList); - const DMSVehCustomerData = await getSessionData(socket.id, "DMSVehCustomer"); + const DMSVehCustomer = await getSessionData(socket.id, "DMSVehCustomer"); + socket.emit("cdk-select-customer", [ - ...(DMSVehCustomerData ? [{ ...DMSVehCustomerData, vinOwner: true }] : []), + ...(DMSVehCustomer ? [{ ...DMSVehCustomer, vinOwner: true }] : []), ...DMSCustList ]); } catch (error) { @@ -97,16 +110,13 @@ async function CdkSelectedCustomer(socket, selectedCustomerId) { if (DMSVid.newId === "Y") { await CdkBase.createLogEvent(socket, "DEBUG", `{4.1} Inserting new vehicle with ID: ID ${DMSVid.vehiclesVehId}`); - const DMSVeh = await InsertDmsVehicle(socket); - await setSessionData(socket.id, "DMSVeh", DMSVeh); + await setSessionData(socket.id, "DMSVeh", await InsertDmsVehicle(socket)); } else { await CdkBase.createLogEvent(socket, "DEBUG", `{4.2} Querying Existing Vehicle using ID ${DMSVid.vehiclesVehId}`); - const DMSVeh = await QueryDmsVehicleById(socket, await getSessionData(socket.id, "JobData"), DMSVid); - await setSessionData(socket.id, "DMSVeh", DMSVeh); + await setSessionData(socket.id, "DMSVeh", await QueryDmsVehicleById(socket, JobData, DMSVid)); await CdkBase.createLogEvent(socket, "DEBUG", `{4.3} Updating Existing Vehicle to associate to owner.`); - const updatedDMSVeh = await UpdateDmsVehicle(socket); - await setSessionData(socket.id, "DMSVeh", updatedDMSVeh); + await setSessionData(socket.id, "DMSVeh", await UpdateDmsVehicle(socket)); } await CdkBase.createLogEvent(socket, "DEBUG", `{5} Creating Transaction header with Dms Start WIP`); @@ -114,8 +124,7 @@ async function CdkSelectedCustomer(socket, selectedCustomerId) { await setSessionData(socket.id, "DMSTransHeader", DMSTransHeader); await CdkBase.createLogEvent(socket, "DEBUG", `{5.1} Creating Transaction with ID ${DMSTransHeader.transID}`); - const DMSBatchTxn = await InsertDmsBatchWip(socket); - await setSessionData(socket.id, "DMSBatchTxn", DMSBatchTxn); + await setSessionData(socket.id, "DMSBatchTxn", await InsertDmsBatchWip(socket)); await CdkBase.createLogEvent( socket, @@ -123,6 +132,7 @@ async function CdkSelectedCustomer(socket, selectedCustomerId) { `{6} Attempting to post Transaction with ID ${DMSTransHeader.transID}` ); const DmsBatchTxnPost = await PostDmsBatchWip(socket); + await setSessionData(socket.id, "DMSBatchTxnPost", DmsBatchTxnPost); if (DmsBatchTxnPost.code === "success") { await CdkBase.createLogEvent(socket, "DEBUG", `{6} Successfully posted transaction to DMS.`); @@ -132,7 +142,6 @@ async function CdkSelectedCustomer(socket, selectedCustomerId) { const DMSVehHistory = await InsertServiceVehicleHistory(socket); await setSessionData(socket.id, "DMSVehHistory", DMSVehHistory); - // TODO: Clear goes here. socket.emit("export-success", JobData.id); } else { await CdkBase.createLogEvent( @@ -144,7 +153,7 @@ async function CdkSelectedCustomer(socket, selectedCustomerId) { await setSessionData(socket.id, "DmsError", DmsError); await CdkBase.createLogEvent(socket, "DEBUG", `{6.2} Deleting Transaction ID ${DMSTransHeader.transID}`); - await DeleteDmsWip(socket); + await setSessionData(socket.id, "DmsBatchTxnPost", await DeleteDmsWip(socket)); for (const e of DmsError.errMsg.split("|")) { if (e !== null && e !== "") {