diff --git a/src/app/Console/Command.php b/src/app/Console/Command.php index bc4eec93..89c6513c 100644 --- a/src/app/Console/Command.php +++ b/src/app/Console/Command.php @@ -1,148 +1,199 @@ getObject(\App\Domain::class, $domain, 'namespace'); + return $this->getObject(\App\Domain::class, $domain, 'namespace', $withDeleted); } /** * Find an object. * * @param string $objectClass The name of the class * @param string $objectIdOrTitle The name of a database field to match. * @param string|null $objectTitle An additional database field to match. + * @param bool $withDeleted Act as if --with-deleted was used * * @return mixed */ - public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null) + public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null, $withDeleted = false) { - if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { - $object = $objectClass::withTrashed()->find($objectIdOrTitle); - } else { - $object = $objectClass::find($objectIdOrTitle); + if (!$withDeleted) { + $withDeleted = $this->hasOption('with-deleted') && $this->option('with-deleted'); } + $object = $this->getObjectModel($objectClass, $withDeleted)->find($objectIdOrTitle); + if (!$object && !empty($objectTitle)) { - if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { - $object = $objectClass::withTrashed()->where($objectTitle, $objectIdOrTitle)->first(); - } else { - $object = $objectClass::where($objectTitle, $objectIdOrTitle)->first(); - } + $object = $this->getObjectModel($objectClass, $withDeleted) + ->where($objectTitle, $objectIdOrTitle)->first(); } return $object; } + /** + * Returns a preconfigured Model object for a specified class. + * + * @param string $objectClass The name of the class + * @param bool $withDeleted Include withTrashed() query + * + * @return mixed + */ + protected function getObjectModel($objectClass, $withDeleted = false) + { + if ($withDeleted) { + $model = $objectClass::withTrashed(); + } else { + $model = new $objectClass(); + } + + $modelsWithTenant = [ + \App\Discount::class, + \App\Domain::class, + \App\Group::class, + \App\Package::class, + \App\Plan::class, + \App\Sku::class, + \App\User::class, + ]; + + $modelsWithOwner = [ + \App\Wallet::class, + ]; + + $tenant_id = \config('app.tenant_id'); + + // Add tenant filter + if (in_array($objectClass, $modelsWithTenant)) { + $model = $model->withEnvTenant(); + } elseif (in_array($objectClass, $modelsWithOwner)) { + $model = $model->whereExists(function ($query) use ($tenant_id) { + $query->select(DB::raw(1)) + ->from('users') + ->whereRaw('wallets.user_id = users.id') + ->whereRaw('users.tenant_id ' . ($tenant_id ? "= $tenant_id" : 'is null')); + }); + } + + // TODO: tenant check for Entitlement, Transaction, etc. + + return $model; + } + /** * Find the user. * - * @param string $user User ID or email + * @param string $user User ID or email + * @param bool $withDeleted Include deleted * * @return \App\User|null */ - public function getUser($user) + public function getUser($user, $withDeleted = false) { - return $this->getObject(\App\User::class, $user, 'email'); + return $this->getObject(\App\User::class, $user, 'email', $withDeleted); } /** * Find the wallet. * * @param string $wallet Wallet ID * * @return \App\Wallet|null */ public function getWallet($wallet) { return $this->getObject(\App\Wallet::class, $wallet, null); } public function handle() { if ($this->dangerous) { $this->warn( "This command is a dangerous scalpel command with potentially significant unintended consequences" ); $confirmation = $this->confirm("Are you sure you understand what's about to happen?"); if (!$confirmation) { $this->info("Better safe than sorry."); return false; } $this->info("VĂ¡monos!"); } return true; } /** * Return a string for output, with any additional attributes specified as well. * * @param mixed $entry An object * * @return string */ protected function toString($entry) { /** * Haven't figured out yet, how to test if this command implements an option for additional * attributes. if (!in_array('attr', $this->options())) { return $entry->{$entry->getKeyName()}; } */ $str = [ $entry->{$entry->getKeyName()} ]; foreach ($this->option('attr') as $attr) { if ($attr == $entry->getKeyName()) { $this->warn("Specifying {$attr} is not useful."); continue; } if (!array_key_exists($attr, $entry->toArray())) { $this->error("Attribute {$attr} isn't available"); continue; } if (is_numeric($entry->{$attr})) { $str[] = $entry->{$attr}; } else { $str[] = !empty($entry->{$attr}) ? $entry->{$attr} : "null"; } } return implode(" ", $str); } } diff --git a/src/app/Console/Commands/Discount/MergeCommand.php b/src/app/Console/Commands/Discount/MergeCommand.php index a581cc16..561fc658 100644 --- a/src/app/Console/Commands/Discount/MergeCommand.php +++ b/src/app/Console/Commands/Discount/MergeCommand.php @@ -1,95 +1,85 @@ 158f660b-e992-4fb9-ac12-5173b5f33807 \ * > 62af659f-17d8-4527-87c1-c69eaa26653c \ * > --description="Employee discount" * ``` */ class MergeCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'discount:merge {source} {target} {--description*}'; /** * The console command description. * * @var string */ protected $description = 'Merge one discount in to another discount, ' . 'optionally set the description, and delete the source discount'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } - /** * Execute the console command. * * @return mixed */ public function handle() { - $source = \App\Discount::find($this->argument('source')); + $source = $this->getObject(\App\Discount::class, $this->argument('source')); if (!$source) { $this->error("No such source discount: {$source}"); return 1; } - $target = \App\Discount::find($this->argument('target')); + $target = $this->getObject(\App\Discount::class, $this->argument('target')); if (!$target) { $this->error("No such target discount: {$target}"); return 1; } if ($source->discount !== $target->discount) { $this->error("Can't merge two discounts that have different rates"); return 1; } foreach ($source->wallets as $wallet) { $wallet->discount_id = $target->id; $wallet->timestamps = false; $wallet->save(); } if ($this->option('description')) { $target->description = $this->option('description'); $target->save(); } $source->delete(); } } diff --git a/src/app/Console/Commands/DomainDelete.php b/src/app/Console/Commands/DomainDelete.php index a1d67a89..4f9af503 100644 --- a/src/app/Console/Commands/DomainDelete.php +++ b/src/app/Console/Commands/DomainDelete.php @@ -1,48 +1,38 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } $domain->delete(); } } diff --git a/src/app/Console/Commands/DomainList.php b/src/app/Console/Commands/DomainList.php index 4ca0ecf3..9356d45a 100644 --- a/src/app/Console/Commands/DomainList.php +++ b/src/app/Console/Commands/DomainList.php @@ -1,59 +1,49 @@ option('deleted')) { $domains = Domain::withTrashed()->orderBy('namespace'); } else { $domains = Domain::orderBy('namespace'); } - $domains->each( + $domains->withEnvTenant()->each( function ($domain) { $msg = $domain->namespace; if ($domain->deleted_at) { $msg .= " (deleted at {$domain->deleted_at})"; } $this->info($msg); } ); } } diff --git a/src/app/Console/Commands/DomainListUsers.php b/src/app/Console/Commands/DomainListUsers.php index 833dcd1b..717cee68 100644 --- a/src/app/Console/Commands/DomainListUsers.php +++ b/src/app/Console/Commands/DomainListUsers.php @@ -1,83 +1,83 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } if ($domain->isPublic()) { $this->error("This domain is a public registration domain."); return 1; } // TODO: actually implement listing users $wallet = $domain->wallet(); if (!$wallet) { $this->error("This domain isn't billed to a wallet."); return 1; } $mailboxSKU = \App\Sku::where('title', 'mailbox')->first(); if (!$mailboxSKU) { $this->error("No mailbox SKU available."); } $entitlements = $wallet->entitlements() ->where('entitleable_type', \App\User::class) ->where('sku_id', $mailboxSKU->id)->get(); $users = []; foreach ($entitlements as $entitlement) { $users[] = $entitlement->entitleable; } usort($users, function ($a, $b) { return $a->email > $b->email; }); foreach ($users as $user) { $this->info($user->email); } } } diff --git a/src/app/Console/Commands/DomainRestore.php b/src/app/Console/Commands/DomainRestore.php index c20a7ef4..79f39569 100644 --- a/src/app/Console/Commands/DomainRestore.php +++ b/src/app/Console/Commands/DomainRestore.php @@ -1,54 +1,54 @@ where('namespace', $this->argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain'), true); if (!$domain) { $this->error("Domain not found."); return 1; } if (!$domain->trashed()) { $this->error("The domain is not yet deleted."); return 1; } $wallet = $domain->wallet(); if ($wallet && !$wallet->owner) { $this->error("The domain owner is deleted."); return 1; } DB::beginTransaction(); $domain->restore(); DB::commit(); } } diff --git a/src/app/Console/Commands/DomainSetStatus.php b/src/app/Console/Commands/DomainSetStatus.php index c97078d9..b9013439 100644 --- a/src/app/Console/Commands/DomainSetStatus.php +++ b/src/app/Console/Commands/DomainSetStatus.php @@ -1,45 +1,45 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } Queue::fake(); // ignore LDAP for now $domain->status = (int) $this->argument('status'); $domain->save(); $this->info($domain->status); } } diff --git a/src/app/Console/Commands/DomainSetWallet.php b/src/app/Console/Commands/DomainSetWallet.php index 80ff84f2..c4111384 100644 --- a/src/app/Console/Commands/DomainSetWallet.php +++ b/src/app/Console/Commands/DomainSetWallet.php @@ -1,69 +1,69 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { $this->error("Domain not found."); return 1; } - $wallet = Wallet::find($this->argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { $this->error("Wallet not found."); return 1; } if ($domain->entitlement) { $this->error("Domain already assigned to a wallet: {$domain->entitlement->wallet->id}."); return 1; } $sku = Sku::where('title', 'domain-hosting')->first(); Queue::fake(); // ignore LDAP for now (note: adding entitlements updates the domain) Entitlement::create( [ 'wallet_id' => $wallet->id, 'sku_id' => $sku->id, 'cost' => 0, 'fee' => 0, 'entitleable_id' => $domain->id, 'entitleable_type' => Domain::class, ] ); } } diff --git a/src/app/Console/Commands/DomainStatus.php b/src/app/Console/Commands/DomainStatus.php index 80a36067..2197adf2 100644 --- a/src/app/Console/Commands/DomainStatus.php +++ b/src/app/Console/Commands/DomainStatus.php @@ -1,64 +1,54 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } $statuses = [ 'active' => Domain::STATUS_ACTIVE, 'suspended' => Domain::STATUS_SUSPENDED, 'deleted' => Domain::STATUS_DELETED, 'ldapReady' => Domain::STATUS_LDAP_READY, 'verified' => Domain::STATUS_VERIFIED, 'confirmed' => Domain::STATUS_CONFIRMED, ]; foreach ($statuses as $text => $bit) { $func = 'is' . \ucfirst($text); $this->info(sprintf("%d %s: %s", $bit, $text, $domain->$func())); } $this->info("In total: {$domain->status}"); } } diff --git a/src/app/Console/Commands/DomainSuspend.php b/src/app/Console/Commands/DomainSuspend.php index 681dd20e..85c3c7db 100644 --- a/src/app/Console/Commands/DomainSuspend.php +++ b/src/app/Console/Commands/DomainSuspend.php @@ -1,51 +1,41 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } $this->info("Found domain: {$domain->id}"); $domain->suspend(); } } diff --git a/src/app/Console/Commands/DomainUnsuspend.php b/src/app/Console/Commands/DomainUnsuspend.php index ba92a818..0a17b547 100644 --- a/src/app/Console/Commands/DomainUnsuspend.php +++ b/src/app/Console/Commands/DomainUnsuspend.php @@ -1,51 +1,41 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } $this->info("Found domain {$domain->id}"); $domain->unsuspend(); } } diff --git a/src/app/Console/Commands/Job/DomainCreate.php b/src/app/Console/Commands/Job/DomainCreate.php index 812feef2..9c6f5716 100644 --- a/src/app/Console/Commands/Job/DomainCreate.php +++ b/src/app/Console/Commands/Job/DomainCreate.php @@ -1,40 +1,40 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } $job = new \App\Jobs\Domain\CreateJob($domain->id); $job->handle(); } } diff --git a/src/app/Console/Commands/Job/DomainUpdate.php b/src/app/Console/Commands/Job/DomainUpdate.php index 35f92570..4b207e42 100644 --- a/src/app/Console/Commands/Job/DomainUpdate.php +++ b/src/app/Console/Commands/Job/DomainUpdate.php @@ -1,40 +1,40 @@ argument('domain'))->first(); + $domain = $this->getDomain($this->argument('domain')); if (!$domain) { return 1; } $job = new \App\Jobs\Domain\UpdateJob($domain->id); $job->handle(); } } diff --git a/src/app/Console/Commands/Job/UserCreate.php b/src/app/Console/Commands/Job/UserCreate.php index ce40e45b..d6c4f750 100644 --- a/src/app/Console/Commands/Job/UserCreate.php +++ b/src/app/Console/Commands/Job/UserCreate.php @@ -1,40 +1,40 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $job = new \App\Jobs\User\CreateJob($user->id); $job->handle(); } } diff --git a/src/app/Console/Commands/Job/UserUpdate.php b/src/app/Console/Commands/Job/UserUpdate.php index b0adf1b4..2b8fe851 100644 --- a/src/app/Console/Commands/Job/UserUpdate.php +++ b/src/app/Console/Commands/Job/UserUpdate.php @@ -1,40 +1,40 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $job = new \App\Jobs\User\UpdateJob($user->id); $job->handle(); } } diff --git a/src/app/Console/Commands/Job/WalletCheck.php b/src/app/Console/Commands/Job/WalletCheck.php index 5b80f707..f24c8d5e 100644 --- a/src/app/Console/Commands/Job/WalletCheck.php +++ b/src/app/Console/Commands/Job/WalletCheck.php @@ -1,40 +1,40 @@ argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } $job = new \App\Jobs\WalletCheck($wallet); $job->handle(); } } diff --git a/src/app/Console/Commands/MollieInfo.php b/src/app/Console/Commands/MollieInfo.php index 098ecd3a..f3e013a2 100644 --- a/src/app/Console/Commands/MollieInfo.php +++ b/src/app/Console/Commands/MollieInfo.php @@ -1,89 +1,89 @@ argument('user')) { - $user = User::where('email', $this->argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $this->info("Found user: {$user->id}"); $wallet = $user->wallets->first(); $provider = new \App\Providers\Payment\Mollie(); if ($mandate = $provider->getMandate($wallet)) { $amount = $wallet->getSetting('mandate_amount'); $balance = $wallet->getSetting('mandate_balance') ?: 0; $status = 'invalid'; if ($mandate['isPending']) { $status = 'pending'; } elseif ($mandate['isValid']) { $status = 'valid'; } if ($wallet->getSetting('mandate_disabled')) { $status .= ' (disabled)'; } $this->info("Auto-payment: {$mandate['method']}"); $this->info(" id: {$mandate['id']}"); $this->info(" status: {$status}"); $this->info(" amount: {$amount} {$wallet->currency}"); $this->info(" min-balance: {$balance} {$wallet->currency}"); } else { $this->info("Auto-payment: none"); } // TODO: List user payments history } else { $this->info("Available payment methods:"); foreach (mollie()->methods()->all() as $method) { $this->info("- {$method->description} ({$method->id}):"); $this->info(" status: {$method->status}"); $this->info(sprintf( " min: %s %s", $method->minimumAmount->value, $method->minimumAmount->currency )); if (!empty($method->maximumAmount)) { $this->info(sprintf( " max: %s %s", $method->maximumAmount->value, $method->maximumAmount->currency )); } } } } } diff --git a/src/app/Console/Commands/OpenVidu/RoomCreate.php b/src/app/Console/Commands/OpenVidu/RoomCreate.php index b9343780..a88b5578 100644 --- a/src/app/Console/Commands/OpenVidu/RoomCreate.php +++ b/src/app/Console/Commands/OpenVidu/RoomCreate.php @@ -1,67 +1,57 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $roomName = $this->argument('room'); if (!preg_match('/^[a-zA-Z0-9_-]{1,16}$/', $roomName)) { $this->error("Invalid room name. Should be up to 16 characters ([a-zA-Z0-9_-])."); return 1; } $room = \App\OpenVidu\Room::where('name', $roomName)->first(); if ($room) { $this->error("Room already exists."); return 1; } \App\OpenVidu\Room::create( [ 'name' => $roomName, 'user_id' => $user->id ] ); } } diff --git a/src/app/Console/Commands/OpenVidu/Rooms.php b/src/app/Console/Commands/OpenVidu/Rooms.php index 77beabc0..597c553f 100644 --- a/src/app/Console/Commands/OpenVidu/Rooms.php +++ b/src/app/Console/Commands/OpenVidu/Rooms.php @@ -1,46 +1,36 @@ info("{$room->name}"); } } } diff --git a/src/app/Console/Commands/OpenVidu/Sessions.php b/src/app/Console/Commands/OpenVidu/Sessions.php index 6b6d8d8f..8894ec61 100644 --- a/src/app/Console/Commands/OpenVidu/Sessions.php +++ b/src/app/Console/Commands/OpenVidu/Sessions.php @@ -1,82 +1,72 @@ \config('openvidu.api_url'), 'verify' => \config('openvidu.api_verify_tls') ] ); $response = $client->request( 'GET', 'sessions', ['auth' => [\config('openvidu.api_username'), \config('openvidu.api_password')]] ); if ($response->getStatusCode() !== 200) { return 1; } $sessionResponse = json_decode($response->getBody(), true); foreach ($sessionResponse['content'] as $session) { $room = \App\OpenVidu\Room::where('session_id', $session['sessionId'])->first(); if ($room) { $owner = $room->owner->email; $roomName = $room->name; } else { $owner = '(none)'; $roomName = '(none)'; } $this->info( sprintf( "Session: %s for %s since %s (by %s)", $session['sessionId'], $roomName, \Carbon\Carbon::parse((int)substr($session['createdAt'], 0, 10), 'UTC'), $owner ) ); } } } diff --git a/src/app/Console/Commands/PackageSkus.php b/src/app/Console/Commands/PackageSkus.php index d04ad369..26ea06bb 100644 --- a/src/app/Console/Commands/PackageSkus.php +++ b/src/app/Console/Commands/PackageSkus.php @@ -1,51 +1,41 @@ get(); foreach ($packages as $package) { $this->info(sprintf("Package: %s", $package->title)); foreach ($package->skus as $sku) { $this->info(sprintf(" SKU: %s (%d)", $sku->title, $sku->pivot->qty)); } } } } diff --git a/src/app/Console/Commands/PlanPackages.php b/src/app/Console/Commands/PlanPackages.php index e406d5f3..b827c4d1 100644 --- a/src/app/Console/Commands/PlanPackages.php +++ b/src/app/Console/Commands/PlanPackages.php @@ -1,87 +1,87 @@ get(); foreach ($plans as $plan) { $this->info(sprintf("Plan: %s", $plan->title)); $plan_costs = 0; foreach ($plan->packages as $package) { $qtyMin = $package->pivot->qty_min; $qtyMax = $package->pivot->qty_max; $discountQty = $package->pivot->discount_qty; $discountRate = (100 - $package->pivot->discount_rate) / 100; $this->info( sprintf( " Package: %s (min: %d, max: %d, discount %d%% after the first %d, base cost: %d)", $package->title, $package->pivot->qty_min, $package->pivot->qty_max, $package->pivot->discount_rate, $package->pivot->discount_qty, $package->cost() ) ); foreach ($package->skus as $sku) { $this->info(sprintf(" SKU: %s (%d)", $sku->title, $sku->pivot->qty)); } if ($qtyMin < $discountQty) { $plan_costs += $qtyMin * $package->cost(); } elseif ($qtyMin == $discountQty) { $plan_costs += $package->cost(); } else { // base rate $plan_costs += $discountQty * $package->cost(); // discounted rate $plan_costs += ($qtyMin - $discountQty) * $package->cost() * $discountRate; } } $this->info(sprintf(" Plan costs per month: %d", $plan_costs)); } } } diff --git a/src/app/Console/Commands/Sku/ListUsers.php b/src/app/Console/Commands/Sku/ListUsers.php index 631bfed8..ae17eedc 100644 --- a/src/app/Console/Commands/Sku/ListUsers.php +++ b/src/app/Console/Commands/Sku/ListUsers.php @@ -1,64 +1,60 @@ argument('sku')); - - if (!$sku) { - $sku = \App\Sku::where('title', $this->argument('sku'))->first(); - } + $sku = $this->getObject(\App\Sku::class, $this->argument('sku'), 'title'); if (!$sku) { $this->error("Unable to find the SKU."); return 1; } $fn = function ($entitlement) { $user_id = $entitlement->user_id; if ($entitlement->entitleable_type == \App\User::class) { $user_id = $entitlement->entitleable_id; } return $user_id; }; $users = \App\Entitlement::select('user_id', 'entitleable_id', 'entitleable_type') ->join('wallets', 'wallets.id', '=', 'wallet_id') ->where('sku_id', $sku->id) ->get() ->map($fn) ->unique(); // TODO: This wereIn() might not scale \App\User::whereIn('id', $users)->orderBy('email')->get() ->pluck('email') ->each(function ($email, $key) { $this->info($email); }); } } diff --git a/src/app/Console/Commands/StripeInfo.php b/src/app/Console/Commands/StripeInfo.php index de67c474..0d0c0888 100644 --- a/src/app/Console/Commands/StripeInfo.php +++ b/src/app/Console/Commands/StripeInfo.php @@ -1,74 +1,74 @@ argument('user')) { - $user = User::where('email', $this->argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $this->info("Found user: {$user->id}"); $wallet = $user->wallets->first(); $provider = PaymentProvider::factory('stripe'); if ($mandate = $provider->getMandate($wallet)) { $amount = $wallet->getSetting('mandate_amount'); $balance = $wallet->getSetting('mandate_balance') ?: 0; $status = 'invalid'; if ($mandate['isPending']) { $status = 'pending'; } elseif ($mandate['isValid']) { $status = 'valid'; } if ($wallet->getSetting('mandate_disabled')) { $status .= ' (disabled)'; } $this->info("Auto-payment: {$mandate['method']}"); $this->info(" id: {$mandate['id']}"); $this->info(" status: {$status}"); $this->info(" amount: {$amount} {$wallet->currency}"); $this->info(" min-balance: {$balance} {$wallet->currency}"); } else { $this->info("Auto-payment: none"); } // TODO: List user payments history } else { // TODO: Fetch some info/stats from Stripe } } } diff --git a/src/app/Console/Commands/UserAddAlias.php b/src/app/Console/Commands/UserAddAlias.php index 6b87aa7b..59c4d62a 100644 --- a/src/app/Console/Commands/UserAddAlias.php +++ b/src/app/Console/Commands/UserAddAlias.php @@ -1,69 +1,59 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $alias = \strtolower($this->argument('alias')); // Check if the alias already exists if ($user->aliases()->where('alias', $alias)->first()) { $this->error("Address is already assigned to the user."); return 1; } $controller = $user->wallet()->owner; // Validate the alias $error = UsersController::validateAlias($alias, $controller); if ($error) { if (!$this->option('force')) { $this->error($error); return 1; } } $user->aliases()->create(['alias' => $alias]); } } diff --git a/src/app/Console/Commands/UserAssignSku.php b/src/app/Console/Commands/UserAssignSku.php index bdc1dd60..e36d6188 100644 --- a/src/app/Console/Commands/UserAssignSku.php +++ b/src/app/Console/Commands/UserAssignSku.php @@ -1,60 +1,56 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { $this->error("Unable to find the user {$this->argument('user')}."); return 1; } - $sku = \App\Sku::find($this->argument('sku')); - - if (!$sku) { - $sku = \App\Sku::where('title', $this->argument('sku'))->first(); - } + $sku = $this->getObject(\App\Sku::class, $this->argument('sku'), 'title'); if (!$sku) { $this->error("Unable to find the SKU {$this->argument('sku')}."); return 1; } $quantity = (int) $this->option('qty'); // Check if the entitlement already exists if (empty($quantity)) { if ($user->entitlements()->where('sku_id', $sku->id)->first()) { $this->error("The entitlement already exists. Maybe try with --qty=X?"); return 1; } } $user->assignSku($sku, $quantity ?: 1); } } diff --git a/src/app/Console/Commands/UserDelete.php b/src/app/Console/Commands/UserDelete.php index 780a01fc..eb74376f 100644 --- a/src/app/Console/Commands/UserDelete.php +++ b/src/app/Console/Commands/UserDelete.php @@ -1,48 +1,38 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $user->delete(); } } diff --git a/src/app/Console/Commands/UserDiscount.php b/src/app/Console/Commands/UserDiscount.php index c363f8a8..5d85a822 100644 --- a/src/app/Console/Commands/UserDiscount.php +++ b/src/app/Console/Commands/UserDiscount.php @@ -1,68 +1,58 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $this->info("Found user {$user->id}"); if ($this->argument('discount') === '0') { $discount = null; } else { - $discount = \App\Discount::find($this->argument('discount')); + $discount = $this->getObject(\App\Discount::class, $this->argument('discount')); if (!$discount) { return 1; } } foreach ($user->wallets as $wallet) { if (!$discount) { $wallet->discount()->dissociate(); } else { $wallet->discount()->associate($discount); } $wallet->save(); } } } diff --git a/src/app/Console/Commands/UserDomains.php b/src/app/Console/Commands/UserDomains.php index 17ebf5f0..ba2a0fc3 100644 --- a/src/app/Console/Commands/UserDomains.php +++ b/src/app/Console/Commands/UserDomains.php @@ -1,53 +1,40 @@ argument('userid'))->first(); + $user = $this->getUser($this->argument('userid')); if (!$user) { return 1; } foreach ($user->domains() as $domain) { $this->info("{$domain->namespace}"); } } } diff --git a/src/app/Console/Commands/UserEntitlements.php b/src/app/Console/Commands/UserEntitlements.php index 8d8d34ed..e3253b88 100644 --- a/src/app/Console/Commands/UserEntitlements.php +++ b/src/app/Console/Commands/UserEntitlements.php @@ -1,65 +1,53 @@ argument('userid'))->first(); + $user = $this->getUser($this->argument('userid')); if (!$user) { return 1; } $this->info("Found user: {$user->id}"); $skus_counted = []; foreach ($user->entitlements as $entitlement) { if (!array_key_exists($entitlement->sku_id, $skus_counted)) { $skus_counted[$entitlement->sku_id] = 1; } else { $skus_counted[$entitlement->sku_id] += 1; } } foreach ($skus_counted as $id => $qty) { - $sku = Sku::find($id); + $sku = \App\Sku::find($id); $this->info("SKU: {$sku->title} ({$qty})"); } } } diff --git a/src/app/Console/Commands/UserForceDelete.php b/src/app/Console/Commands/UserForceDelete.php index 1336ad26..eb818d2a 100644 --- a/src/app/Console/Commands/UserForceDelete.php +++ b/src/app/Console/Commands/UserForceDelete.php @@ -1,46 +1,46 @@ where('email', $this->argument('user'))->first(); + $user = $this->getUser($this->argument('user'), true); if (!$user) { return 1; } if (!$user->trashed()) { $this->error('The user is not yet deleted'); return 1; } DB::beginTransaction(); $user->forceDelete(); DB::commit(); } } diff --git a/src/app/Console/Commands/UserRestore.php b/src/app/Console/Commands/UserRestore.php index 0bd82cff..dbf8906b 100644 --- a/src/app/Console/Commands/UserRestore.php +++ b/src/app/Console/Commands/UserRestore.php @@ -1,47 +1,47 @@ where('email', $this->argument('user'))->first(); + $user = $this->getUser($this->argument('user'), true); if (!$user) { $this->error('User not found.'); return 1; } if (!$user->trashed()) { $this->error('The user is not yet deleted.'); return 1; } DB::beginTransaction(); $user->restore(); DB::commit(); } } diff --git a/src/app/Console/Commands/UserStatus.php b/src/app/Console/Commands/UserStatus.php index 70b7c885..0a6d26e4 100644 --- a/src/app/Console/Commands/UserStatus.php +++ b/src/app/Console/Commands/UserStatus.php @@ -1,63 +1,53 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $statuses = [ 'active' => User::STATUS_ACTIVE, 'suspended' => User::STATUS_SUSPENDED, 'deleted' => User::STATUS_DELETED, 'ldapReady' => User::STATUS_LDAP_READY, 'imapReady' => User::STATUS_IMAP_READY, ]; foreach ($statuses as $text => $bit) { $func = 'is' . \ucfirst($text); $this->info(sprintf("%d %s: %s", $bit, $text, $user->$func())); } $this->info("In total: {$user->status}"); } } diff --git a/src/app/Console/Commands/UserSuspend.php b/src/app/Console/Commands/UserSuspend.php index d0f90ede..c213d23b 100644 --- a/src/app/Console/Commands/UserSuspend.php +++ b/src/app/Console/Commands/UserSuspend.php @@ -1,51 +1,41 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $this->info("Found user: {$user->id}"); $user->suspend(); } } diff --git a/src/app/Console/Commands/UserUnsuspend.php b/src/app/Console/Commands/UserUnsuspend.php index 8497de6a..dbbd32e5 100644 --- a/src/app/Console/Commands/UserUnsuspend.php +++ b/src/app/Console/Commands/UserUnsuspend.php @@ -1,51 +1,40 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $this->info("Found user {$user->id}"); $user->unsuspend(); } } diff --git a/src/app/Console/Commands/UserVerify.php b/src/app/Console/Commands/UserVerify.php index 366a10a7..5830bf3a 100644 --- a/src/app/Console/Commands/UserVerify.php +++ b/src/app/Console/Commands/UserVerify.php @@ -1,51 +1,41 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } $this->info("Found user: {$user->id}"); $job = new \App\Jobs\User\VerifyJob($user->id); $job->handle(); } } diff --git a/src/app/Console/Commands/UserWallets.php b/src/app/Console/Commands/UserWallets.php index d5424331..f522efa8 100644 --- a/src/app/Console/Commands/UserWallets.php +++ b/src/app/Console/Commands/UserWallets.php @@ -1,50 +1,40 @@ argument('user'))->first(); + $user = $this->getUser($this->argument('user')); if (!$user) { return 1; } foreach ($user->wallets as $wallet) { $this->info("{$wallet->id} {$wallet->description}"); } } } diff --git a/src/app/Console/Commands/WalletAddTransaction.php b/src/app/Console/Commands/WalletAddTransaction.php index 445c9652..2c52a811 100644 --- a/src/app/Console/Commands/WalletAddTransaction.php +++ b/src/app/Console/Commands/WalletAddTransaction.php @@ -1,56 +1,46 @@ argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } $qty = (int) $this->argument('qty'); $message = $this->option('message'); if ($qty < 0) { $wallet->debit($qty, $message); } else { $wallet->credit($qty, $message); } } } diff --git a/src/app/Console/Commands/WalletBalances.php b/src/app/Console/Commands/WalletBalances.php index 8264fd3a..9eccfabc 100644 --- a/src/app/Console/Commands/WalletBalances.php +++ b/src/app/Console/Commands/WalletBalances.php @@ -1,65 +1,60 @@ each( + $wallets = \App\Wallet::select('wallets.*') + ->join('users', 'users.id', '=', 'wallets.user_id') + ->withEnvTenant('users') + ->all(); + + $wallets->each( function ($wallet) { if ($wallet->balance == 0) { return; } - $user = \App\User::where('id', $wallet->user_id)->first(); + $user = $wallet->owner; if (!$user) { return; } $this->info( sprintf( "%s: %8s (account: %s/%s (%s))", $wallet->id, $wallet->balance, "https://kolabnow.com/cockpit/admin/accounts/show", $user->id, $user->email ) ); } ); } } diff --git a/src/app/Console/Commands/WalletCharge.php b/src/app/Console/Commands/WalletCharge.php index d96d3d3f..c407e8c9 100644 --- a/src/app/Console/Commands/WalletCharge.php +++ b/src/app/Console/Commands/WalletCharge.php @@ -1,77 +1,67 @@ argument('wallet')) { // Find specified wallet by ID - $wallet = Wallet::find($wallet); + $wallet = $this->getWallet($wallet); - if (!$wallet || !$wallet->owner || $wallet->owner->tenant_id != \config('app.tenant_id')) { + if (!$wallet || !$wallet->owner) { return 1; } $wallets = [$wallet]; } else { // Get all wallets, excluding deleted accounts $wallets = Wallet::select('wallets.*') ->join('users', 'users.id', '=', 'wallets.user_id') ->withEnvTenant('users') ->whereNull('users.deleted_at') ->cursor(); } 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/Console/Commands/WalletDiscount.php b/src/app/Console/Commands/WalletDiscount.php index c0e2327b..30327913 100644 --- a/src/app/Console/Commands/WalletDiscount.php +++ b/src/app/Console/Commands/WalletDiscount.php @@ -1,62 +1,52 @@ argument('wallet'))->first(); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } // FIXME: Using '0' for delete might be not that obvious if ($this->argument('discount') === '0') { $wallet->discount()->dissociate(); } else { - $discount = \App\Discount::find($this->argument('discount')); + $discount = $this->getObject(\App\Discount::class, $this->argument('discount')); if (!$discount) { return 1; } $wallet->discount()->associate($discount); } $wallet->save(); } } diff --git a/src/app/Console/Commands/WalletExpected.php b/src/app/Console/Commands/WalletExpected.php index 74e7e926..fdeecb38 100644 --- a/src/app/Console/Commands/WalletExpected.php +++ b/src/app/Console/Commands/WalletExpected.php @@ -1,76 +1,68 @@ option('user')) { - $user = \App\User::where('email', $this->option('user')) - ->orWhere('id', $this->option('user'))->first(); + $user = $this->getUser($this->option('user')); if (!$user) { return 1; } $wallets = $user->wallets; } else { - $wallets = \App\Wallet::all(); + $wallets = \App\Wallet::select('wallets.*') + ->join('users', 'users.id', '=', 'wallets.user_id') + ->withEnvTenant('users') + ->all(); } foreach ($wallets as $wallet) { $charge = 0; $expected = $wallet->expectedCharges(); if (!$wallet->owner) { \Log::debug("{$wallet->id} has no owner: {$wallet->user_id}"); continue; } if ($this->option('non-zero') && $expected < 1) { continue; } $this->info( sprintf( "expect charging wallet %s for user %s with %d", $wallet->id, $wallet->owner->email, $expected ) ); } } } diff --git a/src/app/Console/Commands/WalletGetBalance.php b/src/app/Console/Commands/WalletGetBalance.php index 27f2fc25..661177dc 100644 --- a/src/app/Console/Commands/WalletGetBalance.php +++ b/src/app/Console/Commands/WalletGetBalance.php @@ -1,48 +1,38 @@ argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } $this->info($wallet->balance); } } diff --git a/src/app/Console/Commands/WalletGetDiscount.php b/src/app/Console/Commands/WalletGetDiscount.php index 6058f84a..6fdbf0ca 100644 --- a/src/app/Console/Commands/WalletGetDiscount.php +++ b/src/app/Console/Commands/WalletGetDiscount.php @@ -1,53 +1,43 @@ argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } if (!$wallet->discount) { $this->info("No discount on this wallet."); return 0; } $this->info($wallet->discount->discount); } } diff --git a/src/app/Console/Commands/WalletMandate.php b/src/app/Console/Commands/WalletMandate.php index e3193b59..f13265c5 100644 --- a/src/app/Console/Commands/WalletMandate.php +++ b/src/app/Console/Commands/WalletMandate.php @@ -1,70 +1,60 @@ argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } $mandate = PaymentsController::walletMandate($wallet); if (!empty($mandate['id'])) { $disabled = $mandate['isDisabled'] ? 'Yes' : 'No'; if ($this->option('disable') && $disabled == 'No') { $wallet->setSetting('mandate_disabled', 1); $disabled = 'Yes'; } elseif ($this->option('enable') && $disabled == 'Yes') { $wallet->setSetting('mandate_disabled', null); $disabled = 'No'; } $this->info("Auto-payment: {$mandate['method']}"); $this->info(" id: {$mandate['id']}"); $this->info(" status: " . ($mandate['isPending'] ? 'pending' : 'valid')); $this->info(" amount: {$mandate['amount']} {$wallet->currency}"); $this->info(" min-balance: {$mandate['balance']} {$wallet->currency}"); $this->info(" disabled: $disabled"); } else { $this->info("Auto-payment: none"); } } } diff --git a/src/app/Console/Commands/WalletSetBalance.php b/src/app/Console/Commands/WalletSetBalance.php index 62106119..d3a082bc 100644 --- a/src/app/Console/Commands/WalletSetBalance.php +++ b/src/app/Console/Commands/WalletSetBalance.php @@ -1,49 +1,39 @@ argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } - $wallet->balance = (int)($this->argument('balance')); + $wallet->balance = (int) $this->argument('balance'); $wallet->save(); } } diff --git a/src/app/Console/Commands/WalletSetDiscount.php b/src/app/Console/Commands/WalletSetDiscount.php index 79e0b45d..50e4e9d6 100644 --- a/src/app/Console/Commands/WalletSetDiscount.php +++ b/src/app/Console/Commands/WalletSetDiscount.php @@ -1,62 +1,52 @@ argument('wallet'))->first(); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } // FIXME: Using '0' for delete might be not that obvious if ($this->argument('discount') === '0') { $wallet->discount()->dissociate(); } else { - $discount = \App\Discount::find($this->argument('discount')); + $discount = $this->getObject(\App\Discount::class, $this->argument('discount')); if (!$discount) { return 1; } $wallet->discount()->associate($discount); } $wallet->save(); } } diff --git a/src/app/Console/Commands/WalletTransactions.php b/src/app/Console/Commands/WalletTransactions.php index 3be8872d..8b19f98a 100644 --- a/src/app/Console/Commands/WalletTransactions.php +++ b/src/app/Console/Commands/WalletTransactions.php @@ -1,72 +1,62 @@ argument('wallet'))->first(); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } foreach ($wallet->transactions()->orderBy('created_at')->get() as $transaction) { $this->info( sprintf( "%s: %s %s", $transaction->id, $transaction->created_at, $transaction->toString() ) ); if ($this->option('detail')) { $elements = \App\Transaction::where('transaction_id', $transaction->id) ->orderBy('created_at')->get(); foreach ($elements as $element) { $this->info( sprintf( " + %s: %s", $element->id, $element->toString() ) ); } } } } } diff --git a/src/app/Console/Commands/WalletUntil.php b/src/app/Console/Commands/WalletUntil.php index 6e544e1d..8d4d7eb2 100644 --- a/src/app/Console/Commands/WalletUntil.php +++ b/src/app/Console/Commands/WalletUntil.php @@ -1,50 +1,40 @@ argument('wallet')); + $wallet = $this->getWallet($this->argument('wallet')); if (!$wallet) { return 1; } $until = $wallet->balanceLastsUntil(); $this->info("Lasts until: " . ($until ? $until->toDateString() : 'unknown')); } }