fix server not having default service (#100)

This commit is contained in:
Saeed Vaziry 2024-01-30 20:45:17 +01:00 committed by GitHub
parent 2214a76e09
commit 3da1f4fe4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View File

@ -4,6 +4,7 @@
use App\Contracts\ServerType;
use App\Enums\ServerStatus;
use App\Enums\ServiceStatus;
use App\Facades\Notifier;
use App\Facades\SSH;
use App\Jobs\Installation\Upgrade;
@ -227,6 +228,18 @@ public function defaultService($type): ?Service
->where('is_default', 1)
->first();
// If no default service found, get the first service with status ready or stopped
if (! $service) {
$service = $this->services()
->where('type', $type)
->whereIn('status', [ServiceStatus::READY, ServiceStatus::STOPPED])
->first();
if ($service) {
$service->is_default = 1;
$service->save();
}
}
return $service;
}

View File

@ -0,0 +1,20 @@
<?php
namespace Tests\Feature\Models;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ServerModelTest extends TestCase
{
use RefreshDatabase;
public function test_should_have_default_service()
{
$php = $this->server->defaultService('php');
$php->update(['is_default' => false]);
$this->assertNotNull($this->server->defaultService('php'));
$php->refresh();
$this->assertTrue($php->is_default);
}
}