all() as $notification) { $users = $notification->eligibleUsers(); if ($users->isEmpty()) { continue; } $filtered = $this->applyCooldowns($users, $notification); if ($filtered->isEmpty()) { continue; } $this->pushService->sendToUsers($filtered, $notification); } } /** * Remove da coleção os usuários que ainda estão em cooldown * (tanto de categoria quanto de notificação específica). */ private function applyCooldowns(Collection $users, BasePushNotification $notification): Collection { $userIds = $users->pluck('id'); $blockedByCategory = collect(); if ($notification->categoryCooldownDays() > 0) { $blockedByCategory = PushNotificationLog::whereIn('user_id', $userIds) ->where('target', $notification->target()->value) ->where('category', $notification->category()->value) ->where('sent_at', '>=', now()->subDays($notification->categoryCooldownDays())) ->pluck('user_id') ->unique(); } $blockedByNotification = PushNotificationLog::whereIn('user_id', $userIds) ->where('label', $notification->label()) ->where('sent_at', '>=', now()->subDays($notification->notificationCooldownDays())) ->pluck('user_id') ->unique(); $blocked = $blockedByCategory->merge($blockedByNotification)->unique(); return $users->whereNotIn('id', $blocked)->values(); } }