use EventDeleted notification

This commit is contained in:
2026-03-28 15:50:17 +01:00
parent 551909aee7
commit ef33591c2a
+13 -43
View File
@@ -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);
}
}