getBool('has_page_notification', false)) { throw new NotFoundHttpException('اعلان داخل اپ غیرفعال است.'); } } public function index() { $this->assertFeatureEnabled(); $customerId = (int) auth()->id(); $rows = AppNotification::query() ->where('is_active', true) ->orderByDesc('id') ->limit(100) ->get(['id', 'title', 'body', 'severity', 'created_at']); $readIds = AppNotificationRead::query() ->where('customer_id', $customerId) ->whereIn('notification_id', $rows->pluck('id')) ->pluck('notification_id') ->all(); $readSet = array_fill_keys($readIds, true); $payload = $rows->map(function (AppNotification $row) use ($readSet) { return [ 'id' => $row->id, 'title' => $row->title, 'body' => $row->body, 'severity' => $row->severity, 'created_at' => $row->created_at, 'is_read' => isset($readSet[$row->id]), ]; }); return response()->json($payload); } public function unreadCount() { $this->assertFeatureEnabled(); $customerId = (int) auth()->id(); $total = AppNotification::query()->where('is_active', true)->count(); $read = AppNotificationRead::query() ->where('customer_id', $customerId) ->whereIn( 'notification_id', AppNotification::query()->where('is_active', true)->select('id') ) ->count(); return response()->json([ 'unread_count' => max(0, $total - $read), ]); } public function markRead(Request $request, int $id) { $this->assertFeatureEnabled(); $customerId = (int) auth()->id(); $exists = AppNotification::query() ->where('id', $id) ->where('is_active', true) ->exists(); if (! $exists) { throw new NotFoundHttpException('اعلان یافت نشد.'); } AppNotificationRead::query()->updateOrCreate( [ 'notification_id' => $id, 'customer_id' => $customerId, ], [ 'read_at' => now(), ] ); return response()->json(true); } public function markAllRead() { $this->assertFeatureEnabled(); $customerId = (int) auth()->id(); $ids = AppNotification::query() ->where('is_active', true) ->pluck('id'); $now = now(); foreach ($ids as $notificationId) { AppNotificationRead::query()->updateOrCreate( [ 'notification_id' => $notificationId, 'customer_id' => $customerId, ], [ 'read_at' => $now, ] ); } return response()->json(true); } }