use TaskDeleted notification

This commit is contained in:
2026-03-28 15:37:14 +01:00
parent 8c8fb946e1
commit 3789a2107a
+12 -50
View File
@@ -3,7 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Events\TaskDateUpdated; use App\Events\TaskDateUpdated;
use App\Events\TaskParticipationCancelled; use App\Notifications\TaskDeleted;
use App\Notifications\VolunteerAssignedToTask; use App\Notifications\VolunteerAssignedToTask;
use App\Events\VolunteerUnassignedFromTask; use App\Events\VolunteerUnassignedFromTask;
use App\Mail\TaskDateUpdateMail; use App\Mail\TaskDateUpdateMail;
@@ -18,7 +18,6 @@ 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 Illuminate\Support\Facades\Mail;
use App\Mail\TaskParticipationCancelledMail;
use App\Services\FormatDate; use App\Services\FormatDate;
class TasksController extends Controller class TasksController extends Controller
@@ -120,64 +119,27 @@ class TasksController extends Controller
} }
} }
public function deleteTask(Request $request, int $id): JsonResponse { public function deleteTask(Request $request, int $id): JsonResponse
if(Gate::denies('delete', Task::class)) { {
return response()->json(["message" => "Can't delete task"], 403); if (Gate::denies('delete', Task::class)) {
return response()->json(['message' => "Can't delete task"], 403);
} }
try { try {
$task = Task::with(['users'])->find($id); $task = Task::with('users')->findOrFail($id);
$event = Event::find($task->events_id); $event = Event::findOrFail($task->events_id);
if (!$task) { if (Carbon::now()->lessThanOrEqualTo($task->end) && $task->users->isNotEmpty()) {
return response()->json(["message" => "Task not found"], 404); Notification::send($task->users, new TaskDeleted($task, $event));
}
$participants = $task->users;
if (Carbon::now()->lessThanOrEqualTo($task->end)) {
if ($participants->isNotEmpty()) {
$webParticipants = $participants->filter(fn($user) => $user->web_notifications);
if ($webParticipants->isNotEmpty()) {
$notification = Notification::create([
'content' => "La tâche {$task->name} pour l'événement {$event->name} a été supprimée. Vous n'y participez donc plus."
]);
$notification->users()->attach($webParticipants->pluck('id'));
foreach ($webParticipants as $user) {
broadcast(new TaskParticipationCancelled(
$user->id,
$task,
$event,
$notification->id,
));
}
}
foreach ($participants as $user) {
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
'taskName' => $task->name,
'start' => $event->start,
'end' => $task->end,
'eventName' => $event->name,
];
Mail::to($user->email)->send(new TaskParticipationCancelledMail($data));
}
}
}
} }
$task->delete(); $task->delete();
return response()->json(['message' => "Task deleted and participants notified"], 200); return response()->json(['message' => 'Task deleted and participants notified'], 200);
} catch(\Exception $e) { } catch (\Exception $e) {
Log::error($e->getMessage()); Log::error($e->getMessage());
return response()->json(['message' => "Server error"], 500); return response()->json(['message' => 'Server error'], 500);
} }
} }