mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:24:04 +00:00
Improve code of pending tenants (#1025)
* Remove `--all` option from ClearPendingTenants * Improve query formatting * Remove redundant test * Convert time constrait options to int * Improve CreatePendingTenants success message
This commit is contained in:
parent
73c5655bc8
commit
7d3298c6bb
4 changed files with 19 additions and 46 deletions
|
|
@ -10,7 +10,6 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
class ClearPendingTenants extends Command
|
class ClearPendingTenants extends Command
|
||||||
{
|
{
|
||||||
protected $signature = 'tenants:pending-clear
|
protected $signature = 'tenants:pending-clear
|
||||||
{--all : Override the default settings and deletes all pending tenants}
|
|
||||||
{--older-than-days= : Deletes all pending tenants older than the amount of days}
|
{--older-than-days= : Deletes all pending tenants older than the amount of days}
|
||||||
{--older-than-hours= : Deletes all pending tenants older than the amount of hours}';
|
{--older-than-hours= : Deletes all pending tenants older than the amount of hours}';
|
||||||
|
|
||||||
|
|
@ -24,13 +23,8 @@ class ClearPendingTenants extends Command
|
||||||
// We compare the original expiration date to the new one to check if the new one is different later
|
// We compare the original expiration date to the new one to check if the new one is different later
|
||||||
$originalExpirationDate = $expirationDate->copy()->toImmutable();
|
$originalExpirationDate = $expirationDate->copy()->toImmutable();
|
||||||
|
|
||||||
// Skip the time constraints if the 'all' option is given
|
$olderThanDays = (int) $this->option('older-than-days');
|
||||||
if (! $this->option('all')) {
|
$olderThanHours = (int) $this->option('older-than-hours');
|
||||||
/** @var ?int $olderThanDays */
|
|
||||||
$olderThanDays = $this->option('older-than-days');
|
|
||||||
|
|
||||||
/** @var ?int $olderThanHours */
|
|
||||||
$olderThanHours = $this->option('older-than-hours');
|
|
||||||
|
|
||||||
if ($olderThanDays && $olderThanHours) {
|
if ($olderThanDays && $olderThanHours) {
|
||||||
$this->line("<options=bold,reverse;fg=red> Cannot use '--older-than-days' and '--older-than-hours' together \n"); // todo@cli refactor all of these styled command outputs to use $this->components
|
$this->line("<options=bold,reverse;fg=red> Cannot use '--older-than-days' and '--older-than-hours' together \n"); // todo@cli refactor all of these styled command outputs to use $this->components
|
||||||
|
|
@ -46,10 +40,8 @@ class ClearPendingTenants extends Command
|
||||||
if ($olderThanHours) {
|
if ($olderThanHours) {
|
||||||
$expirationDate->subHours($olderThanHours);
|
$expirationDate->subHours($olderThanHours);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$deletedTenantCount = tenancy()
|
$deletedTenantCount = tenancy()->query()
|
||||||
->query()
|
|
||||||
->onlyPending()
|
->onlyPending()
|
||||||
->when($originalExpirationDate->notEqualTo($expirationDate), function (Builder $query) use ($expirationDate) {
|
->when($originalExpirationDate->notEqualTo($expirationDate), function (Builder $query) use ($expirationDate) {
|
||||||
$query->where($query->getModel()->getColumnForQuery('pending_since'), '<', $expirationDate->timestamp);
|
$query->where($query->getModel()->getColumnForQuery('pending_since'), '<', $expirationDate->timestamp);
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,8 @@ class CreatePendingTenants extends Command
|
||||||
$createdCount++;
|
$createdCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info($createdCount . ' ' . str('tenant')->plural($createdCount) . ' created.');
|
$this->info($createdCount . ' pending ' . str('tenant')->plural($createdCount) . ' created.');
|
||||||
$this->info($maxPendingTenantCount . ' ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.');
|
$this->info($maxPendingTenantCount . ' pending ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -39,8 +39,7 @@ class CreatePendingTenants extends Command
|
||||||
/** Calculate the number of currently available pending tenants. */
|
/** Calculate the number of currently available pending tenants. */
|
||||||
protected function getPendingTenantCount(): int
|
protected function getPendingTenantCount(): int
|
||||||
{
|
{
|
||||||
return tenancy()
|
return tenancy()->query()
|
||||||
->query()
|
|
||||||
->onlyPending()
|
->onlyPending()
|
||||||
->count();
|
->count();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,7 @@ trait HasTenantOptions
|
||||||
|
|
||||||
protected function getTenants(): LazyCollection
|
protected function getTenants(): LazyCollection
|
||||||
{
|
{
|
||||||
return tenancy()
|
return tenancy()->query()
|
||||||
->query()
|
|
||||||
->when($this->option('tenants'), function ($query) {
|
->when($this->option('tenants'), function ($query) {
|
||||||
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
|
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -67,23 +67,6 @@ test('CreatePendingTenants command cannot run with both time constraints', funct
|
||||||
->assertFailed();
|
->assertFailed();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('CreatePendingTenants commands all option overrides any config constraints', function () {
|
|
||||||
Tenant::createPending();
|
|
||||||
Tenant::createPending();
|
|
||||||
|
|
||||||
tenancy()->model()->query()->onlyPending()->first()->update([
|
|
||||||
'pending_since' => now()->subDays(10)
|
|
||||||
]);
|
|
||||||
|
|
||||||
config(['tenancy.pending.older_than_days' => 4]);
|
|
||||||
|
|
||||||
Artisan::call(ClearPendingTenants::class, [
|
|
||||||
'--all' => true
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect(Tenant::onlyPending()->count())->toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('tenancy can check if there are any pending tenants', function () {
|
test('tenancy can check if there are any pending tenants', function () {
|
||||||
expect(Tenant::onlyPending()->exists())->toBeFalse();
|
expect(Tenant::onlyPending()->exists())->toBeFalse();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue