@@ -182,36 +182,40 @@ public function update(Request $request, $notificationId)
182
182
/**
183
183
* Remove the specified resource from storage.
184
184
*/
185
- public function destroy ()
185
+ public function destroy (Request $ request )
186
186
{
187
187
try {
188
-
189
- UserNotification::query ()->where ([
190
- ['user_id ' , auth ()->id ()],
191
- ['status ' , 'unread ' ]
192
- ])->update ([
193
- 'status ' => 'read '
194
- ]);
195
-
188
+ // Check if the user is authenticated
189
+ $ user = Auth::user ();
190
+
191
+ // Update the status of all notifications (read or unread) for the authenticated user to 'read'
192
+ UserNotification::query ()
193
+ ->where ('user_id ' , $ user ->id )
194
+ ->update (['status ' => 'read ' ]);
195
+
196
+ // Return a success response
196
197
return response ()->json ([
197
- 'status ' => 'success ' ,
198
- 'message ' => 'notifications cleared successfully ' ,
199
- 'status_code ' => Response::HTTP_OK ,
200
- 'data ' => UserNotification::query ()->where ('user_id ' , auth ()->id ())->get (),
198
+ 'data ' => 'All notifications have been cleared. ' ,
199
+ 'message ' => 'All notifications cleared successfully ' ,
200
+ 'status_code ' => Response::HTTP_OK
201
201
], Response::HTTP_OK );
202
202
} catch (ModelNotFoundException $ exception ) {
203
+ // Handle case where notifications are not found
203
204
return response ()->json ([
204
- 'status ' => 'error ' ,
205
- 'message ' => 'notifications not found ' ,
206
- 'status_code ' => Response::HTTP_NOT_FOUND ,
205
+ 'data ' => null ,
206
+ 'error ' => 'Notifications not found ' ,
207
+ 'message ' => 'No notifications found to clear ' ,
208
+ 'status_code ' => Response::HTTP_NOT_FOUND
207
209
], Response::HTTP_NOT_FOUND );
208
210
} catch (\Exception $ exception ) {
209
211
Log::error ($ exception ->getMessage ());
210
212
return response ()->json ([
211
- 'status ' => 'error ' ,
212
- 'message ' => 'something went wrong ' ,
213
- 'status_code ' => Response::HTTP_INTERNAL_SERVER_ERROR ,
213
+ 'data ' => null ,
214
+ 'error ' => 'Internal Server Error ' ,
215
+ 'message ' => 'Something went wrong while clearing notifications ' ,
216
+ 'status_code ' => Response::HTTP_INTERNAL_SERVER_ERROR
214
217
], Response::HTTP_INTERNAL_SERVER_ERROR );
215
218
}
216
219
}
220
+
217
221
}
0 commit comments