refactor(catalog): add event date attribute with dynamic options and update related logic

This commit is contained in:
2026-08-07 10:03:31 -03:00
parent 14d53d1e0b
commit 717ee5d194
11 changed files with 224 additions and 86 deletions

View File

@@ -182,19 +182,37 @@ class CatalogItemDetailControllerTest extends TestCase
'time_start' => '09:00',
'time_end' => '18:00',
]);
$unusedEventDate = $tenant->eventDates()->create([
'date' => '2026-10-10',
'time_start' => '10:00',
'time_end' => '19:00',
]);
$item = $this->createItem($tenant, 'Entry');
$attribute = Attribute::query()->create([
'tenant_codigo' => $tenant->codigo,
'codigo' => 'event_date',
'nombre' => 'Fecha',
'is_required' => true,
'type' => FieldType::EventDate,
]);
$item->itemAttributes()->create(['attribute_id' => $attribute->id]);
$variant = $this->createVariant($item, 10, 0);
$variant->update(['event_date_id' => $eventDate->id]);
$this->getJson("/api/tenants/{$tenant->codigo}/catalog-items/{$item->id}")
->assertOk()
->assertJsonPath('data.attributes.0.id', -1)
->assertJsonPath('data.attributes.0.id', $attribute->id)
->assertJsonPath('data.attributes.0.codigo', 'event_date')
->assertJsonPath('data.attributes.0.type', 'event_date')
->assertJsonCount(2, 'data.attributes.0.options')
->assertJsonPath('data.attributes.0.options.0.id', $eventDate->id)
->assertJsonPath('data.attributes.0.options.0.value', (string) $eventDate->id)
->assertJsonPath('data.attributes.0.options.0.label', '09/10/2026 · 09:00 a 18:00')
->assertJsonPath('data.attributes.0.options.1.id', $unusedEventDate->id)
->assertJsonPath('data.attributes.0.options.1.value', (string) $unusedEventDate->id)
->assertJsonPath('data.variants.0.values.event_date', (string) $eventDate->id);
$this->assertDatabaseCount('attribute_options', 0);
}
private function createItem(

View File

@@ -96,6 +96,7 @@ class CatalogServiceTest extends TestCase
public function test_it_allows_the_same_event_date_with_different_attribute_values(): void
{
$sector = $this->createAttribute('sector');
$eventDateAttribute = $this->createAttribute('event_date', FieldType::EventDate);
$eventDate = $this->tenant->eventDates()->create([
'date' => '2026-10-09',
'time_start' => '09:00',
@@ -107,7 +108,7 @@ class CatalogServiceTest extends TestCase
'slug' => 'entry-by-sector',
'nombre' => 'Entry by sector',
'precio' => 100,
'attribute_codes' => [$sector->codigo],
'attribute_codes' => [$eventDateAttribute->codigo, $sector->codigo],
'variants' => [
[
'event_date_id' => $eventDate->id,
@@ -125,11 +126,20 @@ class CatalogServiceTest extends TestCase
[$eventDate->id, $eventDate->id],
$item->variants->pluck('event_date_id')->all(),
);
$this->assertSame(
['event_date', 'sector'],
$item->itemAttributes->pluck('attribute.codigo')->all(),
);
$this->assertSame(
FieldType::EventDate,
$item->itemAttributes->first()->attribute->type,
);
}
public function test_it_rejects_duplicate_variant_combinations(): void
{
$sector = $this->createAttribute('sector');
$eventDateAttribute = $this->createAttribute('event_date', FieldType::EventDate);
$eventDate = $this->tenant->eventDates()->create([
'date' => '2026-10-09',
'time_start' => '09:00',
@@ -142,7 +152,7 @@ class CatalogServiceTest extends TestCase
'slug' => 'duplicate-entry',
'nombre' => 'Duplicate entry',
'precio' => 100,
'attribute_codes' => [$sector->codigo],
'attribute_codes' => [$eventDateAttribute->codigo, $sector->codigo],
'variants' => [
['event_date_id' => $eventDate->id, 'values' => ['sector' => 'VIP']],
['event_date_id' => $eventDate->id, 'values' => ['sector' => ' vip ']],
@@ -277,13 +287,15 @@ class CatalogServiceTest extends TestCase
]);
}
private function createAttribute(string $code): Attribute
{
private function createAttribute(
string $code,
FieldType $type = FieldType::String,
): Attribute {
return Attribute::query()->create([
'tenant_codigo' => $this->tenant->codigo,
'codigo' => $code,
'nombre' => ucfirst($code),
'type' => FieldType::String,
'type' => $type,
]);
}
}

View File

@@ -15,6 +15,7 @@ use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\FeaturedGroup;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Event\Models\EventDate;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Enums\ValidityTimeType;
use Database\Seeders\AttributeSeeder;
@@ -71,13 +72,15 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
->get();
$this->assertSame(
['servicio', 'color', 'horario', 'talle'],
['event_date', 'servicio', 'color', 'horario', 'talle'],
$attributes->pluck('codigo')->all(),
);
$this->assertSame(['Comedor', 'Vianda'], $attributes[0]->options->pluck('value')->all());
$this->assertSame(['Verde', 'Blanco'], $attributes[1]->options->pluck('value')->all());
$this->assertSame(['Desayuno', 'Almuerzo', 'Cena'], $attributes[2]->options->pluck('value')->all());
$this->assertSame(['14', 'S', 'M', 'L', 'XL', 'XXL'], $attributes[3]->options->pluck('value')->all());
$this->assertSame(FieldType::EventDate, $attributes[0]->type);
$this->assertEmpty($attributes[0]->options);
$this->assertSame(['Comedor', 'Vianda'], $attributes[1]->options->pluck('value')->all());
$this->assertSame(['Verde', 'Blanco'], $attributes[2]->options->pluck('value')->all());
$this->assertSame(['Desayuno', 'Almuerzo', 'Cena'], $attributes[3]->options->pluck('value')->all());
$this->assertSame(['14', 'S', 'M', 'L', 'XL', 'XXL'], $attributes[4]->options->pluck('value')->all());
$this->assertSame(
[
@@ -85,7 +88,7 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
['Almuerzo', ValidityTimeType::TimeWindow, '12:00:00', '15:00:00'],
['Cena', ValidityTimeType::TimeWindow, '20:00:00', '24:00:00'],
],
$attributes[2]->options
$attributes[3]->options
->map(fn ($option): array => [
$option->value,
$option->validityTime->type,
@@ -109,7 +112,7 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
$this->assertNull($generalAdmission->inventory_id);
$this->assertSame(EventProductType::Entry, $generalAdmission->event_product_type);
$this->assertCount(0, $generalAdmission->itemAttributes);
$this->assertCount(1, $generalAdmission->itemAttributes);
$this->assertCount(4, $generalAdmission->variants);
$this->assertEqualsCanonicalizing(
['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'],