Skip to content

Commit 05a4211

Browse files
committed
Merge branch 'main' of https://github.com/yale-swe/f23-here
2 parents c604bb4 + d4500c4 commit 05a4211

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

server/controllers/message.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,16 @@ export const postMessage = async (req, res) => {
8686
});
8787
const saved_log = await notification.save();
8888
for (let [key, value] of user.friends.entries()) {
89-
const internalReq = {
90-
user_id: key,
91-
notif: saved_log
92-
};
93-
const notifRes = await addNotification(internalReq);
89+
try {
90+
const internalReq = {
91+
user_id: key,
92+
notif: saved_log
93+
};
94+
const notifRes = await addNotification(internalReq);
95+
}
96+
catch (notifErr) {
97+
handleServerError(res, notifErr);
98+
}
9499
}
95100
}
96101

@@ -108,10 +113,10 @@ export const postMessage = async (req, res) => {
108113
* @param {Object} req - The request object containing the message and user details.
109114
* @param {Object} res - The response object used to reply to the client.
110115
*/
111-
export const addNotification = async (internalReq, res) => {
116+
export const addNotification = async (internalReq) => {
112117
try {
113118
// Check if the user exists
114-
const user = await UserModel.findById(internalReq.body.user_id);
119+
const user = await UserModel.findById(internalReq.user_id);
115120
if (!user) {
116121
return handleNotFound(
117122
res,
@@ -120,14 +125,14 @@ export const addNotification = async (internalReq, res) => {
120125
}
121126
// Notification log
122127
const notification = internalReq.notif;
128+
123129

124130
// Save the notif log to the user log array
125131
user.notificationLog.push(notification._id);
126132
await user.save();
127-
128-
handleSuccess(res, notification);
133+
return notification;
129134
} catch (err) {
130-
handleServerError(res, err);
135+
throw new Error('An error occurred while adding the notification: ' + err.message);
131136
}
132137
};
133138

0 commit comments

Comments
 (0)