diff --git a/src/app/Console/Commands/Wallet/ChargeCommand.php b/src/app/Console/Commands/Wallet/ChargeCommand.php index 5ec062d3..d885676f 100644 --- a/src/app/Console/Commands/Wallet/ChargeCommand.php +++ b/src/app/Console/Commands/Wallet/ChargeCommand.php @@ -1,57 +1,81 @@ argument('wallet')) { // Find specified wallet by ID $wallet = $this->getWallet($wallet); if (!$wallet) { $this->error("Wallet not found."); return 1; } if (!$wallet->owner) { $this->error("Wallet's owner is deleted."); return 1; } $wallets = [$wallet]; + } elseif ($this->option('topup')) { + // Find wallets that need to be topped up + $wallets = \App\Wallet::select('wallets.id') + ->join('users', 'users.id', '=', 'wallets.user_id') + ->join('wallet_settings', function (\Illuminate\Database\Query\JoinClause $join) { + $join->on('wallet_settings.wallet_id', '=', 'wallets.id') + ->where('wallet_settings.key', '=', 'mandate_balance'); + }) + ->whereNull('users.deleted_at') + ->whereRaw('wallets.balance < (wallet_settings.value * 100)') + ->whereNot( + 'users.status', + '&', + \App\User::STATUS_DEGRADED | \App\User::STATUS_SUSPENDED | \App\User::STATUS_DELETED + ) + ->cursor(); } else { // Get all wallets, excluding deleted accounts $wallets = \App\Wallet::select('wallets.id') ->join('users', 'users.id', '=', 'wallets.user_id') ->whereNull('users.deleted_at') ->cursor(); } foreach ($wallets as $wallet) { - \App\Jobs\WalletCheck::dispatch($wallet->id); + if ($this->option('dry-run')) { + $this->info($wallet->id); + } else { + if ($this->option('topup')) { + \App\Jobs\WalletCharge::dispatch($wallet->id); + } else { + \App\Jobs\WalletCheck::dispatch($wallet->id); + } + } } } }