diff --git a/app/Http/Controllers/TasksController.php b/app/Http/Controllers/TasksController.php index e2bfe3a..d72349d 100644 --- a/app/Http/Controllers/TasksController.php +++ b/app/Http/Controllers/TasksController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Events\TaskDateUpdated; -use App\Events\TaskParticipationCancelled; +use App\Notifications\TaskDeleted; use App\Notifications\VolunteerAssignedToTask; use App\Events\VolunteerUnassignedFromTask; use App\Mail\TaskDateUpdateMail; @@ -18,7 +18,6 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; -use App\Mail\TaskParticipationCancelledMail; use App\Services\FormatDate; class TasksController extends Controller @@ -120,64 +119,27 @@ class TasksController extends Controller } } - public function deleteTask(Request $request, int $id): JsonResponse { - if(Gate::denies('delete', Task::class)) { - return response()->json(["message" => "Can't delete task"], 403); + public function deleteTask(Request $request, int $id): JsonResponse + { + if (Gate::denies('delete', Task::class)) { + return response()->json(['message' => "Can't delete task"], 403); } try { - $task = Task::with(['users'])->find($id); - $event = Event::find($task->events_id); + $task = Task::with('users')->findOrFail($id); + $event = Event::findOrFail($task->events_id); - if (!$task) { - return response()->json(["message" => "Task not found"], 404); - } - - $participants = $task->users; - - if (Carbon::now()->lessThanOrEqualTo($task->end)) { - if ($participants->isNotEmpty()) { - $webParticipants = $participants->filter(fn($user) => $user->web_notifications); - - if ($webParticipants->isNotEmpty()) { - $notification = Notification::create([ - 'content' => "La tâche {$task->name} pour l'événement {$event->name} a été supprimée. Vous n'y participez donc plus." - ]); - $notification->users()->attach($webParticipants->pluck('id')); - - foreach ($webParticipants as $user) { - broadcast(new TaskParticipationCancelled( - $user->id, - $task, - $event, - $notification->id, - )); - } - } - - foreach ($participants as $user) { - if ($user->email_notifications) { - $data = [ - 'name' => $user->name, - 'lastname' => $user->lastname, - 'taskName' => $task->name, - 'start' => $event->start, - 'end' => $task->end, - 'eventName' => $event->name, - ]; - Mail::to($user->email)->send(new TaskParticipationCancelledMail($data)); - } - } - } + if (Carbon::now()->lessThanOrEqualTo($task->end) && $task->users->isNotEmpty()) { + Notification::send($task->users, new TaskDeleted($task, $event)); } $task->delete(); - return response()->json(['message' => "Task deleted and participants notified"], 200); + return response()->json(['message' => 'Task deleted and participants notified'], 200); - } catch(\Exception $e) { + } catch (\Exception $e) { Log::error($e->getMessage()); - return response()->json(['message' => "Server error"], 500); + return response()->json(['message' => 'Server error'], 500); } }