big update all menus
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\NasWebfigDevice;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Models\NasScriptPolicy;
|
||||
|
||||
class NasResourceService
|
||||
{
|
||||
@@ -31,7 +32,7 @@ class NasResourceService
|
||||
$model = $this->model($resource);
|
||||
|
||||
return $model::query()
|
||||
->with('tenant:id,tenant_code,tenant_name')
|
||||
->with(['tenant:id,tenant_code,tenant_name', ...($resource === 'mikrotik' ? ['tenant.nasSetting'] : []), ...($resource === 'package-profile' ? ['scripts.latestVersion'] : [])])
|
||||
->when(! $actor->isMasterAdmin(), fn ($q) => $q->where('tenant_id', $tenantId))
|
||||
->when($actor->isMasterAdmin() && $tenantId, fn ($q) => $q->where('tenant_id', $tenantId))
|
||||
->when(! empty($filters['status']), fn ($q) => $q->where('status', $filters['status']))
|
||||
@@ -40,7 +41,7 @@ class NasResourceService
|
||||
->when(! empty($filters['search']), function ($q) use ($filters, $resource) {
|
||||
$columns = match ($resource) {
|
||||
'mikrotik' => ['name', 'host', 'api_username'],
|
||||
'package-profile' => ['name', 'external_profile_name'],
|
||||
'package-profile' => ['name', 'profile_code'],
|
||||
'olt' => ['name', 'host', 'vendor', 'model'],
|
||||
'webfig' => ['name', 'url', 'device_type'],
|
||||
};
|
||||
@@ -59,11 +60,15 @@ class NasResourceService
|
||||
{
|
||||
$model = $this->model($resource);
|
||||
|
||||
return $model::with('tenant:id,tenant_code,tenant_name')->findOrFail($id);
|
||||
return $model::with(['tenant:id,tenant_code,tenant_name', ...($resource === 'mikrotik' ? ['tenant.nasSetting'] : []), ...($resource === 'package-profile' ? ['scripts.latestVersion'] : [])])->findOrFail($id);
|
||||
}
|
||||
|
||||
public function create(string $resource, array $data, User $actor, ?int $tenantId): Model
|
||||
{
|
||||
if ($resource === 'mikrotik') $data['connection_type'] = 'api';
|
||||
if ($resource === 'package-profile') $data = $this->normalizePackageProfile($data);
|
||||
$scripts = $resource === 'package-profile' ? ($data['scripts'] ?? []) : [];
|
||||
unset($data['scripts']);
|
||||
$data['tenant_id'] = $actor->isMasterAdmin() ? ($data['tenant_id'] ?? $tenantId) : $tenantId;
|
||||
if (! $data['tenant_id']) {
|
||||
throw ValidationException::withMessages(['tenant_id' => 'Tenant wajib dipilih.']);
|
||||
@@ -71,11 +76,18 @@ class NasResourceService
|
||||
$data['status'] ??= 'active';
|
||||
$model = $this->model($resource);
|
||||
|
||||
return $model::create($data)->load('tenant:id,tenant_code,tenant_name');
|
||||
$created = $model::create($data);
|
||||
if ($resource === 'package-profile') $this->syncProfileScripts($created, $scripts);
|
||||
return $created->load('tenant:id,tenant_code,tenant_name');
|
||||
}
|
||||
|
||||
public function update(string $resource, Model $model, array $data, User $actor): Model
|
||||
{
|
||||
if ($resource === 'mikrotik') $data['connection_type'] = 'api';
|
||||
if ($resource === 'package-profile') $data = $this->normalizePackageProfile($data, $model);
|
||||
$hasScripts = $resource === 'package-profile' && array_key_exists('scripts', $data);
|
||||
$scripts = $data['scripts'] ?? [];
|
||||
unset($data['scripts']);
|
||||
if (! $actor->isMasterAdmin()) {
|
||||
unset($data['tenant_id']);
|
||||
}
|
||||
@@ -85,10 +97,45 @@ class NasResourceService
|
||||
}
|
||||
}
|
||||
$model->update($data);
|
||||
if ($hasScripts) $this->syncProfileScripts($model, $scripts);
|
||||
|
||||
return $model->fresh()->load('tenant:id,tenant_code,tenant_name');
|
||||
}
|
||||
|
||||
private function syncProfileScripts(Model $profile, array $scripts): void
|
||||
{
|
||||
$sync=[];
|
||||
foreach($scripts as $row) {
|
||||
$policy = NasScriptPolicy::with('versions:id,nas_script_policy_id')->findOrFail($row['policy_id']);
|
||||
if ($policy->service_type !== $profile->service_type || $policy->event !== $row['event']) {
|
||||
throw ValidationException::withMessages(['scripts' => "Script {$policy->name} tidak sesuai tipe layanan atau event profile."]);
|
||||
}
|
||||
if (($row['version_id'] ?? null) && ! $policy->versions->contains('id', $row['version_id'])) {
|
||||
throw ValidationException::withMessages(['scripts' => "Versi Script {$policy->name} tidak valid."]);
|
||||
}
|
||||
$sync[$row['policy_id']]=['event'=>$row['event'],'nas_script_policy_version_id'=>$row['version_id'] ?? null,'enabled'=>true];
|
||||
}
|
||||
$profile->scripts()->sync($sync);
|
||||
}
|
||||
|
||||
private function normalizePackageProfile(array $data, ?Model $profile = null): array
|
||||
{
|
||||
$type = $data['service_type'] ?? $profile?->service_type;
|
||||
if ($type !== 'hotspot') {
|
||||
foreach (['usage_mode','validity_start','validity_value','validity_unit','uptime_limit_seconds','data_limit_bytes','grace_period_minutes'] as $field) $data[$field] = null;
|
||||
}
|
||||
if ($type !== 'ppp') foreach (['local_address', 'remote_address_pool'] as $field) $data[$field] = null;
|
||||
if ($type !== 'hotspot') foreach (['session_timeout', 'idle_timeout', 'shared_users'] as $field) $data[$field] = null;
|
||||
if (! in_array($type, ['static_ip', 'dynamic_ip'], true)) {
|
||||
foreach (['ip_pool','subnet_cidr','gateway','dns_servers','vlan_id'] as $field) $data[$field] = null;
|
||||
$data['mac_lock_required'] = false;
|
||||
} else {
|
||||
$data['mac_lock_required'] = true;
|
||||
if ($type === 'static_ip') $data['ip_pool'] = null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function delete(Model $model): bool
|
||||
{
|
||||
return $model->delete();
|
||||
|
||||
Reference in New Issue
Block a user