diff --git a/app/Http/Controllers/TasksController.php b/app/Http/Controllers/TasksController.php index 9c655c4..e2bfe3a 100644 --- a/app/Http/Controllers/TasksController.php +++ b/app/Http/Controllers/TasksController.php @@ -4,12 +4,11 @@ namespace App\Http\Controllers; use App\Events\TaskDateUpdated; use App\Events\TaskParticipationCancelled; -use App\Events\VolunteerAssignedToTask; +use App\Notifications\VolunteerAssignedToTask; use App\Events\VolunteerUnassignedFromTask; use App\Mail\TaskDateUpdateMail; use App\Models\Event; use App\Models\Notification; -use App\Mail\VolunteerAssignToTaskMail; use App\Mail\VolunteerUnassignToTaskMail; use App\Models\Task; use App\Models\User; @@ -74,61 +73,36 @@ class TasksController extends Controller } } - public function assignUser(request $request, int $id): JsonResponse { - - if(Gate::denies('assignOther', Task::class)) { - return response()->json(["message" => "Can't create task"], 403); + public function assignUser(Request $request, int $id): JsonResponse + { + if (Gate::denies('assignOther', Task::class)) { + return response()->json(['message' => "Can't create task"], 403); } try { - $task = Task::find($request["task_id"]); - $event = Event::find($task->events_id); + $task = Task::findOrFail($request['task_id']); + $event = Event::findOrFail($task->events_id); + $user = User::findOrFail($id); if ($task->users()->where('user_id', $id)->exists()) { - return response()->json(["message" => "User already assigned to this task"], 400); + return response()->json(['message' => 'User already assigned to this task'], 400); } - if($task->max_participants === $task->users()->count()) { - return response()->json(["message" => "Max participants reached"], 403); + if ($task->max_participants === $task->users()->count()) { + return response()->json(['message' => 'Max participants reached'], 403); } - $user = User::find($id); $task->users()->attach($id); - if($user && Carbon::now()->lessThanOrEqualTo($task->end)) { - - if ($user->web_notifications) { - $notification = Notification::create([ - 'content' => "Vous avez été assigné à la tâche {$task->name} de l'événement {$event->name}." - ]); - $notification->users()->attach($user->id); - - broadcast(new VolunteerAssignedToTask( - $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 VolunteerAssignToTaskMail($data)); - } + if (Carbon::now()->lessThanOrEqualTo($task->end)) { + $user->notify(new VolunteerAssignedToTask($task, $event)); } - return response()->json(['message' => "Task assigned"], 200); + return response()->json(['message' => 'Task assigned'], 200); - } catch(\Exception $e) { - Log::info($e->getMessage()); - return response()->json(['message' => "Server error"], 500); + } catch (\Exception $e) { + Log::error($e->getMessage()); + return response()->json(['message' => 'Server error'], 500); } }