diff --git a/src/app/Console/Commands/WalletCharge.php b/src/app/Console/Commands/WalletCharge.php index a41b2333..2b6a8979 100644 --- a/src/app/Console/Commands/WalletCharge.php +++ b/src/app/Console/Commands/WalletCharge.php @@ -1,76 +1,77 @@ argument('wallet')) { // Find specified wallet by ID $wallet = Wallet::find($wallet); - if (!$wallet || !$wallet->owner) { + if (!$wallet || !$wallet->owner || $wallet->owner->tenant_id != \config('app.tenant_id')) { return 1; } $wallets = [$wallet]; } else { // Get all wallets, excluding deleted accounts $wallets = Wallet::select('wallets.*') ->join('users', 'users.id', '=', 'wallets.user_id') + ->withTenant('users') ->whereNull('users.deleted_at') ->get(); } foreach ($wallets as $wallet) { $charge = $wallet->chargeEntitlements(); if ($charge > 0) { $this->info( "Charged wallet {$wallet->id} for user {$wallet->owner->email} with {$charge}" ); // Top-up the wallet if auto-payment enabled for the wallet \App\Jobs\WalletCharge::dispatch($wallet); } if ($wallet->balance < 0) { // Check the account balance, send notifications, suspend, delete \App\Jobs\WalletCheck::dispatch($wallet); } } } } diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php index 40437600..5e5d9dae 100644 --- a/src/app/Providers/AppServiceProvider.php +++ b/src/app/Providers/AppServiceProvider.php @@ -1,61 +1,71 @@ sql, implode(', ', $query->bindings))); }); } // Register some template helpers Blade::directive('theme_asset', function ($path) { $path = trim($path, '/\'"'); return ""; }); + + // Query builder 'withTenant' macro + Builder::macro('withTenant', function (string $table = null) { + /** @var Builder $this */ + return $this->where( + ($table ? "$table." : '') . 'tenant_id', + \config('app.tenant_id') + ); + }); } } diff --git a/src/phpstan.neon b/src/phpstan.neon index de3d7e1c..0e57a457 100644 --- a/src/phpstan.neon +++ b/src/phpstan.neon @@ -1,14 +1,15 @@ includes: - ./vendor/nunomaduro/larastan/extension.neon parameters: ignoreErrors: - '#Access to an undefined property Illuminate\\Contracts\\Auth\\Authenticatable#' - '#Access to an undefined property [a-zA-Z\\]+::\$pivot#' - '#Access to an undefined property Illuminate\\Database\\Eloquent\\Model::\$id#' - '#Access to an undefined property Illuminate\\Database\\Eloquent\\Model::\$created_at#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Model::toString\(\)#' + - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder[^:]*::withTenant\(\)#' - '#Call to an undefined method Tests\\Browser::#' level: 4 paths: - app/ - tests/