feature/IO-2742-redis - Checkpoint, Redis fully implemented.
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -3,9 +3,8 @@ require("dotenv").config({
|
||||
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
|
||||
});
|
||||
|
||||
const { io } = require("../../server");
|
||||
const { io, setSessionData, clearSessionData, getSessionData } = require("../../server");
|
||||
const { admin } = require("../firebase/firebase-handler");
|
||||
const { isArray } = require("lodash");
|
||||
const logger = require("../utils/logger");
|
||||
const { default: CdkJobExport, CdkSelectedCustomer } = require("../cdk/cdk-job-export");
|
||||
const CdkGetMakes = require("../cdk/cdk-get-makes").default;
|
||||
@@ -32,9 +31,9 @@ function socketAuthMiddleware(socket, next) {
|
||||
}
|
||||
|
||||
// Register all socket events for a given socket connection
|
||||
function registerSocketEvents(socket) {
|
||||
socket.log_level = "TRACE";
|
||||
createLogEvent(socket, "DEBUG", `Connected and Authenticated.`);
|
||||
async function registerSocketEvents(socket) {
|
||||
await setSessionData(socket.id, "log_level", "TRACE");
|
||||
await createLogEvent(socket, "DEBUG", `Connected and Authenticated.`);
|
||||
|
||||
// Register CDK-related socket events
|
||||
registerCdkEvents(socket);
|
||||
@@ -48,18 +47,24 @@ function registerSocketEvents(socket) {
|
||||
// Register room and broadcasting events
|
||||
registerRoomAndBroadcastEvents(socket);
|
||||
|
||||
// Register event to clear DMS session
|
||||
registerDmsClearSessionEvent(socket);
|
||||
|
||||
// Handle socket disconnection
|
||||
socket.on("disconnect", () => {
|
||||
createLogEvent(socket, "DEBUG", `User disconnected.`);
|
||||
socket.on("disconnect", async () => {
|
||||
await createLogEvent(socket, "DEBUG", `User disconnected.`);
|
||||
});
|
||||
}
|
||||
|
||||
// CDK-specific socket events
|
||||
function registerCdkEvents(socket) {
|
||||
socket.on("cdk-export-job", (jobid) => CdkJobExport(socket, jobid));
|
||||
socket.on("cdk-selected-customer", (selectedCustomerId) => {
|
||||
createLogEvent(socket, "DEBUG", `User selected customer ID ${selectedCustomerId}`);
|
||||
CdkSelectedCustomer(socket, selectedCustomerId);
|
||||
socket.on("cdk-selected-customer", async (selectedCustomerId) => {
|
||||
await createLogEvent(socket, "DEBUG", `User selected customer ID ${selectedCustomerId}`);
|
||||
CdkSelectedCustomer(socket, selectedCustomerId).catch((err) =>
|
||||
console.error(`Error in cdk-selected-customer: ${err}`)
|
||||
);
|
||||
await setSessionData(socket.id, "selectedCustomer", selectedCustomerId);
|
||||
});
|
||||
|
||||
socket.on("cdk-get-makes", async (cdk_dealerid, callback) => {
|
||||
@@ -67,15 +72,16 @@ function registerCdkEvents(socket) {
|
||||
const makes = await CdkGetMakes(socket, cdk_dealerid);
|
||||
callback(makes);
|
||||
} catch (error) {
|
||||
createLogEvent(socket, "ERROR", `Error in cdk-get-makes WS call. ${JSON.stringify(error)}`);
|
||||
await createLogEvent(socket, "ERROR", `Error in cdk-get-makes WS call. ${JSON.stringify(error)}`);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("cdk-calculate-allocations", async (jobid, callback) => {
|
||||
const allocations = await CdkCalculateAllocations(socket, jobid);
|
||||
createLogEvent(socket, "DEBUG", `Allocations calculated.`);
|
||||
createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
|
||||
await createLogEvent(socket, "DEBUG", `Allocations calculated.`);
|
||||
await createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
|
||||
callback(allocations);
|
||||
await setSessionData(socket.id, "cdk_allocations", allocations);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -83,15 +89,19 @@ function registerCdkEvents(socket) {
|
||||
function registerPbsArEvents(socket) {
|
||||
socket.on("pbs-calculate-allocations", async (jobid, callback) => {
|
||||
const allocations = await CdkCalculateAllocations(socket, jobid);
|
||||
createLogEvent(socket, "DEBUG", `PBS AR allocations calculated.`);
|
||||
createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
|
||||
await createLogEvent(socket, "DEBUG", `PBS AR allocations calculated.`);
|
||||
await createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
|
||||
callback(allocations);
|
||||
await setSessionData(socket.id, "pbs_allocations", allocations);
|
||||
});
|
||||
|
||||
socket.on("pbs-export-job", (jobid) => PbsExportJob(socket, jobid));
|
||||
socket.on("pbs-selected-customer", (selectedCustomerId) => {
|
||||
createLogEvent(socket, "DEBUG", `PBS AR selected customer ID ${selectedCustomerId}`);
|
||||
PbsSelectedCustomer(socket, selectedCustomerId);
|
||||
socket.on("pbs-selected-customer", async (selectedCustomerId) => {
|
||||
await createLogEvent(socket, "DEBUG", `PBS AR selected customer ID ${selectedCustomerId}`);
|
||||
PbsSelectedCustomer(socket, selectedCustomerId).catch((err) =>
|
||||
console.error(`Error in pbs-selected-customer: ${err}`)
|
||||
);
|
||||
await setSessionData(socket.id, "selectedCustomer", selectedCustomerId);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -99,62 +109,92 @@ function registerPbsArEvents(socket) {
|
||||
function registerPbsApEvents(socket) {
|
||||
socket.on("pbs-calculate-allocations-ap", async (billids, callback) => {
|
||||
const allocations = await PbsCalculateAllocationsAp(socket, billids);
|
||||
createLogEvent(socket, "DEBUG", `PBS AP allocations calculated.`);
|
||||
createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
|
||||
socket.apAllocations = allocations;
|
||||
await createLogEvent(socket, "DEBUG", `PBS AP allocations calculated.`);
|
||||
await createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
|
||||
callback(allocations);
|
||||
await setSessionData(socket.id, "pbs_ap_allocations", allocations);
|
||||
});
|
||||
|
||||
socket.on("pbs-export-ap", ({ billids, txEnvelope }) => {
|
||||
socket.txEnvelope = txEnvelope;
|
||||
PbsExportAp(socket, { billids, txEnvelope });
|
||||
socket.on("pbs-export-ap", async ({ billids, txEnvelope }) => {
|
||||
await setSessionData(socket.id, "pbs_txEnvelope", txEnvelope);
|
||||
PbsExportAp(socket, { billids, txEnvelope }).catch((err) => console.error(`Error in pbs-export-ap: ${err}`));
|
||||
});
|
||||
}
|
||||
|
||||
// Room management and broadcasting events
|
||||
function registerRoomAndBroadcastEvents(socket) {
|
||||
socket.on("join-bodyshop-room", (bodyshopUUID) => {
|
||||
socket.on("join-bodyshop-room", async (bodyshopUUID) => {
|
||||
socket.join(bodyshopUUID);
|
||||
createLogEvent(socket, "DEBUG", `Client joined bodyshop room: ${bodyshopUUID}`);
|
||||
await createLogEvent(socket, "DEBUG", `Client joined bodyshop room: ${bodyshopUUID}`);
|
||||
});
|
||||
|
||||
socket.on("leave-bodyshop-room", (bodyshopUUID) => {
|
||||
socket.on("leave-bodyshop-room", async (bodyshopUUID) => {
|
||||
socket.leave(bodyshopUUID);
|
||||
createLogEvent(socket, "DEBUG", `Client left bodyshop room: ${bodyshopUUID}`);
|
||||
await createLogEvent(socket, "DEBUG", `Client left bodyshop room: ${bodyshopUUID}`);
|
||||
});
|
||||
|
||||
socket.on("broadcast-to-bodyshop", (bodyshopUUID, message) => {
|
||||
socket.on("broadcast-to-bodyshop", async (bodyshopUUID, message) => {
|
||||
io.to(bodyshopUUID).emit("bodyshop-message", message);
|
||||
createLogEvent(socket, "INFO", `Broadcasted message to bodyshop ${bodyshopUUID}`);
|
||||
await createLogEvent(socket, "INFO", `Broadcast message to bodyshop ${bodyshopUUID}`);
|
||||
});
|
||||
}
|
||||
|
||||
// DMS session clearing event
|
||||
function registerDmsClearSessionEvent(socket) {
|
||||
socket.on("clear-dms-session", async () => {
|
||||
await clearSessionData(socket.id); // Clear all session data in Redis
|
||||
await createLogEvent(socket, "INFO", `DMS session data cleared for socket ${socket.id}`);
|
||||
});
|
||||
}
|
||||
|
||||
// Logging helper functions
|
||||
function createLogEvent(socket, level, message) {
|
||||
if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy(level)) {
|
||||
async function createLogEvent(socket, level, message) {
|
||||
const logLevel = await getSessionData(socket.id, "log_level");
|
||||
const recordid = await getSessionData(socket.id, "recordid");
|
||||
const logEvents = await getSessionData(socket.id, "logEvents");
|
||||
|
||||
if (LogLevelHierarchy(logLevel) >= LogLevelHierarchy(level)) {
|
||||
console.log(`[WS LOG EVENT] ${level} - ${new Date()} - ${socket.user.email} - ${socket.id} - ${message}`);
|
||||
socket.emit("log-event", { timestamp: new Date(), level, message });
|
||||
logger.log("ws-log-event", level, socket.user.email, socket.recordid, { wsmessage: message });
|
||||
logger.log("ws-log-event", level, socket.user.email, recordid, { wsmessage: message });
|
||||
|
||||
if (socket.logEvents && isArray(socket.logEvents)) {
|
||||
socket.logEvents.push({ timestamp: new Date(), level, message });
|
||||
if (logEvents && Array.isArray(logEvents)) {
|
||||
logEvents.push({ timestamp: new Date(), level, message });
|
||||
setSessionData(socket.id, "logEvents", logEvents).catch((err) =>
|
||||
console.error(`Error setting logEvents in Redis: ${err}`)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createJsonEvent(socket, level, message, json) {
|
||||
if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy(level)) {
|
||||
async function createJsonEvent(socket, level, message, json) {
|
||||
const logEvents = await getSessionData(socket.id, "logEvents");
|
||||
const logLevel = await getSessionData(socket.id, "log_level");
|
||||
const recordid = await getSessionData(socket.id, "recordid");
|
||||
|
||||
if (LogLevelHierarchy(logLevel) >= LogLevelHierarchy(level)) {
|
||||
socket.emit("log-event", { timestamp: new Date(), level, message });
|
||||
}
|
||||
logger.log("ws-log-event-json", level, socket.user.email, socket.recordid, { wsmessage: message, json });
|
||||
|
||||
if (socket.logEvents && isArray(socket.logEvents)) {
|
||||
socket.logEvents.push({ timestamp: new Date(), level, message });
|
||||
logger.log("ws-log-event-json", level, socket.user.email, recordid, {
|
||||
wsmessage: message,
|
||||
json
|
||||
});
|
||||
|
||||
if (logEvents && Array.isArray(logEvents)) {
|
||||
logEvents.push({ timestamp: new Date(), level, message });
|
||||
setSessionData(socket.id, "logEvents", logEvents).catch((err) =>
|
||||
console.error(`Error setting logEvents in Redis: ${err}`)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function createXmlEvent(socket, xml, message, isError = false) {
|
||||
if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy("TRACE")) {
|
||||
async function createXmlEvent(socket, xml, message, isError = false) {
|
||||
const logLevel = await getSessionData(socket.id, "log_level");
|
||||
const recordid = await getSessionData(socket.id, "recordid");
|
||||
const logEvents = await getSessionData(socket.id, "logEvents");
|
||||
|
||||
if (LogLevelHierarchy(logLevel) >= LogLevelHierarchy("TRACE")) {
|
||||
socket.emit("log-event", {
|
||||
timestamp: new Date(),
|
||||
level: isError ? "ERROR" : "TRACE",
|
||||
@@ -166,12 +206,19 @@ function createXmlEvent(socket, xml, message, isError = false) {
|
||||
isError ? "ws-log-event-xml-error" : "ws-log-event-xml",
|
||||
isError ? "ERROR" : "TRACE",
|
||||
socket.user.email,
|
||||
socket.recordid,
|
||||
recordid,
|
||||
{ wsmessage: message, xml }
|
||||
);
|
||||
|
||||
if (socket.logEvents && isArray(socket.logEvents)) {
|
||||
socket.logEvents.push({ timestamp: new Date(), level: isError ? "ERROR" : "TRACE", message, xml });
|
||||
if (logEvents && Array.isArray(logEvents)) {
|
||||
logEvents.push({
|
||||
timestamp: new Date(),
|
||||
level: isError ? "ERROR" : "TRACE",
|
||||
message,
|
||||
xml
|
||||
});
|
||||
|
||||
await setSessionData(socket.id, "logEvents", logEvents);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user