@@ -86,11 +86,16 @@ export const postMessage = async (req, res) => {
86
86
} ) ;
87
87
const saved_log = await notification . save ( ) ;
88
88
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
+ }
94
99
}
95
100
}
96
101
@@ -108,10 +113,10 @@ export const postMessage = async (req, res) => {
108
113
* @param {Object } req - The request object containing the message and user details.
109
114
* @param {Object } res - The response object used to reply to the client.
110
115
*/
111
- export const addNotification = async ( internalReq , res ) => {
116
+ export const addNotification = async ( internalReq ) => {
112
117
try {
113
118
// Check if the user exists
114
- const user = await UserModel . findById ( internalReq . body . user_id ) ;
119
+ const user = await UserModel . findById ( internalReq . user_id ) ;
115
120
if ( ! user ) {
116
121
return handleNotFound (
117
122
res ,
@@ -120,14 +125,14 @@ export const addNotification = async (internalReq, res) => {
120
125
}
121
126
// Notification log
122
127
const notification = internalReq . notif ;
128
+
123
129
124
130
// Save the notif log to the user log array
125
131
user . notificationLog . push ( notification . _id ) ;
126
132
await user . save ( ) ;
127
-
128
- handleSuccess ( res , notification ) ;
133
+ return notification ;
129
134
} catch ( err ) {
130
- handleServerError ( res , err ) ;
135
+ throw new Error ( 'An error occurred while adding the notification: ' + err . message ) ;
131
136
}
132
137
} ;
133
138
0 commit comments