- Checkpoint

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-09-06 13:22:17 -04:00
parent 44db8f20e9
commit 03e8b62e4f
2 changed files with 210 additions and 193 deletions

193
server.js
View File

@@ -1,66 +1,58 @@
const main = async () => { const express = require("express");
// Import core modules const cors = require("cors");
const express = require("express"); const bodyParser = require("body-parser");
const cors = require("cors"); const path = require("path");
const bodyParser = require("body-parser"); const compression = require("compression");
const path = require("path"); const cookieParser = require("cookie-parser");
const compression = require("compression"); const http = require("http");
const cookieParser = require("cookie-parser"); const { Server } = require("socket.io");
const http = require("http"); const { createClient } = require("redis");
const { Server } = require("socket.io"); const { createAdapter } = require("@socket.io/redis-adapter");
const { createClient } = require("redis"); const logger = require("./server/utils/logger");
const { createAdapter } = require("@socket.io/redis-adapter");
// Load environment variables // This file offers the following exports
require("dotenv").config({ // redisClient: Redis client for external use
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`) // io: Socket.IO server for external use
});
// Import custom utilities and handlers // Load environment variables
const logger = require("./server/utils/logger"); require("dotenv").config({
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
});
// Express app and server setup /**
const app = express(); * CORS Origin for Socket.IO
const port = process.env.PORT || 5000; * @type {string[][]}
const server = http.createServer(app); */
const SOCKETIO_CORS_ORIGIN = [
[
"https://test.imex.online",
"https://www.test.imex.online",
"http://localhost:3000",
"https://imex.online",
"https://www.imex.online",
"https://romeonline.io",
"https://www.romeonline.io",
"https://beta.test.romeonline.io",
"https://www.beta.test.romeonline.io",
"https://beta.romeonline.io",
"https://www.beta.romeonline.io",
"https://beta.test.imex.online",
"https://www.beta.test.imex.online",
"https://beta.imex.online",
"https://www.beta.imex.online",
"https://www.test.promanager.web-est.com",
"https://test.promanager.web-est.com",
"https://www.promanager.web-est.com",
"https://www.promanager.web-est.com",
"http://localhost:3000"
]
];
// Redis client setup for Pub/Sub and Key-Value Store /**
const pubClient = createClient({ url: process.env.REDIS_URL || "redis://localhost:6379" }); * Middleware for Express app
const subClient = pubClient.duplicate(); * @param app
*/
// Clean up on exit const applyMiddleware = (app) => {
process.on("SIGINT", async () => {
await Promise.all([pubClient.disconnect(), subClient.disconnect()]);
process.exit(0);
});
// Connect Redis clients
await Promise.all([pubClient.connect(), subClient.connect()]);
// Redis Pub/Sub adapter for Socket.IO
const io = new Server(server, {
path: "/ws",
adapter: createAdapter(pubClient, subClient),
cors: {
origin: [
"https://test.imex.online",
"https://www.test.imex.online",
"http://localhost"
// Other allowed origins
],
methods: ["GET", "POST"],
credentials: true,
exposedHeaders: ["set-cookie"]
}
});
// Export io and Redis client for external use
exports.io = io;
exports.redisClient = pubClient;
require("./server/web-sockets/web-socket");
// Middleware
app.use(compression()); app.use(compression());
app.use(cookieParser()); app.use(cookieParser());
app.use(bodyParser.json({ limit: "50mb" })); app.use(bodyParser.json({ limit: "50mb" }));
@@ -72,15 +64,93 @@ const main = async () => {
req.logger = logger; req.logger = logger;
next(); next();
}); });
};
// Route groupings /**
* Route groupings for Express app
* @param app
*/
const applyRoutes = (app) => {
app.use("/", require("./server/routes/miscellaneousRoutes")); app.use("/", require("./server/routes/miscellaneousRoutes"));
// Other routes... app.use("/notifications", require("./server/routes/notificationsRoutes"));
app.use("/render", require("./server/routes/renderRoutes"));
app.use("/mixdata", require("./server/routes/mixDataRoutes"));
app.use("/accounting", require("./server/routes/accountingRoutes"));
app.use("/qbo", require("./server/routes/qboRoutes"));
app.use("/media", require("./server/routes/mediaRoutes"));
app.use("/sms", require("./server/routes/smsRoutes"));
app.use("/job", require("./server/routes/jobRoutes"));
app.use("/scheduling", require("./server/routes/schedulingRoutes"));
app.use("/utils", require("./server/routes/utilRoutes"));
app.use("/data", require("./server/routes/dataRoutes"));
app.use("/adm", require("./server/routes/adminRoutes"));
app.use("/tech", require("./server/routes/techRoutes"));
app.use("/intellipay", require("./server/routes/intellipayRoutes"));
app.use("/cdk", require("./server/routes/cdkRoutes"));
app.use("/csi", require("./server/routes/csiRoutes"));
app.use("/payroll", require("./server/routes/payrollRoutes"));
// Default route for forbidden access // Default route for forbidden access
app.get("/", (req, res) => { app.get("/", (req, res) => {
res.status(200).send("Access Forbidden."); res.status(200).send("Access Forbidden.");
}); });
};
/**
* Main function to start the server
* @returns {Promise<void>}
*/
const main = async () => {
// Express app and server setup
const app = express();
const port = process.env.PORT || 5000;
const server = http.createServer(app);
// Redis client setup for Pub/Sub and Key-Value Store
const pubClient = createClient({ url: process.env.REDIS_URL || "redis://localhost:6379" });
const subClient = pubClient.duplicate();
// Add Error listeners to Redis clients
pubClient.on("error", (err) => logger.log(`Redis pubClient error: ${err}`, "ERROR", "redis"));
subClient.on("error", (err) => logger.log(`Redis subClient error: ${err}`, "ERROR", "redis"));
// Connect Redis clients
try {
await Promise.all([pubClient.connect(), subClient.connect()]);
} catch (redisError) {
logger.log(`Failed to connect to Redis`, "ERROR", "redis", redisError);
}
// Clean up on exit
process.on("SIGINT", async () => {
await Promise.all([pubClient.disconnect(), subClient.disconnect()]);
process.exit(0);
});
// Redis Pub/Sub adapter for Socket.IO
// Export io and Redis client for external use
exports.io = new Server(server, {
path: "/ws",
adapter: createAdapter(pubClient, subClient),
cors: {
origin: SOCKETIO_CORS_ORIGIN,
methods: ["GET", "POST"],
credentials: true,
exposedHeaders: ["set-cookie"]
}
});
// Export Redis client for external use
exports.redisClient = pubClient;
// Include the Socket IO references
require("./server/web-sockets/web-socket");
// Middleware
applyMiddleware(app);
// Route groupings
applyRoutes(app);
try { try {
await server.listen(port); await server.listen(port);
@@ -96,4 +166,5 @@ const main = async () => {
}; };
// Start server // Start server
// noinspection JSIgnoredPromiseFromCall
main(); main();

View File

@@ -5,61 +5,60 @@ require("dotenv").config({
const { io } = require("../../server"); const { io } = require("../../server");
const { admin } = require("../firebase/firebase-handler"); 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 { default: CdkJobExport, CdkSelectedCustomer } = require("../cdk/cdk-job-export");
const CdkGetMakes = require("../cdk/cdk-get-makes").default; const CdkGetMakes = require("../cdk/cdk-get-makes").default;
const CdkCalculateAllocations = require("../cdk/cdk-calculate-allocations").default; const CdkCalculateAllocations = require("../cdk/cdk-calculate-allocations").default;
const { isArray } = require("lodash");
const logger = require("../utils/logger");
const { default: PbsExportJob, PbsSelectedCustomer } = require("../accounting/pbs/pbs-job-export"); const { default: PbsExportJob, PbsSelectedCustomer } = require("../accounting/pbs/pbs-job-export");
const { PbsCalculateAllocationsAp, PbsExportAp } = require("../accounting/pbs/pbs-ap-allocations"); const { PbsCalculateAllocationsAp, PbsExportAp } = require("../accounting/pbs/pbs-ap-allocations");
io.use(function (socket, next) { // Middleware for verifying tokens via Firebase authentication
try { function socketAuthMiddleware(socket, next) {
if (socket.handshake.auth.token) { const token = socket.handshake.auth.token;
admin
.auth()
.verifyIdToken(socket.handshake.auth.token)
.then((user) => {
socket.user = user;
next();
})
.catch((error) => {
next(new Error("Authentication error", JSON.stringify(error)));
});
} else {
next(new Error("Authentication error - no authorization token."));
}
} catch (error) {
console.log("Uncaught connection error:::", error);
logger.log("websocket-connection-error", "error", null, null, {
token: socket.handshake.auth.token,
...error
});
next(new Error(`Authentication error ${error}`));
}
});
io.on("connection", (socket) => { if (!token) return next(new Error("Authentication error - no authorization token."));
admin
.auth()
.verifyIdToken(token)
.then((user) => {
socket.user = user;
next();
})
.catch((error) => {
next(new Error("Authentication error", JSON.stringify(error)));
});
}
// Register all socket events for a given socket connection
function registerSocketEvents(socket) {
socket.log_level = "TRACE"; socket.log_level = "TRACE";
createLogEvent(socket, "DEBUG", `Connected and Authenticated.`); createLogEvent(socket, "DEBUG", `Connected and Authenticated.`);
socket.on("set-log-level", (level) => { // Register CDK-related socket events
socket.log_level = level; registerCdkEvents(socket);
socket.emit("log-event", {
timestamp: new Date(),
level: "INFO",
message: `Updated log level to ${level}`
});
});
///CDK // Register PBS AR-related socket events
socket.on("cdk-export-job", (jobid) => { registerPbsArEvents(socket);
CdkJobExport(socket, jobid);
// Register PBS AP-related socket events
registerPbsApEvents(socket);
// Register room and broadcasting events
registerRoomAndBroadcastEvents(socket);
// Handle socket disconnection
socket.on("disconnect", () => {
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) => { socket.on("cdk-selected-customer", (selectedCustomerId) => {
createLogEvent(socket, "DEBUG", `User selected customer ID ${selectedCustomerId}`); createLogEvent(socket, "DEBUG", `User selected customer ID ${selectedCustomerId}`);
socket.selectedCustomerId = selectedCustomerId;
CdkSelectedCustomer(socket, selectedCustomerId); CdkSelectedCustomer(socket, selectedCustomerId);
}); });
@@ -68,44 +67,40 @@ io.on("connection", (socket) => {
const makes = await CdkGetMakes(socket, cdk_dealerid); const makes = await CdkGetMakes(socket, cdk_dealerid);
callback(makes); callback(makes);
} catch (error) { } catch (error) {
createLogEvent(socket, "ERROR", `Error in cdk-get-makes WS call. ${JSON.stringify(error, null, 2)}`); createLogEvent(socket, "ERROR", `Error in cdk-get-makes WS call. ${JSON.stringify(error)}`);
} }
}); });
socket.on("cdk-calculate-allocations", async (jobid, callback) => { socket.on("cdk-calculate-allocations", async (jobid, callback) => {
const allocations = await CdkCalculateAllocations(socket, jobid); const allocations = await CdkCalculateAllocations(socket, jobid);
createLogEvent(socket, "DEBUG", `Allocations calculated.`); createLogEvent(socket, "DEBUG", `Allocations calculated.`);
createLogEvent(socket, "TRACE", `Allocations calculated. ${JSON.stringify(allocations, null, 2)}`); createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
callback(allocations); callback(allocations);
}); });
//END CDK }
//PBS AR // PBS AR-specific socket events
function registerPbsArEvents(socket) {
socket.on("pbs-calculate-allocations", async (jobid, callback) => { socket.on("pbs-calculate-allocations", async (jobid, callback) => {
const allocations = await CdkCalculateAllocations(socket, jobid); const allocations = await CdkCalculateAllocations(socket, jobid);
createLogEvent(socket, "DEBUG", `Allocations calculated.`); createLogEvent(socket, "DEBUG", `PBS AR allocations calculated.`);
createLogEvent(socket, "TRACE", `Allocations calculated. ${JSON.stringify(allocations, null, 2)}`); createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
callback(allocations); callback(allocations);
}); });
socket.on("pbs-export-job", (jobid) => { socket.on("pbs-export-job", (jobid) => PbsExportJob(socket, jobid));
PbsExportJob(socket, jobid);
});
socket.on("pbs-selected-customer", (selectedCustomerId) => { socket.on("pbs-selected-customer", (selectedCustomerId) => {
createLogEvent(socket, "DEBUG", `User selected customer ID ${selectedCustomerId}`); createLogEvent(socket, "DEBUG", `PBS AR selected customer ID ${selectedCustomerId}`);
socket.selectedCustomerId = selectedCustomerId;
PbsSelectedCustomer(socket, selectedCustomerId); PbsSelectedCustomer(socket, selectedCustomerId);
}); });
//End PBS AR }
//PBS AP // PBS AP-specific socket events
function registerPbsApEvents(socket) {
socket.on("pbs-calculate-allocations-ap", async (billids, callback) => { socket.on("pbs-calculate-allocations-ap", async (billids, callback) => {
const allocations = await PbsCalculateAllocationsAp(socket, billids); const allocations = await PbsCalculateAllocationsAp(socket, billids);
createLogEvent(socket, "DEBUG", `AP Allocations calculated.`); createLogEvent(socket, "DEBUG", `PBS AP allocations calculated.`);
createLogEvent(socket, "TRACE", `Allocations calculated. ${JSON.stringify(allocations, null, 2)}`); createLogEvent(socket, "TRACE", `Allocations details: ${JSON.stringify(allocations)}`);
socket.apAllocations = allocations; socket.apAllocations = allocations;
callback(allocations); callback(allocations);
}); });
@@ -114,81 +109,48 @@ io.on("connection", (socket) => {
socket.txEnvelope = txEnvelope; socket.txEnvelope = txEnvelope;
PbsExportAp(socket, { billids, txEnvelope }); PbsExportAp(socket, { billids, txEnvelope });
}); });
}
//END PBS AP // Room management and broadcasting events
function registerRoomAndBroadcastEvents(socket) {
socket.on("disconnect", () => {
createLogEvent(socket, "DEBUG", `User disconnected.`);
});
socket.on("join-bodyshop-room", (bodyshopUUID) => { socket.on("join-bodyshop-room", (bodyshopUUID) => {
socket.join(bodyshopUUID); // Join the room identified by the bodyshop UUID socket.join(bodyshopUUID);
createLogEvent(socket, "DEBUG", `Client joined bodyshop room: ${bodyshopUUID}`); createLogEvent(socket, "DEBUG", `Client joined bodyshop room: ${bodyshopUUID}`);
}); });
// Optionally handle leaving the room
socket.on("leave-bodyshop-room", (bodyshopUUID) => { socket.on("leave-bodyshop-room", (bodyshopUUID) => {
socket.leave(bodyshopUUID); // Leave the room identified by the bodyshop UUID socket.leave(bodyshopUUID);
createLogEvent(socket, "DEBUG", `Client left bodyshop room: ${bodyshopUUID}`); createLogEvent(socket, "DEBUG", `Client left bodyshop room: ${bodyshopUUID}`);
}); });
// Broadcast to specific bodyshop rooms
socket.on("broadcast-to-bodyshop", (bodyshopUUID, message) => { socket.on("broadcast-to-bodyshop", (bodyshopUUID, message) => {
io.to(bodyshopUUID).emit("bodyshop-message", message); io.to(bodyshopUUID).emit("bodyshop-message", message);
createLogEvent(socket, "INFO", `Broadcasted message to bodyshop ${bodyshopUUID}`); createLogEvent(socket, "INFO", `Broadcasted message to bodyshop ${bodyshopUUID}`);
}); });
}); }
// Logging helper functions
function createLogEvent(socket, level, message) { function createLogEvent(socket, level, message) {
if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy(level)) { if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy(level)) {
console.log(`[WS LOG EVENT] ${level} - ${new Date()} - ${socket.user.email} - ${socket.id} - ${message}`); console.log(`[WS LOG EVENT] ${level} - ${new Date()} - ${socket.user.email} - ${socket.id} - ${message}`);
socket.emit("log-event", { socket.emit("log-event", { timestamp: new Date(), level, message });
timestamp: new Date(), logger.log("ws-log-event", level, socket.user.email, socket.recordid, { wsmessage: message });
level,
message
});
logger.log("ws-log-event", level, socket.user.email, socket.recordid, {
wsmessage: message
});
if (socket.logEvents && isArray(socket.logEvents)) { if (socket.logEvents && isArray(socket.logEvents)) {
socket.logEvents.push({ socket.logEvents.push({ timestamp: new Date(), level, message });
timestamp: new Date(),
level,
message
});
} }
// if (level === "ERROR") {
// throw new Error(message);
// }
} }
} }
function createJsonEvent(socket, level, message, json) { function createJsonEvent(socket, level, message, json) {
if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy(level)) { if (LogLevelHierarchy(socket.log_level) >= 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 });
socket.emit("log-event", {
timestamp: new Date(),
level,
message
});
} }
logger.log("ws-log-event-json", level, socket.user.email, socket.recordid, { logger.log("ws-log-event-json", level, socket.user.email, socket.recordid, { wsmessage: message, json });
wsmessage: message,
json
});
if (socket.logEvents && isArray(socket.logEvents)) { if (socket.logEvents && isArray(socket.logEvents)) {
socket.logEvents.push({ socket.logEvents.push({ timestamp: new Date(), level, message });
timestamp: new Date(),
level,
message
});
} }
// if (level === "ERROR") {
// throw new Error(message);
// }
} }
function createXmlEvent(socket, xml, message, isError = false) { function createXmlEvent(socket, xml, message, isError = false) {
@@ -205,41 +167,25 @@ function createXmlEvent(socket, xml, message, isError = false) {
isError ? "ERROR" : "TRACE", isError ? "ERROR" : "TRACE",
socket.user.email, socket.user.email,
socket.recordid, socket.recordid,
{ { wsmessage: message, xml }
wsmessage: message,
xml
}
); );
if (socket.logEvents && isArray(socket.logEvents)) { if (socket.logEvents && isArray(socket.logEvents)) {
socket.logEvents.push({ socket.logEvents.push({ timestamp: new Date(), level: isError ? "ERROR" : "TRACE", message, xml });
timestamp: new Date(),
level: isError ? "ERROR" : "TRACE",
message,
xml
});
} }
} }
// Log level hierarchy
function LogLevelHierarchy(level) { function LogLevelHierarchy(level) {
switch (level) { const levels = { XML: 5, TRACE: 5, DEBUG: 4, INFO: 3, WARNING: 2, ERROR: 1 };
case "XML": return levels[level] || 3;
return 5;
case "TRACE":
return 5;
case "DEBUG":
return 4;
case "INFO":
return 3;
case "WARNING":
return 2;
case "ERROR":
return 1;
default:
return 3;
}
} }
// Socket.IO Middleware and Connection
io.use(socketAuthMiddleware);
io.on("connection", registerSocketEvents);
// Export logging helpers
exports.createLogEvent = createLogEvent; exports.createLogEvent = createLogEvent;
exports.createXmlEvent = createXmlEvent; exports.createXmlEvent = createXmlEvent;
exports.createJsonEvent = createJsonEvent; exports.createJsonEvent = createJsonEvent;