use VolunteerUnassignedFromTask notification

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