delete notification and notification_user when validate

This commit is contained in:
Giovanni committed 2025-12-16 10:11:08 +01:00
1 parent eab9565c7d
commit 8e93a0aba2
7 files changed
+51 -9

No files matched your search

@@ -10,7 +10,7 @@ use Illuminate\Support\Facades\Log;
class NotificationsController extends Controller
{
public function create(Request $request, int $id): JsonResponse {
public function createNotification(Request $request, int $id): JsonResponse {
$notification = Notification::create([
"content" => $request["content"]
]);
@@ -23,7 +23,6 @@ class NotificationsController extends Controller
public function getNotifications(Request $request, int $id): JsonResponse {
try {
$user = User::findOrFail($id);
@@ -35,4 +34,24 @@ class NotificationsController extends Controller
return response()->json(['message' => "Server error"], 500);
}
}
public function deleteNotification(int $userId, int $notificationId): JsonResponse {
try {
$user = User::findOrFail($userId);
$user->notifications()->detach($notificationId);
$notification = Notification::find($notificationId);
if ($notification && $notification->users()->count() === 0) {
$notification->delete();
}
return response()->json(['message' => 'Notification supprimée avec succès']);
} catch (\Exception $e) {
Log::info($e->getMessage());
return response()->json(['message' => 'Erreur lors de la suppression'], 500);
}
}
}
+8 -1
View File
@@ -120,12 +120,19 @@ class UserController extends Controller
public function validate(Request $request, int $id): JsonResponse {
if($request->user()->role != 1 || $request->user()->role == null){
if($request->user()->role != 1 && $request->user()->role != 2 || $request->user()->role == null){
return response()->json(["message" => "Can't validate this user"], 403);
}
try {
$user = User::find($id);
$content = "Nouvelle demande d'inscription : {$user->name} {$user->lastname}";
$notification = Notification::where('content', $content)->first();
if ($notification) {
$notification->users()->detach();
$notification->delete();
}
$user->validate = 1;
$user->role = 3;
+6 -1
View File
@@ -12,6 +12,11 @@ class Notification extends Model
public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(User::class, 'notification_user', 'notification_id', 'user_id');
return $this->belongsToMany(
User::class,
'notification_user',
'notification_id',
'user_id'
)->withTimestamps();
}
}
+6 -1
View File
@@ -57,6 +57,11 @@ class User extends Authenticatable
public function notifications(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Notification::class, 'notification_user', 'user_id', 'notification_id');
return $this->belongsToMany(
Notification::class,
'notification_user',
'user_id',
'notification_id'
)->withTimestamps();
}
}