use EventDeleted notification
This commit is contained in:
@@ -2,16 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Events\EventParticipationCancelled;
|
|
||||||
use App\Models\Event;
|
use App\Models\Event;
|
||||||
use App\Models\Notification;
|
use App\Models\Notification;
|
||||||
use App\Mail\EventParticipationCancelledMail;
|
|
||||||
use App\Models\Task;
|
use App\Models\Task;
|
||||||
|
use App\Notifications\EventDeleted;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Mail;
|
|
||||||
use Carbon\Carbon;
|
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)) {
|
if (Gate::denies('delete', Event::class)) {
|
||||||
return response()->json(['message' => 'Forbidden'], 403);
|
return response()->json(['message' => 'Forbidden'], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$event = Event::with('tasks.users')->find($id);
|
$event = Event::with('tasks.users')->findOrFail($id);
|
||||||
|
|
||||||
if (!$event) {
|
|
||||||
return response()->json(['message' => 'Event not found'], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$participants = $event->tasks->flatMap(function ($task) {
|
|
||||||
return $task->users;
|
|
||||||
})->unique('id');
|
|
||||||
|
|
||||||
if (Carbon::now()->lessThanOrEqualTo($event->end)) {
|
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()) {
|
if ($participants->isNotEmpty()) {
|
||||||
$notification = Notification::create([
|
Notification::send($participants, new EventDeleted($event));
|
||||||
'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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$event->delete();
|
$event->delete();
|
||||||
return response()->json(["message" => "Event deleted and participants notified"]);
|
|
||||||
|
return response()->json(['message' => 'Event deleted and participants notified']);
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::error("Event delete error: " . $e->getMessage());
|
Log::error("Event delete error: " . $e->getMessage());
|
||||||
return response()->json(['message' => "Server error"], 500);
|
return response()->json(['message' => 'Server error'], 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user