feature/IO-3026-Enhanced-Notifications - final revisions
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -109,8 +109,10 @@ const mapStateToProps = createStructuredSelector({
|
|||||||
alerts: selectAlerts
|
alerts: selectAlerts
|
||||||
});
|
});
|
||||||
|
|
||||||
// images.imex.online/alerts/alerts.json
|
const ALERT_FILE_URL = InstanceRenderManager({
|
||||||
const ALERT_FILE_URL = "http://localhost:5000/alerts.json";
|
imex: "https://images.imex.online/alerts/alerts-imex.json",
|
||||||
|
rome: "https://images.imex.online/alerts/alerts-rome.json"
|
||||||
|
});
|
||||||
|
|
||||||
const mapDispatchToProps = (dispatch) => ({
|
const mapDispatchToProps = (dispatch) => ({
|
||||||
setAlerts: (alerts) => dispatch(addAlerts(alerts))
|
setAlerts: (alerts) => dispatch(addAlerts(alerts))
|
||||||
@@ -145,17 +147,15 @@ export function Manage({ conflict, bodyshop, alerts, setAlerts }) {
|
|||||||
fetchAlerts();
|
fetchAlerts();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Use useEffect to watch for new alerts
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("Alerts in Manage component:", alerts);
|
|
||||||
if (alerts && Object.keys(alerts).length > 0) {
|
if (alerts && Object.keys(alerts).length > 0) {
|
||||||
// Convert the alerts object into an array
|
// Convert the alerts object into an array
|
||||||
const alertArray = Object.values(alerts);
|
const alertArray = Object.values(alerts);
|
||||||
|
|
||||||
// Filter out alerts that have already been displayed
|
// Filter out alerts that have already been dismissed
|
||||||
const newAlerts = alertArray.filter((alert) => !displayedAlertIds.includes(alert.id));
|
const newAlerts = alertArray.filter((alert) => !displayedAlertIds.includes(alert.id));
|
||||||
|
|
||||||
console.log("New alerts to display:", newAlerts);
|
|
||||||
|
|
||||||
newAlerts.forEach((alert) => {
|
newAlerts.forEach((alert) => {
|
||||||
// Display the notification
|
// Display the notification
|
||||||
notification.open({
|
notification.open({
|
||||||
@@ -165,18 +165,19 @@ export function Manage({ conflict, bodyshop, alerts, setAlerts }) {
|
|||||||
type: alert.type || "info",
|
type: alert.type || "info",
|
||||||
duration: 0,
|
duration: 0,
|
||||||
placement: "bottomRight",
|
placement: "bottomRight",
|
||||||
closable: true
|
closable: true,
|
||||||
});
|
onClose: () => {
|
||||||
|
// When the notification is closed, update displayed alerts state and localStorage
|
||||||
// Update displayed alerts state and localStorage
|
|
||||||
setDisplayedAlertIds((prevIds) => {
|
setDisplayedAlertIds((prevIds) => {
|
||||||
const updatedIds = [...prevIds, alert.id];
|
const updatedIds = [...prevIds, alert.id];
|
||||||
localStorage.setItem("displayedAlerts", JSON.stringify(updatedIds));
|
localStorage.setItem("displayedAlerts", JSON.stringify(updatedIds));
|
||||||
return updatedIds;
|
return updatedIds;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [alerts]);
|
}, [alerts, displayedAlertIds]);
|
||||||
|
|
||||||
// useEffect(() => {
|
// useEffect(() => {
|
||||||
// const fetchAlerts = async () => {
|
// const fetchAlerts = async () => {
|
||||||
|
|||||||
@@ -68,11 +68,6 @@ export const setUpdateAvailable = (isUpdateAvailable) => ({
|
|||||||
payload: isUpdateAvailable
|
payload: isUpdateAvailable
|
||||||
});
|
});
|
||||||
|
|
||||||
export const setAlerts = (alerts) => ({
|
|
||||||
type: ApplicationActionTypes.SET_ALERTS,
|
|
||||||
payload: alerts
|
|
||||||
});
|
|
||||||
|
|
||||||
export const addAlerts = (alerts) => ({
|
export const addAlerts = (alerts) => ({
|
||||||
type: ApplicationActionTypes.ADD_ALERTS,
|
type: ApplicationActionTypes.ADD_ALERTS,
|
||||||
payload: alerts
|
payload: alerts
|
||||||
|
|||||||
@@ -92,19 +92,11 @@ const applicationReducer = (state = INITIAL_STATE, action) => {
|
|||||||
case ApplicationActionTypes.SET_WSS_STATUS: {
|
case ApplicationActionTypes.SET_WSS_STATUS: {
|
||||||
return { ...state, wssStatus: action.payload };
|
return { ...state, wssStatus: action.payload };
|
||||||
}
|
}
|
||||||
case ApplicationActionTypes.SET_ALERTS: {
|
|
||||||
const alertsMap = {};
|
|
||||||
action.payload.forEach((alert) => {
|
|
||||||
alertsMap[alert.id] = alert;
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
...state,
|
|
||||||
alerts: alertsMap
|
|
||||||
};
|
|
||||||
}
|
|
||||||
case ApplicationActionTypes.ADD_ALERTS: {
|
case ApplicationActionTypes.ADD_ALERTS: {
|
||||||
const newAlertsMap = { ...state.alerts };
|
const newAlertsMap = { ...state.alerts };
|
||||||
action.payload.forEach((alert) => {
|
|
||||||
|
action.payload.alerts.forEach((alert) => {
|
||||||
newAlertsMap[alert.id] = alert;
|
newAlertsMap[alert.id] = alert;
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ const ApplicationActionTypes = {
|
|||||||
SET_PROBLEM_JOBS: "SET_PROBLEM_JOBS",
|
SET_PROBLEM_JOBS: "SET_PROBLEM_JOBS",
|
||||||
SET_UPDATE_AVAILABLE: "SET_UPDATE_AVAILABLE",
|
SET_UPDATE_AVAILABLE: "SET_UPDATE_AVAILABLE",
|
||||||
SET_WSS_STATUS: "SET_WSS_STATUS",
|
SET_WSS_STATUS: "SET_WSS_STATUS",
|
||||||
SET_ALERTS: "SET_ALERTS",
|
|
||||||
ADD_ALERTS: "ADD_ALERTS"
|
ADD_ALERTS: "ADD_ALERTS"
|
||||||
};
|
};
|
||||||
export default ApplicationActionTypes;
|
export default ApplicationActionTypes;
|
||||||
|
|||||||
76
server/alerts/alertcheck.js
Normal file
76
server/alerts/alertcheck.js
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
const axios = require("axios");
|
||||||
|
const _ = require("lodash");
|
||||||
|
const { default: InstanceMgr } = require("../utils/instanceMgr"); // For deep object comparison
|
||||||
|
|
||||||
|
// Constants
|
||||||
|
const ALERTS_REDIS_KEY = "alerts_data"; // The key under which we'll store alerts in Redis
|
||||||
|
const GLOBAL_SOCKET_ID = "global"; // Use 'global' as a socketId to store global data
|
||||||
|
|
||||||
|
const ALERT_FILE_URL = InstanceMgr({
|
||||||
|
imex: "https://images.imex.online/alerts/alerts-imex.json",
|
||||||
|
rome: "https://images.imex.online/alerts/alerts-rome.json"
|
||||||
|
});
|
||||||
|
|
||||||
|
const alertCheck = async (req, res) => {
|
||||||
|
// Access Redis helper functions
|
||||||
|
const { ioRedis, logger } = req;
|
||||||
|
const { getSessionData, setSessionData } = req.sessionUtils;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the JSON Alert file from the server
|
||||||
|
const response = await axios.get(ALERT_FILE_URL);
|
||||||
|
const currentAlerts = response.data;
|
||||||
|
// Retrieve stored alerts from Redis using a global socketId
|
||||||
|
const storedAlerts = await getSessionData(GLOBAL_SOCKET_ID, ALERTS_REDIS_KEY);
|
||||||
|
if (!storedAlerts) {
|
||||||
|
// Alerts not in Redis, store them
|
||||||
|
await setSessionData(GLOBAL_SOCKET_ID, ALERTS_REDIS_KEY, currentAlerts);
|
||||||
|
logger.logger.debug("Alerts added to Redis for the first time.");
|
||||||
|
|
||||||
|
// Emit to clients
|
||||||
|
if (ioRedis) {
|
||||||
|
ioRedis.emit("bodyshop-message", {
|
||||||
|
type: "alert-update",
|
||||||
|
payload: currentAlerts
|
||||||
|
});
|
||||||
|
logger.logger.debug("Alerts emitted to clients for the first time.");
|
||||||
|
} else {
|
||||||
|
logger.log("Socket.IO instance not found. (1)", "error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send("Alerts added to Redis and emitted to clients.");
|
||||||
|
} else {
|
||||||
|
// Alerts are in Redis, compare them
|
||||||
|
if (!_.isEqual(currentAlerts, storedAlerts)) {
|
||||||
|
// Alerts are different, update Redis and emit to clients
|
||||||
|
await setSessionData(GLOBAL_SOCKET_ID, ALERTS_REDIS_KEY, currentAlerts);
|
||||||
|
logger.logger.debug("Alerts updated in Redis.");
|
||||||
|
|
||||||
|
// Emit the new alerts to all connected clients
|
||||||
|
if (ioRedis) {
|
||||||
|
ioRedis.emit("bodyshop-message", {
|
||||||
|
type: "alert-update",
|
||||||
|
payload: currentAlerts
|
||||||
|
});
|
||||||
|
logger.logger.debug("Alerts emitted to clients after update.");
|
||||||
|
} else {
|
||||||
|
logger.log("Socket.IO instance not found. (2)", "error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send("Alerts updated in Redis and emitted to clients.");
|
||||||
|
} else {
|
||||||
|
return res.status(200).send("No changes in alerts.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.log("Error in alertCheck:", "error", null, null, {
|
||||||
|
error: {
|
||||||
|
message: error.message,
|
||||||
|
stack: error.stack
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return res.status(500).send("Internal server error.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = { alertCheck };
|
||||||
@@ -12,6 +12,7 @@ const validateFirebaseIdTokenMiddleware = require("../middleware/validateFirebas
|
|||||||
const withUserGraphQLClientMiddleware = require("../middleware/withUserGraphQLClientMiddleware");
|
const withUserGraphQLClientMiddleware = require("../middleware/withUserGraphQLClientMiddleware");
|
||||||
const { taskAssignedEmail, tasksRemindEmail } = require("../email/tasksEmails");
|
const { taskAssignedEmail, tasksRemindEmail } = require("../email/tasksEmails");
|
||||||
const { canvastest } = require("../render/canvas-handler");
|
const { canvastest } = require("../render/canvas-handler");
|
||||||
|
const { alertCheck } = require("../alerts/alertcheck");
|
||||||
|
|
||||||
//Test route to ensure Express is responding.
|
//Test route to ensure Express is responding.
|
||||||
router.get("/test", async function (req, res) {
|
router.get("/test", async function (req, res) {
|
||||||
@@ -53,4 +54,7 @@ router.post("/taskHandler", validateFirebaseIdTokenMiddleware, taskHandler.taskH
|
|||||||
// Canvas Test
|
// Canvas Test
|
||||||
router.post("/canvastest", validateFirebaseIdTokenMiddleware, canvastest);
|
router.post("/canvastest", validateFirebaseIdTokenMiddleware, canvastest);
|
||||||
|
|
||||||
|
// Alert Check
|
||||||
|
router.post("/alertcheck", eventAuthorizationMiddleware, alertCheck);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user