config->getBool('has_page_notification', true); } public function visibleQuery(int $customerId): Builder { return AppNotification::query() ->where('is_active', true) ->where(function (Builder $q) use ($customerId) { $q->where('audience_type', AppNotificationAudienceService::TYPE_ALL) ->orWhere(function (Builder $inner) use ($customerId) { $inner->whereIn('audience_type', [ AppNotificationAudienceService::TYPE_SEGMENT, AppNotificationAudienceService::TYPE_CUSTOMERS, ])->whereHas('recipients', function (Builder $r) use ($customerId) { $r->where('customer_id', $customerId); }); }); }); } public function countForCustomer(int $customerId): int { if ($customerId <= 0 || ! $this->featureEnabled()) { return 0; } $visibleIds = $this->visibleQuery($customerId)->select('id'); $total = (clone $visibleIds)->count(); $read = AppNotificationRead::query() ->where('customer_id', $customerId) ->whereIn('notification_id', $visibleIds) ->count(); return max(0, $total - $read); } /** * @return array{unread_count: int} */ public function payloadForCustomer(int $customerId): array { return [ 'unread_count' => $this->countForCustomer($customerId), ]; } public function publishForCustomer(int $customerId): void { if ($customerId <= 0 || ! $this->featureEnabled() || ! $this->stream->isEnabled()) { return; } $this->stream->publishForCustomer( $customerId, 'unread-count', $this->payloadForCustomer($customerId) ); } }