From 4a0087104cc51d2fc31a9e122ec5622418b46405 Mon Sep 17 00:00:00 2001 From: Giovanni-Josserand Date: Sat, 28 Mar 2026 15:43:50 +0100 Subject: [PATCH] use VolunteerUnassignedFromTask notification --- app/Http/Controllers/TasksController.php | 61 ++++++------------------ 1 file changed, 15 insertions(+), 46 deletions(-) diff --git a/app/Http/Controllers/TasksController.php b/app/Http/Controllers/TasksController.php index b3588c3..46857ba 100644 --- a/app/Http/Controllers/TasksController.php +++ b/app/Http/Controllers/TasksController.php @@ -5,10 +5,9 @@ namespace App\Http\Controllers; use App\Notifications\TaskDateUpdated; use App\Notifications\TaskDeleted; use App\Notifications\VolunteerAssignedToTask; -use App\Events\VolunteerUnassignedFromTask; +use App\Notifications\VolunteerUnassignedFromTask; use App\Models\Event; use App\Models\Notification; -use App\Mail\VolunteerUnassignToTaskMail; use App\Models\Task; use App\Models\User; use Carbon\Carbon; @@ -16,7 +15,6 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Mail; use App\Services\FormatDate; class TasksController extends Controller @@ -202,61 +200,32 @@ class TasksController extends Controller } } - public function unassignUser(Request $request, int $id): JsonResponse { - - if(Gate::denies('unassignOther', Task::class)) { - return response()->json(["message" => "Unauthorized"], 403); + public function unassignUser(Request $request, int $id): JsonResponse + { + if (Gate::denies('unassignOther', Task::class)) { + return response()->json(['message' => 'Unauthorized'], 403); } try { - $task = Task::find($request["task_id"]); - $event = Event::find($task->events_id); + $task = Task::findOrFail($request['task_id']); + $user = User::findOrFail($id); - if (!$task) { - return response()->json(["message" => "Task not found"], 404); + if (!$task->users()->where('user_id', $id)->exists()) { + return response()->json(['message' => 'User is not assigned to this task'], 400); } - $isAssigned = $task->users()->where('user_id', $id)->exists(); - if (!$isAssigned) { - return response()->json(["message" => "User is not assigned to this task"], 400); - } - - $user = User::find($id); $task->users()->detach($id); - if ($user && Carbon::now()->lessThanOrEqualTo($task->end)) { - if ($user->web_notifications) { - $notification = Notification::create([ - 'content' => "Vous avez été désassigné de la tâche {$task->name} de l'événement {$event->name}." - ]); - $notification->users()->attach($user->id); - - broadcast(new VolunteerUnassignedFromTask( - $user->id, - $task, - $event, - $notification->id, - )); - } - - if ($user->email_notifications) { - $data = [ - 'name' => $user->name, - 'lastname' => $user->lastname, - 'taskName' => $task->name, - 'start' => $task->start, - 'end' => $task->end, - 'eventName' => $event->name, - ]; - Mail::to($user->email)->send(new VolunteerUnassignToTaskMail($data)); - } + if (Carbon::now()->lessThanOrEqualTo($task->end)) { + $event = Event::findOrFail($task->events_id); + $user->notify(new VolunteerUnassignedFromTask($task, $event)); } - return response()->json(['message' => "User unassigned successfully"], 200); + return response()->json(['message' => 'User unassigned successfully'], 200); - } catch(\Exception $e) { + } catch (\Exception $e) { Log::error("UnassignUser error: " . $e->getMessage()); - return response()->json(['message' => "Server error"], 500); + return response()->json(['message' => 'Server error'], 500); } } }