Files
SAE-BUT2-backend/app/Http/Controllers/TasksController.php
T
2026-03-28 15:37:14 +01:00

290 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Events\TaskDateUpdated;
use App\Notifications\TaskDeleted;
use App\Notifications\VolunteerAssignedToTask;
use App\Events\VolunteerUnassignedFromTask;
use App\Mail\TaskDateUpdateMail;
use App\Models\Event;
use App\Models\Notification;
use App\Mail\VolunteerUnassignToTaskMail;
use App\Models\Task;
use App\Models\User;
use Carbon\Carbon;
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
{
public function create(Request $request): JsonResponse {
if(Gate::denies('create', Task::class)) {
return response()->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::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) {
if ($user->web_notifications) {
$notification = Notification::create([
'content' => "La date de la tâche {$task->name} de l'événement {$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,
$event,
FormatDate::formatDate($request["start"]),
FormatDate::formatDate($request["end"]),
$notification->id,
));
}
if ($user->email_notifications) {
$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);
$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);
}
}
}