diff --git a/src/app/Console/Commands/Domain/SetWalletCommand.php b/src/app/Console/Commands/Domain/SetWalletCommand.php --- a/src/app/Console/Commands/Domain/SetWalletCommand.php +++ b/src/app/Console/Commands/Domain/SetWalletCommand.php @@ -3,9 +3,7 @@ namespace App\Console\Commands\Domain; use App\Console\Command; -use App\Entitlement; -use App\Domain; -use App\Sku; +use App\Package; use Illuminate\Support\Facades\Queue; class SetWalletCommand extends Command @@ -15,7 +13,7 @@ * * @var string */ - protected $signature = 'domain:set-wallet {domain} {wallet}'; + protected $signature = 'domain:set-wallet {domain} {wallet} {--package=}'; /** * The console command description. @@ -46,23 +44,28 @@ } if ($entitlement = $domain->entitlements()->first()) { - $this->error("Domain already assigned to a wallet: {$entitlement->wallet->id}."); + $this->error("Domain already assigned to a wallet: {$entitlement->wallet_id}."); return 1; } - $sku = Sku::withObjectTenantContext($domain)->where('title', 'domain-hosting')->first(); + if ($p = $this->option('package')) { + $package = Package::withObjectTenantContext($domain)->find($p); + + if (!$package) { + $package = Package::withObjectTenantContext($domain)->where('title', $p)->first(); + } + } else { + $package = Package::withObjectTenantContext($domain)->where('title', 'domain-hosting')->first(); + } + + if (!$package) { + $this->error("Package not found."); + return 1; + } 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, - ] - ); + // Assign package the same as we do in DomainsController when a new domain is created + $domain->assignPackageAndWallet($package, $wallet); } } diff --git a/src/tests/Feature/Console/Domain/SetWalletTest.php b/src/tests/Feature/Console/Domain/SetWalletTest.php --- a/src/tests/Feature/Console/Domain/SetWalletTest.php +++ b/src/tests/Feature/Console/Domain/SetWalletTest.php @@ -54,7 +54,14 @@ $john = $this->getTestUser('john@kolab.org'); $wallet = $john->wallets->first(); - $code = \Artisan::call("domain:set-wallet domain-delete.com " . $wallet->id); + // Non-existing package + $code = \Artisan::call("domain:set-wallet domain-delete.com {$wallet->id} --package=123"); + $output = trim(\Artisan::output()); + $this->assertSame(1, $code); + $this->assertSame("Package not found.", $output); + + // All good, expect success + $code = \Artisan::call("domain:set-wallet domain-delete.com {$wallet->id}"); $output = trim(\Artisan::output()); $this->assertSame(0, $code); $this->assertSame('', $output); @@ -67,7 +74,7 @@ $this->assertSame($wallet->id, $entitlement->wallet_id); // Already assigned to a wallet - $code = \Artisan::call("domain:set-wallet domain-delete.com " . $wallet->id); + $code = \Artisan::call("domain:set-wallet domain-delete.com {$wallet->id}"); $output = trim(\Artisan::output()); $this->assertSame(1, $code); $this->assertSame("Domain already assigned to a wallet: {$wallet->id}.", $output);