Merge branch dev into features/notifications
This commit is contained in:
@@ -7,12 +7,17 @@ use App\Events\VolunteerAssignedToTask;
|
||||
use App\Events\VolunteerUnassignedFromTask;
|
||||
use App\Models\Event;
|
||||
use App\Models\Notification;
|
||||
use App\Mail\VolunteerAssignToTaskMail;
|
||||
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\Mail\TaskParticipationCancelledMail;
|
||||
|
||||
class TasksController extends Controller
|
||||
{
|
||||
@@ -74,8 +79,8 @@ class TasksController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$task = Task::with('event')->find($request["task_id"]);
|
||||
$task = Task::find($request["task_id"]);
|
||||
$event = \App\Models\Event::find($task->events_id);
|
||||
|
||||
if ($task->users()->where('user_id', $id)->exists()) {
|
||||
return response()->json(["message" => "User already assigned to this task"], 400);
|
||||
@@ -87,17 +92,28 @@ class TasksController extends Controller
|
||||
|
||||
$user = User::find($id);
|
||||
|
||||
if($user) {
|
||||
if($user && Carbon::now()->lessThanOrEqualTo($task->end)) {
|
||||
$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
|
||||
$event
|
||||
));
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'taskName' => $task->name,
|
||||
'start' => $task->start,
|
||||
'end' => $task->end,
|
||||
];
|
||||
Mail::to($user->email)->send(new VolunteerAssignToTaskMail($data));
|
||||
}
|
||||
|
||||
return response()->json(['message' => "Task assigned"], 200);
|
||||
@@ -129,26 +145,39 @@ class TasksController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
$task = Task::with(['users', 'event'])->find($id);
|
||||
$task = Task::with(['users'])->find($id);
|
||||
$event = \App\Models\Event::find($task->events_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
|
||||
));
|
||||
if (Carbon::now()->lessThanOrEqualTo($task->end)) {
|
||||
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,
|
||||
$event
|
||||
));
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'taskName' => $task->name,
|
||||
'start' => $event->start,
|
||||
'end' => $task->end,
|
||||
];
|
||||
Mail::to($user->email)->send(new TaskParticipationCancelledMail($data));
|
||||
}
|
||||
}
|
||||
|
||||
$task->delete();
|
||||
@@ -186,10 +215,6 @@ class TasksController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function unassignSelf(Request $request): JsonResponse {
|
||||
try {
|
||||
$task = Task::find($request["task_id"]);
|
||||
@@ -209,8 +234,6 @@ class TasksController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function unassignUser(Request $request, int $id): JsonResponse {
|
||||
|
||||
if(Gate::denies('unassignOther', Task::class)) {
|
||||
@@ -218,17 +241,13 @@ class TasksController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
$task = Task::with('event')->find($request["task_id"]);
|
||||
$task = Task::find($request["task_id"]);
|
||||
$event = \App\Models\Event::find($task->events_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);
|
||||
@@ -236,7 +255,7 @@ class TasksController extends Controller
|
||||
|
||||
$user = User::find($id);
|
||||
|
||||
if ($user) {
|
||||
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->title} de l'événement {$task->event->name}."
|
||||
@@ -245,8 +264,17 @@ class TasksController extends Controller
|
||||
broadcast(new VolunteerUnassignedFromTask(
|
||||
$user->id,
|
||||
$task,
|
||||
$task->event
|
||||
$event
|
||||
));
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'taskName' => $task->name,
|
||||
'start' => $task->start,
|
||||
'end' => $task->end,
|
||||
];
|
||||
Mail::to($user->email)->send(new VolunteerUnassignToTaskMail($data));
|
||||
}
|
||||
|
||||
return response()->json(['message' => "User unassigned successfully"], 200);
|
||||
|
||||
Reference in New Issue
Block a user