subHours(6); $connections = BankingConnection::query() ->with(['user', 'accounts']) ->whereHas('user') ->where('provider', 'enablebanking') ->where('status', '!=', BankingConnectionStatus::Revoked) ->where('created_at', '<=', $cutoff) ->get(); $count = $connections->count(); if ($count === 0) { $this->info('No eligible Enable Banking connections found for free users.'); return Command::SUCCESS; } $revoked = 0; $skipped = 0; $connections ->groupBy('user_id') ->each(function ($userConnections) use ($disconnectBankingConnection, &$revoked, &$skipped): void { $user = $userConnections->first()?->user; if (! $user) { return; } if ($user->hasProPlan()) { $skipped += $userConnections->count(); return; } foreach ($userConnections as $connection) { $disconnectBankingConnection->handle($connection); $revoked++; } if ($user->canReceiveEmails()) { Mail::to($user)->send(new EnableBankingConnectionsCancelledEmail( $user, $userConnections->count(), )); } }); $this->info("Revoked {$revoked} Enable Banking connection(s). Skipped paid users: {$skipped}."); return Command::SUCCESS; } }