feat: implement product attributes and tenant properties management with CRUD controllers, requests, and domain models
This commit is contained in:
@@ -6,7 +6,7 @@ use App\Domains\Catalog\Models\ProductAttribute;
|
||||
use App\Domains\Catalog\Requests\StoreProductAttributeRequest;
|
||||
use App\Domains\Catalog\Requests\UpdateProductAttributeRequest;
|
||||
use App\Domains\Catalog\Resources\ProductAttributeResource;
|
||||
use App\Domains\Catalog\Support\ProductAttributeType;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -33,7 +33,7 @@ class ProductAttributeController extends Controller
|
||||
$options = $validated['options'] ?? [];
|
||||
unset($validated['options']);
|
||||
|
||||
$type = ProductAttributeType::from((string) $validated['type']);
|
||||
$type = FieldType::from((string) $validated['type']);
|
||||
if (! $type->supportsOptions()) {
|
||||
$validated['metadata_schema'] = null;
|
||||
$options = [];
|
||||
@@ -68,7 +68,7 @@ class ProductAttributeController extends Controller
|
||||
$options = $validated['options'] ?? [];
|
||||
unset($validated['options']);
|
||||
|
||||
$type = ProductAttributeType::from((string) $validated['type']);
|
||||
$type = FieldType::from((string) $validated['type']);
|
||||
if (! $type->supportsOptions()) {
|
||||
$validated['metadata_schema'] = null;
|
||||
$options = [];
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Domains\Catalog\Models;
|
||||
|
||||
use App\Domains\Catalog\Support\ProductAttributeType;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -32,7 +32,7 @@ class ProductAttribute extends Model
|
||||
return [
|
||||
'is_required' => 'boolean',
|
||||
'metadata_schema' => 'array',
|
||||
'type' => ProductAttributeType::class,
|
||||
'type' => FieldType::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Domains\Catalog\Requests;
|
||||
|
||||
use App\Domains\Catalog\Support\ProductAttributeType;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\Validator;
|
||||
@@ -31,15 +31,15 @@ class StoreProductAttributeRequest extends FormRequest
|
||||
'nombre' => ['required', 'string', 'max:255'],
|
||||
'is_required' => ['sometimes', 'boolean'],
|
||||
'metadata_schema' => ['nullable', 'array'],
|
||||
'type' => ['required', Rule::enum(ProductAttributeType::class)],
|
||||
'type' => ['required', Rule::enum(FieldType::class)],
|
||||
'options' => [
|
||||
Rule::requiredIf(fn (): bool => in_array($this->input('type'), [
|
||||
ProductAttributeType::Select->value,
|
||||
ProductAttributeType::Multiselect->value,
|
||||
FieldType::Select->value,
|
||||
FieldType::Multiselect->value,
|
||||
], true)),
|
||||
Rule::prohibitedIf(fn (): bool => ! in_array($this->input('type'), [
|
||||
ProductAttributeType::Select->value,
|
||||
ProductAttributeType::Multiselect->value,
|
||||
FieldType::Select->value,
|
||||
FieldType::Multiselect->value,
|
||||
], true)),
|
||||
'array',
|
||||
],
|
||||
@@ -53,7 +53,7 @@ class StoreProductAttributeRequest extends FormRequest
|
||||
{
|
||||
$validator->after(function (Validator $validator): void {
|
||||
$type = $this->input('type');
|
||||
$supportsOptions = in_array($type, [ProductAttributeType::Select->value, ProductAttributeType::Multiselect->value], true);
|
||||
$supportsOptions = in_array($type, [FieldType::Select->value, FieldType::Multiselect->value], true);
|
||||
|
||||
if (! $supportsOptions && $this->filled('metadata_schema')) {
|
||||
$validator->errors()->add('metadata_schema', 'The metadata_schema field is only allowed for select and multiselect attributes.');
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace App\Domains\Catalog\Requests;
|
||||
|
||||
use App\Domains\Catalog\Models\ProductAttribute;
|
||||
use App\Domains\Catalog\Support\ProductAttributeType;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\Validator;
|
||||
@@ -35,7 +35,7 @@ class UpdateProductAttributeRequest extends FormRequest
|
||||
'nombre' => ['required', 'string', 'max:255'],
|
||||
'is_required' => ['sometimes', 'boolean'],
|
||||
'metadata_schema' => ['nullable', 'array'],
|
||||
'type' => ['required', Rule::enum(ProductAttributeType::class)],
|
||||
'type' => ['required', Rule::enum(FieldType::class)],
|
||||
'options' => ['sometimes', 'array'],
|
||||
'options.*.label' => ['required', 'string', 'max:255'],
|
||||
'options.*.sort_order' => ['sometimes', 'integer'],
|
||||
@@ -47,7 +47,7 @@ class UpdateProductAttributeRequest extends FormRequest
|
||||
{
|
||||
$validator->after(function (Validator $validator): void {
|
||||
$type = $this->input('type');
|
||||
$supportsOptions = in_array($type, [ProductAttributeType::Select->value, ProductAttributeType::Multiselect->value], true);
|
||||
$supportsOptions = in_array($type, [FieldType::Select->value, FieldType::Multiselect->value], true);
|
||||
|
||||
if (! $supportsOptions && $this->filled('metadata_schema')) {
|
||||
$validator->errors()->add('metadata_schema', 'The metadata_schema field is only allowed for select and multiselect attributes.');
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Catalog\Support;
|
||||
|
||||
enum ProductAttributeType: string
|
||||
{
|
||||
case Text = 'text';
|
||||
case Numeric = 'numeric';
|
||||
case Select = 'select';
|
||||
case Multiselect = 'multiselect';
|
||||
|
||||
public function supportsOptions(): bool
|
||||
{
|
||||
return in_array($this, [self::Select, self::Multiselect], true);
|
||||
}
|
||||
}
|
||||
27
app/Domains/Shared/Enums/FieldType.php
Normal file
27
app/Domains/Shared/Enums/FieldType.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Shared\Enums;
|
||||
|
||||
enum FieldType: string
|
||||
{
|
||||
case String = 'string';
|
||||
case Number = 'number';
|
||||
case Boolean = 'boolean';
|
||||
case Select = 'select';
|
||||
case Multiselect = 'multiselect';
|
||||
case Color = 'color';
|
||||
case Image = 'image';
|
||||
|
||||
public function supportsOptions(): bool
|
||||
{
|
||||
return in_array($this, [self::Select, self::Multiselect], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return array_column(self::cases(), 'value');
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use App\Domains\Tenant\Support\TenantPropDataType;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -28,7 +28,7 @@ class TenantProp extends Model
|
||||
{
|
||||
return [
|
||||
'is_required' => 'boolean',
|
||||
'data_type' => TenantPropDataType::class,
|
||||
'data_type' => FieldType::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Domains\Tenant\Requests;
|
||||
|
||||
use App\Domains\Tenant\Support\TenantPropDataType;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@@ -23,7 +23,7 @@ class StoreTenantPropRequest extends FormRequest
|
||||
'nombre' => ['required', 'string', 'max:255'],
|
||||
'descripcion' => ['nullable', 'string'],
|
||||
'is_required' => ['sometimes', 'boolean'],
|
||||
'data_type' => ['required', Rule::enum(TenantPropDataType::class)],
|
||||
'data_type' => ['required', Rule::enum(FieldType::class)],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace App\Domains\Tenant\Requests;
|
||||
|
||||
use App\Domains\Tenant\Models\TenantProp;
|
||||
use App\Domains\Tenant\Support\TenantPropDataType;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@@ -27,7 +27,7 @@ class UpdateTenantPropRequest extends FormRequest
|
||||
'nombre' => ['required', 'string', 'max:255'],
|
||||
'descripcion' => ['nullable', 'string'],
|
||||
'is_required' => ['sometimes', 'boolean'],
|
||||
'data_type' => ['required', Rule::enum(TenantPropDataType::class)],
|
||||
'data_type' => ['required', Rule::enum(FieldType::class)],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Support;
|
||||
|
||||
enum TenantPropDataType: string
|
||||
{
|
||||
case String = 'string';
|
||||
case Number = 'number';
|
||||
case Boolean = 'boolean';
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return array_column(self::cases(), 'value');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
DB::table('productos_attributes')
|
||||
->where('type', 'text')
|
||||
->update(['type' => 'string']);
|
||||
|
||||
DB::table('productos_attributes')
|
||||
->where('type', 'numeric')
|
||||
->update(['type' => 'number']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
DB::table('productos_attributes')
|
||||
->where('type', 'string')
|
||||
->update(['type' => 'text']);
|
||||
|
||||
DB::table('productos_attributes')
|
||||
->where('type', 'number')
|
||||
->update(['type' => 'numeric']);
|
||||
}
|
||||
};
|
||||
@@ -42,11 +42,11 @@ class ProductAttributeControllerTest extends TestCase
|
||||
|
||||
$response
|
||||
->assertCreated()
|
||||
->assertJsonPath('tenant_codigo', 'acme')
|
||||
->assertJsonPath('codigo', 'color')
|
||||
->assertJsonPath('type', 'select')
|
||||
->assertJsonPath('options.0.label', 'Red')
|
||||
->assertJsonPath('options.1.metadata.hex', '#0000ff');
|
||||
->assertJsonPath('data.tenant_codigo', 'acme')
|
||||
->assertJsonPath('data.codigo', 'color')
|
||||
->assertJsonPath('data.type', 'select')
|
||||
->assertJsonPath('data.options.0.label', 'Red')
|
||||
->assertJsonPath('data.options.1.metadata.hex', '#0000ff');
|
||||
|
||||
$this->assertDatabaseHas('productos_attributes', [
|
||||
'tenant_codigo' => 'acme',
|
||||
@@ -60,7 +60,7 @@ class ProductAttributeControllerTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_rejects_options_for_text_attributes(): void
|
||||
public function test_it_rejects_options_for_string_attributes(): void
|
||||
{
|
||||
Tenant::create([
|
||||
'codigo' => 'acme',
|
||||
@@ -71,7 +71,7 @@ class ProductAttributeControllerTest extends TestCase
|
||||
$response = $this->postJson('/api/tenants/acme/product-attributes', [
|
||||
'codigo' => 'material',
|
||||
'nombre' => 'Material',
|
||||
'type' => 'text',
|
||||
'type' => 'string',
|
||||
'options' => [
|
||||
['label' => 'Cotton'],
|
||||
],
|
||||
|
||||
@@ -33,9 +33,9 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonPath('codigo', 'acme')
|
||||
->assertJsonPath('dominio', 'acme.com')
|
||||
->assertJsonPath('props.primary_color', 'blue');
|
||||
->assertJsonPath('data.codigo', 'acme')
|
||||
->assertJsonPath('data.dominio', 'acme.com')
|
||||
->assertJsonPath('data.props.primary_color', 'blue');
|
||||
}
|
||||
|
||||
public function test_it_bootstraps_a_tenant_from_a_full_url(): void
|
||||
@@ -52,8 +52,8 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonPath('codigo', 'acme')
|
||||
->assertJsonPath('dominio', 'acme.com');
|
||||
->assertJsonPath('data.codigo', 'acme')
|
||||
->assertJsonPath('data.dominio', 'acme.com');
|
||||
}
|
||||
|
||||
public function test_it_returns_not_found_when_the_domain_does_not_exist(): void
|
||||
@@ -84,8 +84,8 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
|
||||
$firstResponse
|
||||
->assertCreated()
|
||||
->assertJsonPath('dominio', 'acme.com')
|
||||
->assertJsonPath('props.primary_color', 'blue');
|
||||
->assertJsonPath('data.dominio', 'acme.com')
|
||||
->assertJsonPath('data.props.primary_color', 'blue');
|
||||
|
||||
$this->assertDatabaseHas('tenant_prop_values', [
|
||||
'tenant_codigo' => 'acme',
|
||||
@@ -137,9 +137,9 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
|
||||
$successfulResponse
|
||||
->assertOk()
|
||||
->assertJsonPath('nombre', 'Acme Updated')
|
||||
->assertJsonPath('dominio', 'acme.com')
|
||||
->assertJsonPath('props.primary_color', 'green');
|
||||
->assertJsonPath('data.nombre', 'Acme Updated')
|
||||
->assertJsonPath('data.dominio', 'acme.com')
|
||||
->assertJsonPath('data.props.primary_color', 'green');
|
||||
|
||||
$failingResponse = $this->putJson("/api/tenants/{$otherTenant->id}", [
|
||||
'codigo' => 'globex',
|
||||
|
||||
@@ -21,8 +21,8 @@ class TenantPropControllerTest extends TestCase
|
||||
|
||||
$response
|
||||
->assertCreated()
|
||||
->assertJsonPath('codigo', 'primary_color')
|
||||
->assertJsonPath('data_type', 'string');
|
||||
->assertJsonPath('data.codigo', 'primary_color')
|
||||
->assertJsonPath('data.data_type', 'string');
|
||||
|
||||
$this->assertDatabaseHas('tenant_props', [
|
||||
'codigo' => 'primary_color',
|
||||
|
||||
Reference in New Issue
Block a user