Merge branch dev into features/notifications
This commit is contained in:
@@ -5,11 +5,14 @@ namespace App\Http\Controllers;
|
||||
use App\Events\EventParticipationCancelled;
|
||||
use App\Models\Event;
|
||||
use App\Models\Notification;
|
||||
use App\Mail\EventParticipationCancelledMail;
|
||||
use App\Models\Task;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Carbon\Carbon;
|
||||
|
||||
|
||||
class EventsController extends Controller
|
||||
@@ -96,22 +99,32 @@ class EventsController extends Controller
|
||||
return $task->users;
|
||||
})->unique('id');
|
||||
|
||||
$notification = Notification::create([
|
||||
'content' => "L'événement '{$event->name}' a été supprimé. Vous n'y participez donc plus."
|
||||
]);
|
||||
$notification->users()->attach($participants->pluck('id'));
|
||||
|
||||
foreach ($participants as $user) {
|
||||
broadcast(new EventParticipationCancelled(
|
||||
$user->id,
|
||||
$event
|
||||
));
|
||||
if (Carbon::now()->lessThanOrEqualTo($event->end)) {
|
||||
$notification = Notification::create([
|
||||
'content' => "L'événement '{$event->name}' a été supprimé. Vous n'y participez donc plus."
|
||||
]);
|
||||
$notification->users()->attach($participants->pluck('id'));
|
||||
|
||||
foreach ($participants as $user) {
|
||||
broadcast(new EventParticipationCancelled(
|
||||
$user->id,
|
||||
$event
|
||||
));
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'eventName' => $event->name,
|
||||
'start' => $event->start,
|
||||
'end' => $event->end,
|
||||
];
|
||||
Mail::to($user->email)->send(new EventParticipationCancelledMail($data));
|
||||
}
|
||||
}
|
||||
|
||||
$event->delete();
|
||||
|
||||
return response()->json(["message" => "Event deleted and participants notified"]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Event delete error: " . $e->getMessage());
|
||||
return response()->json(['message' => "Server error"], 500);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\UserCreated;
|
||||
use App\Mail\CreateUserByAdminMail;
|
||||
use App\Mail\DeactivateAccountMail;
|
||||
use App\Mail\DeleteAccountMail;
|
||||
use App\Mail\RegisterMail;
|
||||
use App\Mail\UpdateRoleMail;
|
||||
use App\Mail\ValidateMail;
|
||||
use App\Models\Notification;
|
||||
use App\Models\User;
|
||||
@@ -54,9 +59,18 @@ class UserController extends Controller
|
||||
$notification->users()->attach(
|
||||
$admins->pluck('id')->toArray()
|
||||
);
|
||||
|
||||
broadcast(new UserCreated($user));
|
||||
|
||||
foreach ($admins as $admin) {
|
||||
$data = [
|
||||
'adminName' => $admin->name,
|
||||
'adminLastname' => $admin->lastname,
|
||||
'userName' => $user->name,
|
||||
'userLastname' => $user->lastname,
|
||||
];
|
||||
Mail::to($admin->email)->send(new RegisterMail($data));
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'User created successfully']);
|
||||
}
|
||||
|
||||
@@ -85,6 +99,13 @@ class UserController extends Controller
|
||||
"phone" => $phone->formatInternational(),
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
];
|
||||
Mail::to($user->email)->send(new CreateUserByAdminMail($data));
|
||||
|
||||
|
||||
return response()->json([
|
||||
'message' => 'User created successfully',
|
||||
"password" => $password,
|
||||
@@ -94,6 +115,13 @@ class UserController extends Controller
|
||||
public function delete(Request $request): JsonResponse {
|
||||
try {
|
||||
$user = User::find($request->user()->id);
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
];
|
||||
Mail::to($user->email)->send(new DeleteAccountMail($data));
|
||||
|
||||
$user->delete();
|
||||
return response()->json(['message' => 'User deleted successfully']);
|
||||
} catch(\Exception $e) {
|
||||
@@ -124,6 +152,12 @@ class UserController extends Controller
|
||||
try {
|
||||
$user = User::find($id);
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
];
|
||||
Mail::to($user->email)->send(new DeleteAccountMail($data));
|
||||
|
||||
$user->delete();
|
||||
return response()->json(['message' => 'User deleted successfully']);
|
||||
} catch(\Exception $e) {
|
||||
@@ -142,6 +176,12 @@ class UserController extends Controller
|
||||
try {
|
||||
$userToDeactivate->update(['validate' => 0]);
|
||||
|
||||
$data = [
|
||||
'name' => $userToDeactivate->name,
|
||||
'lastname' => $userToDeactivate->lastname,
|
||||
];
|
||||
Mail::to($userToDeactivate->email)->send(new DeactivateAccountMail($data));
|
||||
|
||||
return response()->json(['message' => 'User deactivated successfully']);
|
||||
} catch (ModelNotFoundException $e) {
|
||||
return response()->json(['message' => 'Utilisateur non trouvé'], 404);
|
||||
@@ -181,12 +221,12 @@ class UserController extends Controller
|
||||
|
||||
try {
|
||||
$user = User::find($id);
|
||||
|
||||
$user->validate = 1;
|
||||
$user->role = 3;
|
||||
$user->verified_at = now();
|
||||
$user->save();
|
||||
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
@@ -213,11 +253,20 @@ class UserController extends Controller
|
||||
$user->save();
|
||||
|
||||
$role = \App\Services\GetRole::getRole($request['role']);
|
||||
|
||||
$notification = Notification::create([
|
||||
'content' => "Votre rôle à changé pour {$role} !",
|
||||
]);
|
||||
$notification->users()->attach($user->id);
|
||||
|
||||
$data = [
|
||||
'name' => $user->name,
|
||||
'lastname' => $user->lastname,
|
||||
'role' => $role,
|
||||
];
|
||||
Mail::to($user->email)->send(new UpdateRoleMail($data));
|
||||
|
||||
|
||||
return response()->json(['message' => 'User role added successfully']);
|
||||
} catch(\Exception $e) {
|
||||
Log::info($e->getMessage());
|
||||
|
||||
Reference in New Issue
Block a user