Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/yale-swe/f23-here
Browse files Browse the repository at this point in the history
  • Loading branch information
phucd5 committed Dec 2, 2023
2 parents c604bb4 + d4500c4 commit 05a4211
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 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,10 +113,10 @@ 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.body.user_id);
const user = await UserModel.findById(internalReq.user_id);
if (!user) {
return handleNotFound(
res,
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 05a4211

Please sign in to comment.