Ability to select allowed regions/plans per provider

This commit is contained in:
Dennis
2021-08-12 11:23:01 +02:00
parent 3a8682bbed
commit f1aace3d8f
8 changed files with 115 additions and 4 deletions

View File

@@ -14,6 +14,8 @@ class ProviderController extends Controller
return inertia('Admin/Services/Provider/Edit', [
'provider' => $provider,
'availablePlans' => $provider->plans()->pluck('label', 'id'),
'availableRegions' => $provider->regions()->pluck('label', 'id'),
]);
}

View File

@@ -104,9 +104,21 @@ class ServerController extends Controller
{
$provider = $request->user()->package->providers()->findOrFail($providerId);
$regions = $provider->regions()
->when($provider->allowed_regions, function ($query) use ($provider) {
return $query->whereIn('id', $provider->allowed_regions);
})
->pluck('label', 'id');
$plans = $provider->plans()
->when($provider->allowed_plans, function ($query) use ($provider) {
return $query->whereIn('id', $provider->allowed_plans);
})
->pluck('label', 'id');
return [
'regions' => $provider->regions()->pluck('label', 'id'),
'plans' => $provider->plans()->pluck('label', 'id'),
'regions' => $regions,
'plans' => $plans,
];
}
}

View File

@@ -28,6 +28,14 @@ class ProviderRequest extends FormRequest
'required',
'string',
'max:255'
],
'allowed_plans' => [
'nullable',
'array'
],
'allowed_regions' => [
'nullable',
'array'
]
];
}

View File

@@ -8,6 +8,11 @@ class Provider extends Model
{
protected $guarded = [];
protected $casts = [
'allowed_plans' => 'json',
'allowed_regions' => 'json',
];
public function getNameWithLabelAttribute()
{
$string = $this->name;

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddAllowedRegionsAndPlansToProvidersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('providers', function (Blueprint $table) {
$table->json('allowed_plans')->nullable()->after('name');
$table->json('allowed_regions')->nullable()->after('allowed_plans');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('providers', function (Blueprint $table) {
$table->dropColumn('allowed_plans', 'allowed_regions');
});
}
}

2
public/js/app.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -21,6 +21,43 @@
<template #form>
<form class="space-y-4" @submit.prevent="submit">
<FormInput :label="__('Name')" :errors="$page.props.errors.name" v-model="form.name" />
<div class="space-y-4">
<h3 class="text-base leading-6 font-medium border-b border-dotted border-medium-emphasis pb-1">
{{ __('Allowed plans') }}</h3>
<p class="text-medium-emphasis">
Select the plans here that your users are allowed to use for this provider.
If there are no plans selected, all will be available.
</p>
<div>
<div class="space-y-1" v-for="(name, id) in availablePlans">
<input :id="`plan-${id}`" :value="id" v-model="form.allowed_plans"
class="form-checkbox" type="checkbox">
<label :for="`plan-${id}`" class="ml-2 text-sm">{{ name }}</label>
</div>
</div>
</div>
<div class="space-y-4">
<h3 class="text-base leading-6 font-medium border-b border-dotted border-medium-emphasis pb-1">
{{ __('Allowed regions') }}</h3>
<p class="text-medium-emphasis">
Select the regions here that your users are allowed to use for this provider.
If there are no regions selected, all will be available.
</p>
<div>
<div class="space-y-1" v-for="(name, id) in availableRegions">
<input :id="`region-${id}`" :value="id" v-model="form.allowed_regions"
class="form-checkbox" type="checkbox">
<label :for="`region-${id}`" class="ml-2 text-sm">{{ name }}</label>
</div>
</div>
</div>
<FormActions>
<Button>{{ __('Save changes') }}</Button>
<Button variant="danger" type="button" @click="confirmDelete">{{ __('Delete') }}</Button>
@@ -110,12 +147,16 @@
form: {
name: this.provider.name,
allowed_plans: this.provider.allowed_plans ?? [],
allowed_regions: this.provider.allowed_regions ?? [],
},
}
},
props: {
provider: Object,
availableRegions: [Object, Array],
availablePlans: [Object, Array]
},
methods: {

View File

@@ -30,7 +30,17 @@
</TableHead>
<TableBody>
<TableRow v-for="availableProvider in availableProviders" :key="availableProvider.id">
<TableData>{{ availableProvider.name }}</TableData>
<TableData class="space-y-2">
<div>
{{ availableProvider.name }}
</div>
<div class="space-x-1 text-xs">
<span class="text-medium-emphasis">{{ availableProvider.provider.plans.length }} plan(s)</span>
<span>&centerdot;</span>
<span class="text-medium-emphasis">{{ availableProvider.provider.regions.length }} region(s)</span>
</div>
</TableData>
<TableData>{{ availableProvider.label }}</TableData>
<TableData class="text-right">
<Button size="sm" v-on:click="syncProvider(availableProvider)">