Skip to content

Commit

Permalink
debugged notification functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
annayzhang1337 committed Dec 2, 2023
1 parent 6569ed9 commit d4500c4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions server/controllers/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ export const postMessage = async (req, res) => {
});
const saved_log = await notification.save();
for (let [key, value] of user.friends.entries()) {
const internalReq = {
user_id: key,
notif: saved_log
};
const notifRes = await addNotification(internalReq);
try {
const internalReq = {
user_id: key,
notif: saved_log
};
const notifRes = await addNotification(internalReq);
}
catch (notifErr) {
handleServerError(res, notifErr);
}
}
}

Expand All @@ -108,7 +113,7 @@ export const postMessage = async (req, res) => {
* @param {Object} req - The request object containing the message and user details.
* @param {Object} res - The response object used to reply to the client.
*/
export const addNotification = async (internalReq, res) => {
export const addNotification = async (internalReq) => {
try {
// Check if the user exists
const user = await UserModel.findById(internalReq.user_id);
Expand All @@ -120,14 +125,14 @@ export const addNotification = async (internalReq, res) => {
}
// Notification log
const notification = internalReq.notif;


// Save the notif log to the user log array
user.notificationLog.push(notification._id);
await user.save();

handleSuccess(res, notification);
return notification;
} catch (err) {
handleServerError(res, err);
throw new Error('An error occurred while adding the notification: ' + err.message);
}
};

Expand Down

0 comments on commit d4500c4

Please sign in to comment.