json(["message" => "Can't create task"], 403); } try { $event = Event::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::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); } if ($task->max_participants === $task->users()->count()) { return response()->json(['message' => 'Max participants reached'], 403); } $task->users()->attach($id); if (Carbon::now()->lessThanOrEqualTo($task->end)) { $user->notify(new VolunteerAssignedToTask($task, $event)); } return response()->json(['message' => 'Task assigned'], 200); } catch (\Exception $e) { Log::error($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')->findOrFail($id); $event = Event::findOrFail($task->events_id); 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); } 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::with('users')->findOrFail($id); $datesChanged = $task->start !== $request['start'] || $task->end !== $request['end']; if ($datesChanged && Carbon::now()->lessThanOrEqualTo($task->end)) { $event = Event::findOrFail($task->events_id); Notification::send($task->users, new TaskDateUpdated( task: $task, event: $event, oldStart: FormatDate::formatDate($task->start), oldEnd: FormatDate::formatDate($task->end), newStart: FormatDate::formatDate($request['start']), newEnd: FormatDate::formatDate($request['end']), )); } $task->update([ '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 updated'], 200); } catch (\Exception $e) { Log::error($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); $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)); } } return response()->json(['message' => "User unassigned successfully"], 200); } catch(\Exception $e) { Log::error("UnassignUser error: " . $e->getMessage()); return response()->json(['message' => "Server error"], 500); } } }