json(["message" => "Can't create task"], 403); } try { $event = Events::find($request["event_id"]); $event->tasks()->create([ "name" => $request["name"], "description" => $request["description"], "start" => $request["start"], "end" => $request["end"], "location" => $request["location"], "max_participants" => $request["max_participants"], ]); return response()->json(['message' => "Task created"], 200); } catch(\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function assignSelf(Request $request): JsonResponse { try { $task = Task::findOrFail($request["task_id"]); if ($task->users()->count() >= $task->max_participants) { return response()->json(["message" => "Max participants reached"], 403); } if ($task->users()->where('users.id', $request->user()->id)->exists()) { return response()->json(["message" => "Already registered"], 409); } $task->users()->attach($request->user()->id); return response()->json(['message' => "Task assigned"], 200); } catch (\Throwable $e) { Log::error($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } 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::with('event')->find($request["task_id"]); if ($task->users()->where('user_id', $id)->exists()) { 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); } $user = User::find($id); if($user) { $task->users()->attach($id); $notification = Notification::create([ 'content' => "Vous avez été assigné à la tâche {$task->title} de l'événement {$task->event->name}." ]); $notification->users()->attach($user->id); broadcast(new VolunteerAssignedToTask( $user->id, $task, $task->event )); } return response()->json(['message' => "Task assigned"], 200); } catch(\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function getEventTasks(Request $request, int $id): JsonResponse { try { $event = Events::find($id); return response()->json(['data' => $event->tasks()->get()]); } catch(\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } 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', 'event'])->find($id); if (!$task) { return response()->json(["message" => "Task not found"], 404); } $participants = $task->users; if ($participants->isNotEmpty()) { $notification = Notification::create([ 'content' => "La tâche '{$task->title}' pour l'événement '{$task->event->name}' a été supprimée. Vous n'y participez donc plus." ]); $notification->users()->attach($participants->pluck('id')); } foreach ($participants as $user) { broadcast(new TaskParticipationCancelled( $user->id, $task, $task->event )); } $task->delete(); return response()->json(['message' => "Task deleted and participants notified"], 200); } catch(\Exception $e) { Log::error($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function update(Request $request, int $id): JsonResponse { if(Gate::denies('update', Task::class)) { return response()->json(["message" => "Forbidden"], 403); } try { $task = Task::find($id); $task->name = $request["name"]; $task->description = $request["description"]; $task->start = $request["start"]; $task->end = $request["end"]; $task->location = $request["location"]; $task->max_participants = $request["max_participants"]; $task->save(); return response()->json(['message' => "Task updated"], 200); } catch (\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function unassignSelf(Request $request): JsonResponse { try { $task = Task::find($request["task_id"]); $userId = $request->user()->id; if (!$task) { return response()->json(["message" => "Task not found"], 404); } $task->users()->detach($userId); return response()->json(['message' => "Successfully unassigned from task"], 200); } catch (\Exception $e) { Log::error("UnassignSelf error: " . $e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function unassignUser(Request $request, int $id): JsonResponse { if(Gate::denies('unassignOther', Task::class)) { return response()->json(["message" => "Unauthorized"], 403); } try { $task = Task::with('event')->find($request["task_id"]); if (!$task) { return response()->json(["message" => "Task not found"], 404); } if (!$task->event) { Log::error("Relation 'event' manquante pour la tâche ID: " . $task->id); return response()->json(["message" => "Task is missing its parent event"], 422); } $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); if ($user) { $task->users()->detach($id); $notification = Notification::create([ 'content' => "Vous avez été désassigné de la tâche {$task->title} de l'événement {$task->event->name}." ]); $notification->users()->attach($user->id); broadcast(new VolunteerUnassignedFromTask( $user->id, $task, $task->event )); } return response()->json(['message' => "User unassigned successfully"], 200); } catch(\Exception $e) { Log::error("UnassignUser error: " . $e->getMessage()); return response()->json(['message' => "Server error"], 500); } } }