An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix(destinations): handle empty and server-scoped destinations
Andras Bacsai65c0c92c0258
diff --git a/app/Livewire/Destination/Index.php b/app/Livewire/Destination/Index.phpindex a3df3fd562..7a4b89fab9 100644--- a/app/Livewire/Destination/Index.php+++ b/app/Livewire/Destination/Index.php@@ -3,6 +3,7 @@namespace App\Livewire\Destination;use App\Models\Server;+use Illuminate\Support\Collection;use Livewire\Attributes\Locked;use Livewire\Component;@@ -11,9 +12,15 @@ class Index extends Component#[Locked]public $servers;- public function mount()+ #[Locked]+ public Collection $destinations;++ public function mount(): void{$this->servers = Server::isUsable()->get();+ $this->destinations = $this->servers+ ->flatMap(fn (Server $server) => $server->standaloneDockers->concat($server->swarmDockers))+ ->values();}public function render()diff --git a/app/Livewire/Destination/New/Docker.php b/app/Livewire/Destination/New/Docker.phpindex 6f9b6f9950..2548231639 100644--- a/app/Livewire/Destination/New/Docker.php+++ b/app/Livewire/Destination/New/Docker.php@@ -33,44 +33,49 @@ class Docker extends Component#[Validate(['required', 'boolean'])]public bool $isSwarm = false;- public function mount(?string $server_id = null)+ public function mount(?string $server_id = null): void{- $this->network = new Cuid2;+ $this->network = (string) new Cuid2;$this->servers = Server::isUsable()->get();- if ($server_id) {- $foundServer = $this->servers->find($server_id) ?: $this->servers->first();- if (! $foundServer) {- throw new \Exception('Server not found.');++ if (filled($server_id)) {+ $this->selectedServer = Server::ownedByCurrentTeam()->whereKey($server_id)->firstOrFail();++ if (! $this->servers->contains('id', $this->selectedServer->id)) {+ $this->servers->push($this->selectedServer);}- $this->selectedServer = $foundServer;- $this->serverId = $this->selectedServer->id;++ $this->serverId = (string) $this->selectedServer->id;} else {$foundServer = $this->servers->first();if (! $foundServer) {throw new \Exception('Server not found.');}$this->selectedServer = $foundServer;- $this->serverId = $this->selectedServer->id;+ $this->serverId = (string) $this->selectedServer->id;}$this->generateName();}- public function updatedServerId()+ public function updatedServerId(): void{$this->selectedServer = $this->servers->find($this->serverId);+ if (! $this->selectedServer) {+ throw new \Exception('Server not found.');+ }$this->generateName();}- public function generateName()+ public function generateName(): void{$name = data_get($this->selectedServer, 'name', new Cuid2);$this->name = str("{$name}-{$this->network}")->kebab();}- public function submit()+ public function submit(): mixed{try {- $this->authorize('create', StandaloneDocker::class);+ $this->authorize('create', $this->isSwarm ? SwarmDocker::class : StandaloneDocker::class);$this->validate();if ($this->isSwarm) {$found = $this->selectedServer->swarmDockers()->where('network', $this->network)->first();diff --git a/app/Livewire/Server/Destinations.php b/app/Livewire/Server/Destinations.phpindex 117b43ad64..f3f1426467 100644--- a/app/Livewire/Server/Destinations.php+++ b/app/Livewire/Server/Destinations.php@@ -45,7 +45,7 @@ class Destinations extends Component} else {SwarmDocker::create(['name' => $this->server->name.'-'.$name,- 'network' => $this->name,+ 'network' => $name,'server_id' => $this->server->id,]);}diff --git a/resources/views/livewire/destination/index.blade.php b/resources/views/livewire/destination/index.blade.phpindex aecd58d7ad..d5026a651d 100644--- a/resources/views/livewire/destination/index.blade.php+++ b/resources/views/livewire/destination/index.blade.php@@ -14,34 +14,30 @@</div><div class="subtitle">Network endpoints to deploy your resources.</div><div class="grid gap-4 lg:grid-cols-2 -mt-1">- @forelse ($servers as $server)- @forelse ($server->destinations() as $destination)- @if ($destination->getMorphClass() === 'App\Models\StandaloneDocker')- <a class="coolbox group" {{ wireNavigate() }}- href="{{ route('destination.show', ['destination_uuid' => data_get($destination, 'uuid')]) }}">- <div class="flex flex-col justify-center mx-6">- <div class="box-title">{{ $destination->name }}</div>- <div class="box-description">Server: {{ $destination->server->name }}</div>+ @forelse ($destinations as $destination)+ @if ($destination->getMorphClass() === 'App\Models\StandaloneDocker')+ <a class="coolbox group" {{ wireNavigate() }}+ href="{{ route('destination.show', ['destination_uuid' => data_get($destination, 'uuid')]) }}">+ <div class="flex flex-col justify-center mx-6">+ <div class="box-title">{{ $destination->name }}</div>+ <div class="box-description">Server: {{ $destination->server->name }}</div>+ </div>+ </a>+ @endif+ @if ($destination->getMorphClass() === 'App\Models\SwarmDocker')+ <a class="coolbox group" {{ wireNavigate() }}+ href="{{ route('destination.show', ['destination_uuid' => data_get($destination, 'uuid')]) }}">+ <div class="flex flex-col mx-6">+ <div class="box-title">+ {{ $destination->name }}+ <x-deprecated-badge /></div>- </a>- @endif- @if ($destination->getMorphClass() === 'App\Models\SwarmDocker')- <a class="coolbox group" {{ wireNavigate() }}- href="{{ route('destination.show', ['destination_uuid' => data_get($destination, 'uuid')]) }}">- <div class="flex flex-col mx-6">- <div class="box-title">- {{ $destination->name }}- <x-deprecated-badge />- </div>- <div class="box-description">server: {{ $destination->server->name }}</div>- </div>- </a>- @endif- @empty- <div>No destinations found.</div>- @endforelse+ <div class="box-description">server: {{ $destination->server->name }}</div>+ </div>+ </a>+ @endif@empty- <div>No servers found.</div>+ <div>No destinations found.</div>@endforelse</div></div>diff --git a/resources/views/livewire/server/destinations.blade.php b/resources/views/livewire/server/destinations.blade.phpindex b5e8111e93..9d8a2b4370 100644--- a/resources/views/livewire/server/destinations.blade.php+++ b/resources/views/livewire/server/destinations.blade.php@@ -29,6 +29,9 @@<x-forms.button>{{ data_get($docker, 'network') }} </x-forms.button></a>@endforeach+ @if ($server->standaloneDockers->isEmpty() && $server->swarmDockers->isEmpty())+ <div class="text-sm text-neutral-500">No destinations configured for this server yet.</div>+ @endif</div>@if ($networks->count() > 0)<div class="pt-2">diff --git a/tests/Feature/ServerDestinationsPageTest.php b/tests/Feature/ServerDestinationsPageTest.phpnew file mode 100644index 0000000000..3a1fb1790b--- /dev/null+++ b/tests/Feature/ServerDestinationsPageTest.php@@ -0,0 +1,107 @@+<?php++use App\Livewire\Destination\New\Docker;+use App\Livewire\Server\Destinations;+use App\Models\InstanceSettings;+use App\Models\Server;+use App\Models\StandaloneDocker;+use App\Models\SwarmDocker;+use App\Models\Team;+use App\Models\User;+use Illuminate\Foundation\Testing\RefreshDatabase;+use Illuminate\Support\Facades\Queue;+use Livewire\Livewire;++uses(RefreshDatabase::class);++beforeEach(function () {+ Queue::fake();++ InstanceSettings::unguarded(fn () => InstanceSettings::query()->create(['id' => 0]));++ $this->user = User::factory()->create();+ $this->team = Team::factory()->create();+ $this->user->teams()->attach($this->team, ['role' => 'owner']);++ $this->actingAs($this->user);+ session(['currentTeam' => $this->team]);+});++test('destination creation modal can mount with selected team server even when global usable server list excludes it', function () {+ $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server->settings()->update([+ 'is_reachable' => true,+ 'is_usable' => true,+ 'is_build_server' => true,+ ]);++ StandaloneDocker::withoutEvents(fn () => $server->standaloneDockers()->delete());++ Livewire::test(Docker::class, ['server_id' => (string) $server->id])+ ->assertSet('selectedServer.id', $server->id)+ ->assertSet('serverId', (string) $server->id);+});++test('server destinations page renders when selected server has no destinations', function () {+ $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server->settings()->update([+ 'is_reachable' => true,+ 'is_usable' => true,+ 'is_build_server' => true,+ ]);++ StandaloneDocker::withoutEvents(fn () => $server->standaloneDockers()->delete());++ $this->get(route('server.destinations', ['server_uuid' => $server->uuid]))+ ->assertSuccessful()+ ->assertSee('Destinations')+ ->assertSee('No destinations configured for this server yet.')+ ->assertDontSee('Server not found.');+});++test('global destinations page does not render per-server empty states beside existing destinations', function () {+ $serverWithDestination = Server::factory()->create(['team_id' => $this->team->id]);+ $serverWithDestination->settings()->update([+ 'is_reachable' => true,+ 'is_usable' => true,+ ]);++ $serverWithoutDestination = Server::factory()->create(['team_id' => $this->team->id]);+ $serverWithoutDestination->settings()->update([+ 'is_reachable' => true,+ 'is_usable' => true,+ ]);+ StandaloneDocker::withoutEvents(fn () => $serverWithoutDestination->standaloneDockers()->delete());++ $this->get(route('destination.index'))+ ->assertSuccessful()+ ->assertSee($serverWithDestination->standaloneDockers()->first()->name)+ ->assertDontSee('No destinations found.');+});++test('global destinations page renders a single empty state when no usable servers have destinations', function () {+ $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server->settings()->update([+ 'is_reachable' => true,+ 'is_usable' => true,+ ]);+ StandaloneDocker::withoutEvents(fn () => $server->standaloneDockers()->delete());++ $this->get(route('destination.index'))+ ->assertSuccessful()+ ->assertSee('No destinations found.');+});++test('adding a discovered swarm destination stores the selected network name', function () {+ $server = Server::factory()->create(['team_id' => $this->team->id]);+ $server->settings()->update([+ 'is_reachable' => true,+ 'is_usable' => true,+ 'is_swarm_manager' => true,+ ]);++ Livewire::test(Destinations::class, ['server_uuid' => $server->uuid])+ ->call('add', 'customer-network');++ expect(SwarmDocker::where('server_id', $server->id)->where('network', 'customer-network')->exists())->toBeTrue();+});