notifications socket event, ack api

This commit is contained in:
Ubuntu 2025-07-31 23:13:17 +05:30
parent e6a4c003a6
commit 9fef0f12ee
3 changed files with 74 additions and 2 deletions

View File

@ -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) => { exports.signup = async (req, res) => {
try { try {

View File

@ -164,5 +164,6 @@ router.put('/flag', upload.none(), authMiddleware.authenticateToken, appUserCont
router.post('/logs/add-report', authMiddleware.authenticateToken, appUserController.addReportText); router.post('/logs/add-report', authMiddleware.authenticateToken, appUserController.addReportText);
router.put('/acknowledge/:id',authMiddleware.authenticateToken, upload.none(), appUserController.updateChecked);
module.exports = router; module.exports = router;

View File

@ -308,7 +308,6 @@ wss.on("connection", (ws) => {
return; return;
} }
console.log("result1:-------------------", result1);
const hospitalCode = result1[0].hospital_code; const hospitalCode = result1[0].hospital_code;
@ -329,9 +328,48 @@ wss.on("connection", (ws) => {
} catch (error) { } catch (error) {
emitEvent("app-usersby-hospitalid", { error: error.message }, ws.userId); 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);
}
}