use VolunteerAssignedToTask notification

This commit is contained in:
2026-03-28 15:34:36 +01:00
parent 0ffd8e8ad4
commit 8df5d54d14
+17 -43
View File
@@ -4,12 +4,11 @@ namespace App\Http\Controllers;
use App\Events\TaskDateUpdated; use App\Events\TaskDateUpdated;
use App\Events\TaskParticipationCancelled; use App\Events\TaskParticipationCancelled;
use App\Events\VolunteerAssignedToTask; use App\Notifications\VolunteerAssignedToTask;
use App\Events\VolunteerUnassignedFromTask; use App\Events\VolunteerUnassignedFromTask;
use App\Mail\TaskDateUpdateMail; use App\Mail\TaskDateUpdateMail;
use App\Models\Event; use App\Models\Event;
use App\Models\Notification; use App\Models\Notification;
use App\Mail\VolunteerAssignToTaskMail;
use App\Mail\VolunteerUnassignToTaskMail; use App\Mail\VolunteerUnassignToTaskMail;
use App\Models\Task; use App\Models\Task;
use App\Models\User; use App\Models\User;
@@ -74,61 +73,36 @@ class TasksController extends Controller
} }
} }
public function assignUser(request $request, int $id): JsonResponse { public function assignUser(Request $request, int $id): JsonResponse
{
if(Gate::denies('assignOther', Task::class)) { if (Gate::denies('assignOther', Task::class)) {
return response()->json(["message" => "Can't create task"], 403); return response()->json(['message' => "Can't create task"], 403);
} }
try { try {
$task = Task::find($request["task_id"]); $task = Task::findOrFail($request['task_id']);
$event = Event::find($task->events_id); $event = Event::findOrFail($task->events_id);
$user = User::findOrFail($id);
if ($task->users()->where('user_id', $id)->exists()) { if ($task->users()->where('user_id', $id)->exists()) {
return response()->json(["message" => "User already assigned to this task"], 400); return response()->json(['message' => 'User already assigned to this task'], 400);
} }
if($task->max_participants === $task->users()->count()) { if ($task->max_participants === $task->users()->count()) {
return response()->json(["message" => "Max participants reached"], 403); return response()->json(['message' => 'Max participants reached'], 403);
} }
$user = User::find($id);
$task->users()->attach($id); $task->users()->attach($id);
if($user && Carbon::now()->lessThanOrEqualTo($task->end)) { if (Carbon::now()->lessThanOrEqualTo($task->end)) {
$user->notify(new VolunteerAssignedToTask($task, $event));
if ($user->web_notifications) {
$notification = Notification::create([
'content' => "Vous avez été assigné à la tâche {$task->name} de l'événement {$event->name}."
]);
$notification->users()->attach($user->id);
broadcast(new VolunteerAssignedToTask(
$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 VolunteerAssignToTaskMail($data));
}
} }
return response()->json(['message' => "Task assigned"], 200); return response()->json(['message' => 'Task assigned'], 200);
} catch(\Exception $e) { } catch (\Exception $e) {
Log::info($e->getMessage()); Log::error($e->getMessage());
return response()->json(['message' => "Server error"], 500); return response()->json(['message' => 'Server error'], 500);
} }
} }