feat(database): remove 'database_rules' from WebsiteTypeExtra configurations and related tests
This commit is contained in:
@@ -12,7 +12,6 @@ use App\Domains\Tenant\Models\WebsiteType;
|
|||||||
use App\Domains\Tenant\Models\WebsiteTypeExtra;
|
use App\Domains\Tenant\Models\WebsiteTypeExtra;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Validator;
|
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
@@ -115,8 +114,6 @@ class WebsiteExtraService
|
|||||||
$config,
|
$config,
|
||||||
$requestRoot
|
$requestRoot
|
||||||
);
|
);
|
||||||
$this->validateDatabaseConfig($definition, $transformedConfig, $requestRoot);
|
|
||||||
|
|
||||||
$tenant->websiteExtras()->create([
|
$tenant->websiteExtras()->create([
|
||||||
'website_type_extra_id' => $definition->id,
|
'website_type_extra_id' => $definition->id,
|
||||||
'config' => $transformedConfig,
|
'config' => $transformedConfig,
|
||||||
@@ -161,7 +158,6 @@ class WebsiteExtraService
|
|||||||
|
|
||||||
return DB::transaction(function () use ($tenant, $definition, $config): WebsiteExtra {
|
return DB::transaction(function () use ($tenant, $definition, $config): WebsiteExtra {
|
||||||
$transformedConfig = $this->applyTransforms($tenant, $definition, $config, 'config');
|
$transformedConfig = $this->applyTransforms($tenant, $definition, $config, 'config');
|
||||||
$this->validateDatabaseConfig($definition, $transformedConfig, 'config');
|
|
||||||
|
|
||||||
return $tenant->websiteExtras()->updateOrCreate(
|
return $tenant->websiteExtras()->updateOrCreate(
|
||||||
['website_type_extra_id' => $definition->id],
|
['website_type_extra_id' => $definition->id],
|
||||||
@@ -342,33 +338,4 @@ class WebsiteExtraService
|
|||||||
|
|
||||||
return $attachment->id;
|
return $attachment->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validateDatabaseConfig(
|
|
||||||
WebsiteTypeExtra $definition,
|
|
||||||
mixed $config,
|
|
||||||
string $requestRoot
|
|
||||||
): void {
|
|
||||||
$schemaRules = $definition->config_schema['database_rules'] ?? [];
|
|
||||||
$rules = [];
|
|
||||||
|
|
||||||
foreach ($schemaRules as $path => $pathRules) {
|
|
||||||
$attribute = $path === '$'
|
|
||||||
? 'config'
|
|
||||||
: 'config.'.ltrim(str_starts_with($path, '$.') ? substr($path, 2) : $path, '.');
|
|
||||||
$rules[$attribute] = $this->compileRules($pathRules);
|
|
||||||
}
|
|
||||||
|
|
||||||
$validator = Validator::make(['config' => $config], $rules);
|
|
||||||
|
|
||||||
if ($validator->fails()) {
|
|
||||||
$messages = [];
|
|
||||||
|
|
||||||
foreach ($validator->errors()->toArray() as $attribute => $errors) {
|
|
||||||
$suffix = $attribute === 'config' ? '' : substr($attribute, strlen('config'));
|
|
||||||
$messages["{$requestRoot}{$suffix}"] = $errors;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw ValidationException::withMessages($messages);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
DB::table('website_type_extras')
|
||||||
|
->select(['id', 'config_schema'])
|
||||||
|
->orderBy('id')
|
||||||
|
->each(function (object $definition): void {
|
||||||
|
$schema = json_decode($definition->config_schema, true);
|
||||||
|
|
||||||
|
if (! is_array($schema) || ! array_key_exists('database_rules', $schema)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($schema['database_rules']);
|
||||||
|
|
||||||
|
DB::table('website_type_extras')
|
||||||
|
->where('id', $definition->id)
|
||||||
|
->update(['config_schema' => json_encode($schema)]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
// Removed rules cannot be reconstructed generically.
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -31,10 +31,6 @@ class WebsiteTypeSeeder extends Seeder
|
|||||||
'attachment_type' => 'image',
|
'attachment_type' => 'image',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'database_rules' => [
|
|
||||||
'$' => 'required|array',
|
|
||||||
'$.*' => 'required|integer|distinct|exists:attachments,id',
|
|
||||||
],
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@@ -65,14 +61,6 @@ class WebsiteTypeSeeder extends Seeder
|
|||||||
'attachment_type' => 'image',
|
'attachment_type' => 'image',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'database_rules' => [
|
|
||||||
'$' => 'required|array',
|
|
||||||
'title_html' => 'nullable|string',
|
|
||||||
'description_html' => 'nullable|string',
|
|
||||||
'button_text' => 'nullable|string',
|
|
||||||
'button_href' => 'nullable|string',
|
|
||||||
'background_image_id' => 'nullable|integer|exists:attachments,id',
|
|
||||||
],
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@@ -93,14 +81,6 @@ class WebsiteTypeSeeder extends Seeder
|
|||||||
'dates.*' => 'required|date_format:Y-m-d|distinct',
|
'dates.*' => 'required|date_format:Y-m-d|distinct',
|
||||||
],
|
],
|
||||||
'transforms' => [],
|
'transforms' => [],
|
||||||
'database_rules' => [
|
|
||||||
'$' => 'required|array',
|
|
||||||
'title' => 'nullable|string',
|
|
||||||
'location' => 'nullable|string',
|
|
||||||
'dates_text' => 'nullable|string|max:255',
|
|
||||||
'dates' => 'nullable|array',
|
|
||||||
'dates.*' => 'required|date_format:Y-m-d|distinct',
|
|
||||||
],
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -37,10 +37,6 @@ class WebsiteTypeSeederTest extends TestCase
|
|||||||
'attachment_type' => 'image',
|
'attachment_type' => 'image',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'database_rules' => [
|
|
||||||
'$' => 'required|array',
|
|
||||||
'$.*' => 'required|integer|distinct|exists:attachments,id',
|
|
||||||
],
|
|
||||||
], $shopIt->extras->sole()->config_schema);
|
], $shopIt->extras->sole()->config_schema);
|
||||||
|
|
||||||
$onTicket = WebsiteType::query()
|
$onTicket = WebsiteType::query()
|
||||||
@@ -70,14 +66,6 @@ class WebsiteTypeSeederTest extends TestCase
|
|||||||
'attachment_type' => 'image',
|
'attachment_type' => 'image',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'database_rules' => [
|
|
||||||
'$' => 'required|array',
|
|
||||||
'title_html' => 'nullable|string',
|
|
||||||
'description_html' => 'nullable|string',
|
|
||||||
'button_text' => 'nullable|string',
|
|
||||||
'button_href' => 'nullable|string',
|
|
||||||
'background_image_id' => 'nullable|integer|exists:attachments,id',
|
|
||||||
],
|
|
||||||
], $heroSchema);
|
], $heroSchema);
|
||||||
|
|
||||||
$eventSchema = $onTicket->extras->firstWhere('codigo', 'eventConfig')->config_schema;
|
$eventSchema = $onTicket->extras->firstWhere('codigo', 'eventConfig')->config_schema;
|
||||||
@@ -91,14 +79,6 @@ class WebsiteTypeSeederTest extends TestCase
|
|||||||
'dates.*' => 'required|date_format:Y-m-d|distinct',
|
'dates.*' => 'required|date_format:Y-m-d|distinct',
|
||||||
],
|
],
|
||||||
'transforms' => [],
|
'transforms' => [],
|
||||||
'database_rules' => [
|
|
||||||
'$' => 'required|array',
|
|
||||||
'title' => 'nullable|string',
|
|
||||||
'location' => 'nullable|string',
|
|
||||||
'dates_text' => 'nullable|string|max:255',
|
|
||||||
'dates' => 'nullable|array',
|
|
||||||
'dates.*' => 'required|date_format:Y-m-d|distinct',
|
|
||||||
],
|
|
||||||
], $eventSchema);
|
], $eventSchema);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,10 +39,6 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
|
|||||||
'phone' => 'required|string|max:30',
|
'phone' => 'required|string|max:30',
|
||||||
],
|
],
|
||||||
'transforms' => [],
|
'transforms' => [],
|
||||||
'database_rules' => [
|
|
||||||
'$' => 'required|array',
|
|
||||||
'phone' => 'required|string|max:30',
|
|
||||||
],
|
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -100,7 +96,6 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
|
|||||||
'config_schema' => [
|
'config_schema' => [
|
||||||
'request_rules' => ['$' => 'required|array'],
|
'request_rules' => ['$' => 'required|array'],
|
||||||
'transforms' => [],
|
'transforms' => [],
|
||||||
'database_rules' => ['$' => 'required|array'],
|
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user