use VolunteerUnassignedFromTask notification

This commit is contained in:
2026-03-28 15:43:50 +01:00
parent 52c187dc2c
commit 4a0087104c
+15 -46
View File
@@ -5,10 +5,9 @@ namespace App\Http\Controllers;
use App\Notifications\TaskDateUpdated;
use App\Notifications\TaskDeleted;
use App\Notifications\VolunteerAssignedToTask;
use App\Events\VolunteerUnassignedFromTask;
use App\Notifications\VolunteerUnassignedFromTask;
use App\Models\Event;
use App\Models\Notification;
use App\Mail\VolunteerUnassignToTaskMail;
use App\Models\Task;
use App\Models\User;
use Carbon\Carbon;
@@ -16,7 +15,6 @@ 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
@@ -202,61 +200,32 @@ class TasksController extends Controller
}
}
public function unassignUser(Request $request, int $id): JsonResponse {
if(Gate::denies('unassignOther', Task::class)) {
return response()->json(["message" => "Unauthorized"], 403);
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);
$task = Task::findOrFail($request['task_id']);
$user = User::findOrFail($id);
if (!$task) {
return response()->json(["message" => "Task not found"], 404);
if (!$task->users()->where('user_id', $id)->exists()) {
return response()->json(['message' => 'User is not assigned to this task'], 400);
}
$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 (Carbon::now()->lessThanOrEqualTo($task->end)) {
$event = Event::findOrFail($task->events_id);
$user->notify(new VolunteerUnassignedFromTask($task, $event));
}
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);
return response()->json(['message' => "User unassigned successfully"], 200);
} catch(\Exception $e) {
} catch (\Exception $e) {
Log::error("UnassignUser error: " . $e->getMessage());
return response()->json(['message' => "Server error"], 500);
return response()->json(['message' => 'Server error'], 500);
}
}
}