json(["message" => "Can't create task"], 403); } try { $event = Event::find($request["events_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::find($request["task_id"]); $event = Event::find($task->events_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 && Carbon::now()->lessThanOrEqualTo($task->end)) { $task->users()->attach($id); $notification = Notification::create([ 'content' => "Vous avez été assigné à la tâche {$task->name} de l'événement {$task->event->name}." ]); $notification->users()->attach($user->id); broadcast(new VolunteerAssignedToTask( $user->id, $task, $event )); $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)); } 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 = Event::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'])->find($id); $event = Event::find($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()) { $notification = Notification::create([ 'content' => "La tâche '{$task->name}' 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, $event )); $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)); } } $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); if(( $task->start !== $request["start"] || $task->end !== $request["end"] ) && Carbon::now()->lessThanOrEqualTo($task->end) ){ $participants = $task->users; $formatedTaskStart = FormatDate::formatDate($task->start); $formatedTaskEnd = FormatDate::formatDate($task->end); $event = Event::find($task->events_id); foreach ($participants as $user) { $notification = Notification::create([ 'content' => "La date de la tâche {$task->name} de l'événement {$task->event->name} à été mis à jour. Cette tâche aura maintenant lieu du {$formatedTaskStart} au {$formatedTaskEnd}." ]); $notification->users()->attach($user->id); broadcast(new TaskDateUpdated( $user->id, $task, )); $data = [ 'name' => $user->name, 'lastname' => $user->lastname, 'taskName' => $task->name, 'oldStart' => $formatedTaskStart, 'oldEnd' => $formatedTaskEnd, 'newStart' => $request["start"], 'newEnd' => $request["end"], 'eventName' => $event->name, ]; Mail::to($user->email)->send(new TaskDateUpdateMail($data)); } } $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::find($request["task_id"]); $event = Event::find($task->events_id); if (!$task) { return response()->json(["message" => "Task not found"], 404); } $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 && Carbon::now()->lessThanOrEqualTo($task->end)) { $task->users()->detach($id); $notification = Notification::create([ 'content' => "Vous avez été désassigné de la tâche {$task->name} de l'événement {$task->event->name}." ]); $notification->users()->attach($user->id); broadcast(new VolunteerUnassignedFromTask( $user->id, $task, $event )); $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)); } return response()->json(['message' => "User unassigned successfully"], 200); } catch(\Exception $e) { Log::error("UnassignUser error: " . $e->getMessage()); return response()->json(['message' => "Server error"], 500); } } }