From ef33591c2ac20ce35916b9d5bc3b566b9d793c44 Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Sat, 28 Mar 2026 15:50:17 +0100 Subject: [PATCH] use EventDeleted notification --- app/Http/Controllers/EventsController.php | 56 ++++++----------------- 1 file changed, 13 insertions(+), 43 deletions(-) diff --git a/app/Http/Controllers/EventsController.php b/app/Http/Controllers/EventsController.php index b2fe48c..1a27fee 100644 --- a/app/Http/Controllers/EventsController.php +++ b/app/Http/Controllers/EventsController.php @@ -2,16 +2,14 @@ namespace App\Http\Controllers; -use App\Events\EventParticipationCancelled; use App\Models\Event; use App\Models\Notification; -use App\Mail\EventParticipationCancelledMail; use App\Models\Task; +use App\Notifications\EventDeleted; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Mail; use Carbon\Carbon; @@ -80,60 +78,32 @@ class EventsController extends Controller } - public function delete(Request $request, int $id): JsonResponse { - + public function delete(Request $request, int $id): JsonResponse + { if (Gate::denies('delete', Event::class)) { return response()->json(['message' => 'Forbidden'], 403); } try { - $event = Event::with('tasks.users')->find($id); - - if (!$event) { - return response()->json(['message' => 'Event not found'], 404); - } - - $participants = $event->tasks->flatMap(function ($task) { - return $task->users; - })->unique('id'); + $event = Event::with('tasks.users')->findOrFail($id); if (Carbon::now()->lessThanOrEqualTo($event->end)) { - $webParticipants = $participants->filter(fn($user) => $user->web_notifications); + $participants = $event->tasks + ->flatMap(fn($task) => $task->users) + ->unique('id'); - if ($webParticipants->isNotEmpty()) { - $notification = Notification::create([ - 'content' => "L'événement {$event->name} a été supprimé. Vous n'y participez donc plus." - ]); - $notification->users()->attach($webParticipants->pluck('id')); - - foreach ($webParticipants as $user) { - broadcast(new EventParticipationCancelled( - $user->id, - $event, - $notification->id, - )); - } - } - - foreach ($participants as $user) { - if ($user->email_notifications) { - $data = [ - 'name' => $user->name, - 'lastname' => $user->lastname, - 'eventName' => $event->name, - 'start' => $event->start, - 'end' => $event->end, - ]; - Mail::to($user->email)->send(new EventParticipationCancelledMail($data)); - } + if ($participants->isNotEmpty()) { + Notification::send($participants, new EventDeleted($event)); } } $event->delete(); - return response()->json(["message" => "Event deleted and participants notified"]); + + return response()->json(['message' => 'Event deleted and participants notified']); + } catch (\Exception $e) { Log::error("Event delete error: " . $e->getMessage()); - return response()->json(['message' => "Server error"], 500); + return response()->json(['message' => 'Server error'], 500); } }