check if user accept notifications

This commit is contained in:
2026-03-26 10:29:51 +01:00
parent 83ec6c9a2a
commit 1d9c6726b3
3 changed files with 208 additions and 183 deletions
+10 -3
View File
@@ -97,20 +97,26 @@ class EventsController extends Controller
return $task->users;
})->unique('id');
if (Carbon::now()->lessThanOrEqualTo($event->end)) {
$webParticipants = $participants->filter(fn($user) => $user->web_notifications);
if ($webParticipants->isNotEmpty()) {
$notification = Notification::create([
'content' => "L'événement {$event->name} a été supprimé. Vous n'y participez donc plus."
]);
$notification->users()->attach($participants->pluck('id'));
$notification->users()->attach($webParticipants->pluck('id'));
foreach ($participants as $user) {
foreach ($webParticipants as $user) {
broadcast(new EventParticipationCancelled(
$user->id,
$event,
$notification->id,
));
}
}
foreach ($participants as $user) {
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
@@ -121,6 +127,7 @@ class EventsController extends Controller
Mail::to($user->email)->send(new EventParticipationCancelledMail($data));
}
}
}
$event->delete();
return response()->json(["message" => "Event deleted and participants notified"]);
+25 -5
View File
@@ -43,7 +43,6 @@ class TasksController extends Controller
"max_participants" => $request["max_participants"],
]);
return response()->json(['message' => "Task created"], 200);
} catch(\Exception $e) {
@@ -98,6 +97,7 @@ class TasksController extends Controller
if($user && Carbon::now()->lessThanOrEqualTo($task->end)) {
if ($user->web_notifications) {
$notification = Notification::create([
'content' => "Vous avez été assigné à la tâche {$task->name} de l'événement {$event->name}."
]);
@@ -109,7 +109,9 @@ class TasksController extends Controller
$event,
$notification->id,
));
}
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
@@ -120,6 +122,7 @@ class TasksController extends Controller
];
Mail::to($user->email)->send(new VolunteerAssignToTaskMail($data));
}
}
return response()->json(['message' => "Task assigned"], 200);
@@ -141,7 +144,6 @@ class TasksController extends Controller
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
}
}
public function deleteTask(Request $request, int $id): JsonResponse {
@@ -161,20 +163,26 @@ class TasksController extends Controller
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($participants->pluck('id'));
}
$notification->users()->attach($webParticipants->pluck('id'));
foreach ($participants as $user) {
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,
@@ -186,6 +194,8 @@ class TasksController extends Controller
Mail::to($user->email)->send(new TaskParticipationCancelledMail($data));
}
}
}
}
$task->delete();
@@ -216,10 +226,12 @@ class TasksController extends Controller
$event = Event::find($task->events_id);
foreach ($participants as $user) {
if ($user->web_notifications) {
$notification = Notification::create([
'content' => "La date de la tâche {$task->name} de l'événement {$event->name} à été mis à jour. Cette tâche aura maintenant lieu du {$formatedTaskStart} au {$formatedTaskEnd}."
]);
$notification->users()->attach($user->id);
broadcast(new TaskDateUpdated(
$user->id,
$task,
@@ -228,7 +240,9 @@ class TasksController extends Controller
FormatDate::formatDate($request["end"]),
$notification->id,
));
}
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
@@ -242,6 +256,7 @@ class TasksController extends Controller
Mail::to($user->email)->send(new TaskDateUpdateMail($data));
}
}
}
$task->name = $request["name"];
$task->description = $request["description"];
@@ -301,17 +316,21 @@ class TasksController extends Controller
$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 ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
@@ -322,6 +341,7 @@ class TasksController extends Controller
];
Mail::to($user->email)->send(new VolunteerUnassignToTaskMail($data));
}
}
return response()->json(['message' => "User unassigned successfully"], 200);
+22 -24
View File
@@ -55,20 +55,21 @@ class UserController extends Controller
"email_notifications" => 1,
]);
$notification = Notification::create([
'content' => "Nouvelle demande d'inscription : {$user->name} {$user->lastname}"
]);
$admins = User::whereIn('role', [1, 2])->get();
$notification->users()->attach(
$admins->pluck('id')->toArray()
);
$webAdmins = $admins->filter(fn($admin) => $admin->web_notifications);
$notification->users()->attach($webAdmins->pluck('id')->toArray());
broadcast(new UserCreated(
$user,
$notification->id,
));
foreach ($admins as $admin) {
if ($admin->email_notifications) {
$data = [
'adminName' => $admin->name,
'adminLastname' => $admin->lastname,
@@ -77,6 +78,7 @@ class UserController extends Controller
];
Mail::to($admin->email)->send(new RegisterMail($data));
}
}
return response()->json(['message' => 'User created successfully']);
}
@@ -104,14 +106,17 @@ class UserController extends Controller
"validate" => 1,
"role" => 3,
"phone" => $phone->formatInternational(),
"web_notifications" => 1,
"email_notifications" => 1,
]);
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
];
Mail::to($user->email)->send(new CreateUserByAdminMail($data));
}
return response()->json([
'message' => 'User created successfully',
@@ -123,11 +128,13 @@ class UserController extends Controller
try {
$user = User::find($request->user()->id);
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
];
Mail::to($user->email)->send(new DeleteAccountMail($data));
}
$user->delete();
return response()->json(['message' => 'User deleted successfully']);
@@ -138,7 +145,6 @@ class UserController extends Controller
}
public function getUser(Request $request, int $id): JsonResponse {
try {
return response()->json(UserService::getData($id));
} catch (ModelNotFoundException $e) {
@@ -147,7 +153,6 @@ class UserController extends Controller
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
}
}
public function deleteById(Request $request, int $id): JsonResponse {
@@ -159,11 +164,13 @@ class UserController extends Controller
try {
$user = User::find($id);
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
];
Mail::to($user->email)->send(new DeleteAccountMail($data));
}
$user->delete();
return response()->json(['message' => 'User deleted successfully']);
@@ -183,11 +190,13 @@ class UserController extends Controller
try {
$userToDeactivate->update(['validate' => 0]);
if ($userToDeactivate->email_notifications) {
$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) {
@@ -196,13 +205,10 @@ class UserController extends Controller
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
}
}
public function update(Request $request): JsonResponse {
try {
$phone = new PhoneNumber($request["phone"], 'FR');
$user = User::find($request->user()->id);
@@ -212,12 +218,10 @@ class UserController extends Controller
$user->save();
return response()->json(['message' => 'User updated successfully']);
} catch(\Exception $e) {
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
}
}
public function validate(Request $request, int $id): JsonResponse {
@@ -234,14 +238,15 @@ class UserController extends Controller
$user->verified_at = now();
$user->save();
if ($user->email_notifications) {
$data = [
'name' => $user->name,
'lastname' => $user->lastname,
];
Mail::to($user->email)->send(new ValidateMail($data));
}
return response()->json(['message' => 'User validated successfully']);
} catch(\Exception $e) {
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
@@ -261,6 +266,7 @@ class UserController extends Controller
$role = GetRole::getRole($request['role']);
if ($user->web_notifications) {
$notification = Notification::create([
'content' => "Votre rôle à changé pour : {$role}",
]);
@@ -271,14 +277,16 @@ class UserController extends Controller
$role,
$notification->id,
));
}
if ($user->email_notifications) {
$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) {
@@ -288,16 +296,12 @@ class UserController extends Controller
}
public function getUsers(Request $request): JsonResponse {
try {
$users = User::where('validate', 1)
->orderBy('verified_at', 'desc')
->pluck('id');
return response()->json($users);
} catch(\Exception $e) {
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
@@ -305,7 +309,6 @@ class UserController extends Controller
}
public function getUserTasks(Request $request): JsonResponse {
try {
return response()->json(['data' => $request->user()->tasks]);
} catch (\Exception $e) {
@@ -315,7 +318,6 @@ class UserController extends Controller
}
public function getUserByIdTasks(Request $request, int $id): JsonResponse {
try {
return response()->json(['data' => User::find($id)->tasks]);
} catch (\Exception $e) {
@@ -325,7 +327,6 @@ class UserController extends Controller
}
public function getUserTaskById(Request $request, int $idTask): JsonResponse {
try {
return response()->json(['data' => $request->user()->tasks->find($idTask)]);
} catch (\Exception $e) {
@@ -340,11 +341,9 @@ class UserController extends Controller
}
try {
$users = User::where('validate', 0)->pluck('id');
return response()->json($users);
} catch (\Exception $e) {
Log::info($e->getMessage());
return response()->json(['message' => "Server error"], 500);
@@ -377,4 +376,3 @@ class UserController extends Controller
}
}
}