diff --git a/app/Http/Controllers/NotificationsController.php b/app/Http/Controllers/NotificationsController.php index b370b1a..9dd9146 100644 --- a/app/Http/Controllers/NotificationsController.php +++ b/app/Http/Controllers/NotificationsController.php @@ -38,4 +38,18 @@ class NotificationsController extends Controller return response()->json(['message' => 'Erreur lors de la suppression'], 500); } } + + public function readNotifications(Request $request): JsonResponse { + try { + $user = $request->user(); + $notificationIds = $user->notifications()->pluck('notification_id'); + + $user->notifications()->updateExistingPivot($notificationIds, ['unread' => 0]); + + return response()->json(['message' => "Notifications marquées comme lues"], 200); + } catch(\Exception $e) { + Log::info($e->getMessage()); + return response()->json(['message' => "Server error"], 500); + } + } } diff --git a/routes/notifications.php b/routes/notifications.php index 8f34f18..ba11d61 100644 --- a/routes/notifications.php +++ b/routes/notifications.php @@ -7,3 +7,6 @@ Route::middleware(['web', 'auth:sanctum'])->get('/users/notifications', [Notific Route::middleware(['web', 'auth:sanctum'])->delete('/users/delete/notification/{notificationId}', [NotificationsController::class, 'deleteNotification']) ->whereNumber('notificationId'); + +Route::middleware(['web', 'auth:sanctum'])->post('/users/notifications/read', [NotificationsController::class, "readNotifications"]); + diff --git a/tests/Notifications/Read notifications.bru b/tests/Notifications/Read notifications.bru new file mode 100644 index 0000000..57bb525 --- /dev/null +++ b/tests/Notifications/Read notifications.bru @@ -0,0 +1,15 @@ +meta { + name: Read notifications + type: http + seq: 3 +} + +post { + url: {{url}}/api/users/notifications/read + body: none + auth: inherit +} + +settings { + encodeUrl: true +} diff --git a/tests/collection.bru b/tests/collection.bru index cc7d692..f502c76 100644 --- a/tests/collection.bru +++ b/tests/collection.bru @@ -1,4 +1,4 @@ headers { Accept: application/json - X-XSRF-TOKEN: eyJpdiI6IjZ3OWo1dG43ZFZrWkI0akozSkZOUnc9PSIsInZhbHVlIjoieE1kNTBPSGlReXlhOHY0MlF6MDUxa2VDc3c3ajdhVUFEUGdOZDNOcUJoZGdwaFBWQUZ2a2g3a1FOdnRwTnVNcW44V095bGlIaEkwZEZiUnIzWElVV29nNmZPZVNpSzlXU2FJMS9TOERqRWtOVStxVTEvVWZLQVFpQ2VzTlA1bkEiLCJtYWMiOiJiZjNkODFkMjk2YTZiNDU4ODlkMTAxNzU0ZTdlOTdmODU3YjEyNzI1ODk1ZjM0ZDVhN2U2YThhZmMwYWY4NjZhIiwidGFnIjoiIn0= + X-XSRF-TOKEN: eyJpdiI6ImFwSWk4NFdMcWJkcGF2YjRZQlQrRlE9PSIsInZhbHVlIjoiUXlyeFBkZFhDWmF1WE85OW1uY3c4ak1MM2dWMWlKRy9DRkN0MVNPNGVUUmthT1MxZ2hKREZhT0Y4d25idk5RdnZEcFh2UTRlbWpzYXRwSkNWL2Y5V2xlaEtYN09KMHpMQnQ4RzJvaElmWnFzTXEvSTJzZmZBenJEa2ZPelpydHoiLCJtYWMiOiIxNmIwNGRhNWQwYzIwZjBiYjA1ZTZlYzA2Y2NiY2FiNDY0NTZmNzU4NGIyZDYxODVhZGEwZWJiZmMwOGEwNzlmIiwidGFnIjoiIn0= }