feat: custom server limit

This commit is contained in:
Andras Bacsai 2024-02-23 15:45:53 +01:00
parent 55dd1ab0a1
commit 297b314904
6 changed files with 58 additions and 13 deletions

View File

@ -3,6 +3,7 @@
namespace App\Livewire\Server; namespace App\Livewire\Server;
use App\Models\PrivateKey; use App\Models\PrivateKey;
use App\Models\Team;
use Livewire\Component; use Livewire\Component;
class Create extends Component class Create extends Component
@ -16,11 +17,7 @@ class Create extends Component
$this->limit_reached = false; $this->limit_reached = false;
return; return;
} }
$team = currentTeam(); $this->limit_reached = Team::serverLimitReached();
$servers = $team->servers->count();
['serverLimit' => $serverLimit] = $team->limits;
$this->limit_reached = $servers >= $serverLimit;
} }
public function render() public function render()
{ {

View File

@ -5,6 +5,7 @@ namespace App\Livewire\Server\New;
use App\Enums\ProxyStatus; use App\Enums\ProxyStatus;
use App\Enums\ProxyTypes; use App\Enums\ProxyTypes;
use App\Models\Server; use App\Models\Server;
use App\Models\Team;
use Livewire\Component; use Livewire\Component;
class ByIp extends Component class ByIp extends Component
@ -76,6 +77,9 @@ class ByIp extends Component
if (is_null($this->private_key_id)) { if (is_null($this->private_key_id)) {
return $this->dispatch('error', 'You must select a private key'); return $this->dispatch('error', 'You must select a private key');
} }
if (Team::serverLimitReached()) {
return $this->dispatch('error', 'You have reached the server limit for your subscription.');
}
$payload = [ $payload = [
'name' => $this->name, 'name' => $this->name,
'description' => $this->description, 'description' => $this->description,

View File

@ -2,6 +2,7 @@
namespace App\Livewire\Subscription; namespace App\Livewire\Subscription;
use App\Models\Team;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Livewire\Component; use Livewire\Component;
@ -10,9 +11,7 @@ class Actions extends Component
public $server_limits = 0; public $server_limits = 0;
public function mount() public function mount()
{ {
$limits = currentTeam()->limits; $this->server_limits = Team::serverLimit();
$this->server_limits = data_get($limits, 'serverLimit', 0);
} }
public function cancel() public function cancel()
{ {

View File

@ -49,6 +49,16 @@ class Team extends Model implements SendsDiscord, SendsEmail
return explode(',', $recipients); return explode(',', $recipients);
} }
static public function serverLimitReached() {
$serverLimit = Team::serverLimit();
$team = currentTeam();
$servers = $team->servers->count();
return $servers >= $serverLimit;
}
static public function serverLimit()
{
return data_get(Team::find(currentTeam()->id), 'limits.serverLimit', 0);
}
public function limits(): Attribute public function limits(): Attribute
{ {
return Attribute::make( return Attribute::make(
@ -63,14 +73,19 @@ class Team extends Model implements SendsDiscord, SendsEmail
$subscription = $subscription->type(); $subscription = $subscription->type();
} }
} }
$serverLimit = config('constants.limits.server')[strtolower($subscription)]; if ($this->custom_server_limit) {
$serverLimit = $this->custom_server_limit;
} else {
$serverLimit = config('constants.limits.server')[strtolower($subscription)];
}
$sharedEmailEnabled = config('constants.limits.email')[strtolower($subscription)]; $sharedEmailEnabled = config('constants.limits.email')[strtolower($subscription)];
return ['serverLimit' => $serverLimit, 'sharedEmailEnabled' => $sharedEmailEnabled]; return ['serverLimit' => $serverLimit, 'sharedEmailEnabled' => $sharedEmailEnabled];
} }
); );
} }
public function environment_variables() { public function environment_variables()
{
return $this->hasMany(SharedEnvironmentVariable::class)->whereNull('project_id')->whereNull('environment_id'); return $this->hasMany(SharedEnvironmentVariable::class)->whereNull('project_id')->whereNull('environment_id');
} }
public function members() public function members()
@ -130,7 +145,8 @@ class Team extends Model implements SendsDiscord, SendsEmail
{ {
return $this->hasMany(S3Storage::class)->where('is_usable', true); return $this->hasMany(S3Storage::class)->where('is_usable', true);
} }
public function trialEnded() { public function trialEnded()
{
foreach ($this->servers as $server) { foreach ($this->servers as $server) {
$server->settings()->update([ $server->settings()->update([
'is_usable' => false, 'is_usable' => false,
@ -138,7 +154,8 @@ class Team extends Model implements SendsDiscord, SendsEmail
]); ]);
} }
} }
public function trialEndedButSubscribed() { public function trialEndedButSubscribed()
{
foreach ($this->servers as $server) { foreach ($this->servers as $server) {
$server->settings()->update([ $server->settings()->update([
'is_usable' => true, 'is_usable' => true,

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('teams', function (Blueprint $table) {
$table->integer('custom_server_limit')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('teams', function (Blueprint $table) {
$table->dropColumn('custom_server_limit');
});
}
};

View File

@ -1,6 +1,6 @@
<div class="flex flex-col items-center justify-center h-screen"> <div class="flex flex-col items-center justify-center h-screen">
<span class="text-xl font-bold text-white">You have reached the limit of {{ $name }} you can create.</span> <span class="text-xl font-bold text-white">You have reached the limit of {{ $name }} you can create.</span>
<span>Please <a class="text-white underline "href="{{ route('team.index') }}">upgrade your <span>Please <a class="text-white underline "href="{{ route('subscription.show') }}">upgrade your
subscription</a> to create more subscription</a> to create more
{{ $name }}.</span> {{ $name }}.</span>
</div> </div>