diff --git a/src/controllers/appUserController.js b/src/controllers/appUserController.js index 30fb82d..1604f86 100644 --- a/src/controllers/appUserController.js +++ b/src/controllers/appUserController.js @@ -291,6 +291,39 @@ exports.addReportText = async (req, res) => { } }; +exports.updateChecked = async (req,res) =>{ + try { + const app_user_id = req.params.id; + if (!["Admin", 8].includes(req.user.role)) { + return res.status(403).json({ error: "Unauthorized to approve IDs" }); + } + + const updateQuery = ` + UPDATE app_users + SET checked = ? + WHERE id = ? + `; + const result = await db.query(updateQuery, [1, app_user_id]); + if (result.affectedRows > 0) { + return res.status(200).json({ + status: 'success', + message: 'Acknowledged successfully', + + }); + } else { + return res.status(404).json({ + status: 'error', + message: 'No matching record found to update checked', + }); + } + + + } catch (error) { + console.error("Error updating checked:", error); + return res.status(500).json({ error: "Internal server error" }); + } +} + exports.signup = async (req, res) => { try { diff --git a/src/routes/appUsers.js b/src/routes/appUsers.js index 5dfe4c3..67e4e32 100644 --- a/src/routes/appUsers.js +++ b/src/routes/appUsers.js @@ -164,5 +164,6 @@ router.put('/flag', upload.none(), authMiddleware.authenticateToken, appUserCont router.post('/logs/add-report', authMiddleware.authenticateToken, appUserController.addReportText); +router.put('/acknowledge/:id',authMiddleware.authenticateToken, upload.none(), appUserController.updateChecked); module.exports = router; \ No newline at end of file diff --git a/src/services/secondaryWebsocket.js b/src/services/secondaryWebsocket.js index 55b63f0..571ae9e 100644 --- a/src/services/secondaryWebsocket.js +++ b/src/services/secondaryWebsocket.js @@ -308,7 +308,6 @@ wss.on("connection", (ws) => { return; } - console.log("result1:-------------------", result1); const hospitalCode = result1[0].hospital_code; @@ -329,9 +328,48 @@ wss.on("connection", (ws) => { } catch (error) { emitEvent("app-usersby-hospitalid", { error: error.message }, ws.userId); } + } + + if(data.event === "get-signup-notifications"){ + + if (!data.token) { + emitEvent("get-signup-notifications", { error: "Token missing" }, ws.userId); + return; + } + + try { + const decoded = jwt.verify(data.token, process.env.JWT_ACCESS_TOKEN_SECRET); + const allowedRoles = ['Admin',8]; + + // Role-based access check + if (!allowedRoles.includes(decoded.role)) { + emitEvent("get-signup-notifications", { error: "You are not authorized!" }, decoded.id); + return; } - + + + // Fetch documents for hospital + const hospital_code = await db.query( + "SELECT hospital_code FROM hospitals WHERE id = ?", + [decoded.id] + ); + + // Fetch notifications of new signup + const notifications = await db.query( + "SELECT * FROM hospitals WHERE hospital_code = ? AND checked=0", + [hospital_code] + ); + + emitEvent("get-signup-notifications", { + message: "Notifications fetched successfully.", + notifications + }, decoded.id); + + } catch (error) { + emitEvent("get-signup-notifications", { error: error.message }, ws.userId); + } + }