From 8e93a0aba21256eb0a1892c3a461a15f66179b10 Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Tue, 16 Dec 2025 10:11:08 +0100 Subject: [PATCH] delete notification and notification_user when validate --- .../Controllers/NotificationsController.php | 23 +++++++++++++++++-- app/Http/Controllers/UserController.php | 9 +++++++- app/Models/Notification.php | 7 +++++- app/Models/User.php | 7 +++++- public/index.html | 2 +- routes/users.php | 10 ++++++-- tests/collection.bru | 2 +- 7 files changed, 51 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/NotificationsController.php b/app/Http/Controllers/NotificationsController.php index d990432..5d70b58 100644 --- a/app/Http/Controllers/NotificationsController.php +++ b/app/Http/Controllers/NotificationsController.php @@ -10,7 +10,7 @@ use Illuminate\Support\Facades\Log; class NotificationsController extends Controller { - public function create(Request $request, int $id): JsonResponse { + public function createNotification(Request $request, int $id): JsonResponse { $notification = Notification::create([ "content" => $request["content"] ]); @@ -23,7 +23,6 @@ class NotificationsController extends Controller public function getNotifications(Request $request, int $id): JsonResponse { - try { $user = User::findOrFail($id); @@ -35,4 +34,24 @@ class NotificationsController extends Controller return response()->json(['message' => "Server error"], 500); } } + + + + public function deleteNotification(int $userId, int $notificationId): JsonResponse { + try { + $user = User::findOrFail($userId); + $user->notifications()->detach($notificationId); + + $notification = Notification::find($notificationId); + if ($notification && $notification->users()->count() === 0) { + $notification->delete(); + } + + return response()->json(['message' => 'Notification supprimée avec succès']); + } catch (\Exception $e) { + Log::info($e->getMessage()); + return response()->json(['message' => 'Erreur lors de la suppression'], 500); + } + } + } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index d8a9a88..1f45734 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -120,12 +120,19 @@ class UserController extends Controller public function validate(Request $request, int $id): JsonResponse { - if($request->user()->role != 1 || $request->user()->role == null){ + if($request->user()->role != 1 && $request->user()->role != 2 || $request->user()->role == null){ return response()->json(["message" => "Can't validate this user"], 403); } try { $user = User::find($id); + $content = "Nouvelle demande d'inscription : {$user->name} {$user->lastname}"; + $notification = Notification::where('content', $content)->first(); + + if ($notification) { + $notification->users()->detach(); + $notification->delete(); + } $user->validate = 1; $user->role = 3; diff --git a/app/Models/Notification.php b/app/Models/Notification.php index ea6ac32..33b7fa6 100644 --- a/app/Models/Notification.php +++ b/app/Models/Notification.php @@ -12,6 +12,11 @@ class Notification extends Model public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { - return $this->belongsToMany(User::class, 'notification_user', 'notification_id', 'user_id'); + return $this->belongsToMany( + User::class, + 'notification_user', + 'notification_id', + 'user_id' + )->withTimestamps(); } } diff --git a/app/Models/User.php b/app/Models/User.php index 4681041..b37b7a2 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -57,6 +57,11 @@ class User extends Authenticatable public function notifications(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { - return $this->belongsToMany(Notification::class, 'notification_user', 'user_id', 'notification_id'); + return $this->belongsToMany( + Notification::class, + 'notification_user', + 'user_id', + 'notification_id' + )->withTimestamps(); } } diff --git a/public/index.html b/public/index.html index 1d2573d..44047c1 100644 --- a/public/index.html +++ b/public/index.html @@ -5,7 +5,7 @@ Comité des fêtes de Beaupont - + diff --git a/routes/users.php b/routes/users.php index 0ad5ef2..ca9f953 100644 --- a/routes/users.php +++ b/routes/users.php @@ -37,9 +37,15 @@ Route::middleware(['web', 'auth:sanctum'])->get('/users/{id}/tasks', [UserContro Route::middleware(['web', 'auth:sanctum'])->get('/users/tasks/{idTask}', [UserController::class, "getUserTaskById"]); -Route::middleware(['web', 'auth:sanctum'])->get('/users/{id}/notifications', [NotificationsController::class, "getNotifications"])->whereNumber('id'); +Route::middleware(['web', 'auth:sanctum'])->get('/users/{id}/notifications', [NotificationsController::class, "getNotifications"]) + ->whereNumber('id'); -Route::middleware(['web', 'auth:sanctum'])->post("/users/{id}/create/notification", [NotificationsController::class, "create"])->whereNumber('id'); +Route::middleware(['web', 'auth:sanctum'])->post("/users/{id}/create/notification", [NotificationsController::class, "createNotification"]) + ->whereNumber('id'); + +Route::middleware(['web', 'auth:sanctum'])->delete('/users/{userId}/delete/notification/{notificationId}', [NotificationsController::class, 'deleteNotification']) + ->whereNumber('userId') + ->whereNumber('notificationId'); Route::middleware(['web', 'auth:sanctum'])->get('/users/search', [SearchController::class, "searchUsers"]); diff --git a/tests/collection.bru b/tests/collection.bru index 242bd56..084fc3b 100644 --- a/tests/collection.bru +++ b/tests/collection.bru @@ -1,4 +1,4 @@ headers { Accept: application/json - X-XSRF-TOKEN: eyJpdiI6IjdETzVjT3JoTWU2VE5nMVBKNnBUUVE9PSIsInZhbHVlIjoiWjhvNTRqTzY2MEhxM01MK1lSOFB0NnBvRU4rL1U2OEg0RTdvbXc5ZVQvK0JJMFhWUDFWWS9iSEFsZzdtS3hYd1ZkY2tJcWNpMDNVMlVnVGFGdTJGQzhqSFdCc0V4RVlBUkdObkVhdVh4RisxMWEvcFh4NXZkQTZyL3kvbmJ6T0EiLCJtYWMiOiIyYzA2NjkxYjRhMWE5ZGE2ZjNhZGFjOGFkZDBjN2Y0NTBiMDhiMzAwOThlNWVhOWQzYzVmNzU1NzczNWZjMWIzIiwidGFnIjoiIn0= + X-XSRF-TOKEN: eyJpdiI6Ii9YZURUS2taQlBjVXB3azhoelpqS3c9PSIsInZhbHVlIjoiYzdpSmVhVWlZRnlvVWR1RWlHbEtwcUp5R1dQNUJXckFTRm9Tb0xYcGszdmZNb0p4NTFDM2tBMEZjTkdqYWlnSmRWZi9LdFgyczJZam1DdUVUcW84TXBOaDRmU1g5c2pseDVKMW50eTJGd3cxd3p5RjV2b2pGOGNwYzlmWDFkREkiLCJtYWMiOiI5NDY1YWM3YmE0ZDgyNWMxZDQwNTRiZWIxNWU3OTA0NjYxNDkxMjRmZmVkOTA1Y2U3YTE2ZGY2YWViYWE2ZmRjIiwidGFnIjoiIn0= }