diff --git a/src/app/Console/Commands/WalletCharge.php b/src/app/Console/Commands/WalletCharge.php index 2b6a8979..a599bf08 100644 --- a/src/app/Console/Commands/WalletCharge.php +++ b/src/app/Console/Commands/WalletCharge.php @@ -1,77 +1,77 @@ argument('wallet')) { // Find specified wallet by ID $wallet = Wallet::find($wallet); 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') + ->withEnvTenant('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 5e5d9dae..278e42e2 100644 --- a/src/app/Providers/AppServiceProvider.php +++ b/src/app/Providers/AppServiceProvider.php @@ -1,71 +1,88 @@ 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) { + // Query builder 'withEnvTenant' macro + Builder::macro('withEnvTenant', function (string $table = null) { + $tenant_id = \config('app.tenant_id'); + + if ($tenant_id) { + /** @var Builder $this */ + return $this->where(($table ? "$table." : '') . 'tenant_id', $tenant_id); + } + + /** @var Builder $this */ + return $this->whereNull(($table ? "$table." : '') . 'tenant_id'); + }); + + // Query builder 'withUserTenant' macro + Builder::macro('withUserTenant', function (string $table = null) { + $tenant_id = auth()->user()->tenant_id; + + if ($tenant_id) { + /** @var Builder $this */ + return $this->where(($table ? "$table." : '') . 'tenant_id', $tenant_id); + } + /** @var Builder $this */ - return $this->where( - ($table ? "$table." : '') . 'tenant_id', - \config('app.tenant_id') - ); + return $this->whereNull(($table ? "$table." : '') . 'tenant_id'); }); } } diff --git a/src/phpstan.neon b/src/phpstan.neon index 0e57a457..519151bb 100644 --- a/src/phpstan.neon +++ b/src/phpstan.neon @@ -1,15 +1,16 @@ 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 Illuminate\\Database\\Eloquent\\Builder[^:]*::withEnvTenant\(\)#' + - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder[^:]*::withUserTenant\(\)#' - '#Call to an undefined method Tests\\Browser::#' level: 4 paths: - app/ - tests/