feat(website-extras): refactor WebsiteExtra management to use 'codigo' for identification, update routes, requests, and services
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Attachable\Services\AttachmentService;
|
||||
use App\Domains\Shared\Rules\ImageOrBase64Rule;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Tenant\Models\WebsiteExtra;
|
||||
use App\Domains\Tenant\Models\WebsiteType;
|
||||
use App\Domains\Tenant\Models\WebsiteTypeExtra;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -34,7 +35,7 @@ class WebsiteExtraService
|
||||
}
|
||||
|
||||
$definitions = $this->definitionsFor($websiteTypeCode);
|
||||
$allowedNames = $definitions->pluck('nombre')->all();
|
||||
$allowedCodes = $definitions->pluck('codigo')->all();
|
||||
$hasRequiredExtras = $definitions->contains(
|
||||
fn (WebsiteTypeExtra $definition): bool => $definition->is_required
|
||||
);
|
||||
@@ -43,17 +44,17 @@ class WebsiteExtraService
|
||||
'extras' => [
|
||||
$hasRequiredExtras ? 'required' : 'sometimes',
|
||||
'array',
|
||||
function (string $attribute, mixed $value, \Closure $fail) use ($allowedNames): void {
|
||||
function (string $attribute, mixed $value, \Closure $fail) use ($allowedCodes): void {
|
||||
if (! is_array($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$unknownNames = array_diff(array_keys($value), $allowedNames);
|
||||
$unknownCodes = array_diff(array_keys($value), $allowedCodes);
|
||||
|
||||
if ($unknownNames !== []) {
|
||||
if ($unknownCodes !== []) {
|
||||
$fail(
|
||||
'The '.$attribute.' field contains extras not supported by the website type: '
|
||||
.implode(', ', $unknownNames).'.'
|
||||
.implode(', ', $unknownCodes).'.'
|
||||
);
|
||||
}
|
||||
},
|
||||
@@ -69,14 +70,14 @@ class WebsiteExtraService
|
||||
));
|
||||
array_unshift($rootRules, $definition->is_required ? 'required' : 'sometimes');
|
||||
|
||||
$rules["extras.{$definition->nombre}"] = $rootRules;
|
||||
$rules["extras.{$definition->codigo}"] = $rootRules;
|
||||
|
||||
foreach ($schemaRules as $path => $pathRules) {
|
||||
if ($path === '$') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rules[$this->requestAttribute($definition->nombre, $path)] = $this->compileRules($pathRules);
|
||||
$rules[$this->requestAttribute($definition->codigo, $path)] = $this->compileRules($pathRules);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +96,7 @@ class WebsiteExtraService
|
||||
}
|
||||
|
||||
$definitions = $this->definitionsFor((string) $tenant->website_type_code)
|
||||
->keyBy('nombre');
|
||||
->keyBy('codigo');
|
||||
|
||||
foreach ($extras as $name => $config) {
|
||||
/** @var WebsiteTypeExtra|null $definition */
|
||||
@@ -107,8 +108,14 @@ class WebsiteExtraService
|
||||
]);
|
||||
}
|
||||
|
||||
$transformedConfig = $this->applyTransforms($tenant, $definition, $config);
|
||||
$this->validateDatabaseConfig($definition, $transformedConfig);
|
||||
$requestRoot = "extras.{$definition->codigo}";
|
||||
$transformedConfig = $this->applyTransforms(
|
||||
$tenant,
|
||||
$definition,
|
||||
$config,
|
||||
$requestRoot
|
||||
);
|
||||
$this->validateDatabaseConfig($definition, $transformedConfig, $requestRoot);
|
||||
|
||||
$tenant->websiteExtras()->create([
|
||||
'website_type_extra_id' => $definition->id,
|
||||
@@ -120,18 +127,55 @@ class WebsiteExtraService
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all configured extras for a tenant.
|
||||
* Build request rules for one extra addressed by its stable code.
|
||||
*
|
||||
* @param array<string, mixed> $extras
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function replaceForTenant(Tenant $tenant, array $extras): void
|
||||
public function requestRulesForExtra(Tenant $tenant, string $extraCode): array
|
||||
{
|
||||
DB::transaction(function () use ($tenant, $extras): void {
|
||||
$tenant->websiteExtras()->delete();
|
||||
$this->createForTenant($tenant, $extras);
|
||||
});
|
||||
$definition = $this->definitionForTenant($tenant, $extraCode);
|
||||
$schemaRules = $definition->config_schema['request_rules'] ?? [];
|
||||
$rootRules = $this->compileRules($schemaRules['$'] ?? []);
|
||||
$rootRules = array_values(array_filter(
|
||||
$rootRules,
|
||||
fn (mixed $rule): bool => ! in_array($rule, ['required', 'sometimes'], true)
|
||||
));
|
||||
array_unshift($rootRules, 'required');
|
||||
|
||||
$tenant->unsetRelation('websiteExtras');
|
||||
$rules = ['config' => $rootRules];
|
||||
|
||||
foreach ($schemaRules as $path => $pathRules) {
|
||||
if ($path === '$') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rules[$this->configAttribute('config', $path)] = $this->compileRules($pathRules);
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function updateForTenant(Tenant $tenant, string $extraCode, mixed $config): WebsiteExtra
|
||||
{
|
||||
$definition = $this->definitionForTenant($tenant, $extraCode);
|
||||
|
||||
return DB::transaction(function () use ($tenant, $definition, $config): WebsiteExtra {
|
||||
$transformedConfig = $this->applyTransforms($tenant, $definition, $config, 'config');
|
||||
$this->validateDatabaseConfig($definition, $transformedConfig, 'config');
|
||||
|
||||
return $tenant->websiteExtras()->updateOrCreate(
|
||||
['website_type_extra_id' => $definition->id],
|
||||
['config' => $transformedConfig]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public function definitionForTenant(Tenant $tenant, string $extraCode): WebsiteTypeExtra
|
||||
{
|
||||
return WebsiteTypeExtra::query()
|
||||
->where('website_type_code', $tenant->website_type_code)
|
||||
->where('codigo', $extraCode)
|
||||
->firstOrFail();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,23 +207,29 @@ class WebsiteExtraService
|
||||
);
|
||||
}
|
||||
|
||||
private function requestAttribute(string $extraName, string $path): string
|
||||
private function requestAttribute(string $extraCode, string $path): string
|
||||
{
|
||||
return $this->configAttribute("extras.{$extraCode}", $path);
|
||||
}
|
||||
|
||||
private function configAttribute(string $root, string $path): string
|
||||
{
|
||||
if ($path === '$') {
|
||||
return "extras.{$extraName}";
|
||||
return $root;
|
||||
}
|
||||
|
||||
if (str_starts_with($path, '$.')) {
|
||||
$path = substr($path, 2);
|
||||
}
|
||||
|
||||
return "extras.{$extraName}.{$path}";
|
||||
return "{$root}.{$path}";
|
||||
}
|
||||
|
||||
private function applyTransforms(
|
||||
Tenant $tenant,
|
||||
WebsiteTypeExtra $definition,
|
||||
mixed $config
|
||||
mixed $config,
|
||||
string $requestRoot
|
||||
): mixed {
|
||||
foreach ($definition->config_schema['transforms'] ?? [] as $path => $transform) {
|
||||
$segments = $this->pathSegments($path);
|
||||
@@ -191,7 +241,8 @@ class WebsiteExtraService
|
||||
$definition,
|
||||
$path,
|
||||
$value,
|
||||
$transform
|
||||
$transform,
|
||||
$requestRoot
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -245,7 +296,8 @@ class WebsiteExtraService
|
||||
WebsiteTypeExtra $definition,
|
||||
string $path,
|
||||
mixed $value,
|
||||
array $transform
|
||||
array $transform,
|
||||
string $requestRoot
|
||||
): mixed {
|
||||
if ($value === null) {
|
||||
return null;
|
||||
@@ -253,7 +305,7 @@ class WebsiteExtraService
|
||||
|
||||
if (($transform['handler'] ?? null) !== 'attachment') {
|
||||
throw new InvalidArgumentException(
|
||||
"Unsupported transform handler for {$definition->nombre}: ".($transform['handler'] ?? 'null')
|
||||
"Unsupported transform handler for {$definition->codigo}: ".($transform['handler'] ?? 'null')
|
||||
);
|
||||
}
|
||||
|
||||
@@ -262,7 +314,7 @@ class WebsiteExtraService
|
||||
|
||||
if (! $attachment) {
|
||||
throw ValidationException::withMessages([
|
||||
$this->requestAttribute($definition->nombre, $path) => [
|
||||
$this->configAttribute($requestRoot, $path) => [
|
||||
'The selected attachment does not exist.',
|
||||
],
|
||||
]);
|
||||
@@ -270,7 +322,7 @@ class WebsiteExtraService
|
||||
} else {
|
||||
$attachment = $this->attachmentService->store(
|
||||
$value,
|
||||
"tenants/{$tenant->codigo}/extras/{$definition->nombre}"
|
||||
"tenants/{$tenant->codigo}/extras/{$definition->codigo}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -282,7 +334,7 @@ class WebsiteExtraService
|
||||
&& $attachment->type->value !== $expectedType
|
||||
) {
|
||||
throw ValidationException::withMessages([
|
||||
$this->requestAttribute($definition->nombre, $path) => [
|
||||
$this->configAttribute($requestRoot, $path) => [
|
||||
"The attachment must be of type {$expectedType}.",
|
||||
],
|
||||
]);
|
||||
@@ -293,7 +345,8 @@ class WebsiteExtraService
|
||||
|
||||
private function validateDatabaseConfig(
|
||||
WebsiteTypeExtra $definition,
|
||||
mixed $config
|
||||
mixed $config,
|
||||
string $requestRoot
|
||||
): void {
|
||||
$schemaRules = $definition->config_schema['database_rules'] ?? [];
|
||||
$rules = [];
|
||||
@@ -312,7 +365,7 @@ class WebsiteExtraService
|
||||
|
||||
foreach ($validator->errors()->toArray() as $attribute => $errors) {
|
||||
$suffix = $attribute === 'config' ? '' : substr($attribute, strlen('config'));
|
||||
$messages["extras.{$definition->nombre}{$suffix}"] = $errors;
|
||||
$messages["{$requestRoot}{$suffix}"] = $errors;
|
||||
}
|
||||
|
||||
throw ValidationException::withMessages($messages);
|
||||
|
||||
Reference in New Issue
Block a user