feat: add support for multiple event dates in variants

- Updated the FeaturedGroupService to include 'variants.eventDates' in the items query.
- Introduced a BelongsToMany relationship in EventDate for selected variants.
- Modified PurchaseItemResource and PurchaseItemSnapshotFactory to handle multiple event dates for variants.
- Enhanced StartCheckoutService to load event dates for variants.
- Updated TicketGeneratorService to accommodate event dates in ticket generation.
- Created migrations to support multi-value event dates and allow multiple variant values per attribute.
- Adjusted seeders to reflect new event date handling and added new attributes.
- Added tests to ensure correct functionality for multi-date variants and their integration with ticket generation.
This commit is contained in:
2026-08-07 11:57:38 -03:00
parent 91af233941
commit 21b70777f2
22 changed files with 516 additions and 413 deletions

View File

@@ -136,6 +136,42 @@ class CatalogServiceTest extends TestCase
);
}
public function test_it_creates_a_variant_with_multiple_event_dates(): void
{
$eventDateAttribute = $this->createAttribute('event_date', FieldType::EventDate);
$firstDate = $this->tenant->eventDates()->create([
'date' => '2026-10-09',
'time_start' => '00:00:00',
'time_end' => '23:59:59',
]);
$secondDate = $this->tenant->eventDates()->create([
'date' => '2026-10-10',
'time_start' => '00:00:00',
'time_end' => '23:59:59',
]);
$item = $this->service->create([
'tenant_code' => $this->tenant->codigo,
'slug' => 'multi-date-pass',
'nombre' => 'Multi-date pass',
'precio' => 100,
'attribute_codes' => [$eventDateAttribute->codigo],
'multi_select_attribute_codes' => ['event_date'],
'variants' => [[
'real_stock' => 5,
'event_date_ids' => [$secondDate->id, $firstDate->id],
]],
]);
$variant = $item->variants->sole();
$this->assertNull($variant->event_date_id);
$this->assertSame([$firstDate->id, $secondDate->id], $variant->eventDates->pluck('id')->all());
$this->assertSame(
[(string) $firstDate->id, (string) $secondDate->id],
$variant->selectionValues()->get('event_date'),
);
}
public function test_it_rejects_duplicate_variant_combinations(): void
{
$sector = $this->createAttribute('sector');

View File

@@ -4,20 +4,12 @@ namespace Tests\Feature\Seeders;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Enums\CatalogItemType;
use App\Domains\Catalog\Enums\EventProductType;
use App\Domains\Catalog\Enums\FeaturedGroupSource;
use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Enums\ProductLayout;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\CatalogItem;
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;
use Database\Seeders\FiestaFutbolInfantilProductSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -27,21 +19,10 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
{
use RefreshDatabase;
public function test_it_seeds_event_items_for_the_new_catalog(): void
public function test_it_seeds_the_category_free_configurable_catalog(): void
{
$headerLogo = Attachment::query()->create([
'path' => 'tests/header.png',
'filename' => 'header.png',
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
$footerLogo = Attachment::query()->create([
'path' => 'tests/footer.png',
'filename' => 'footer.png',
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
$headerLogo = $this->attachment('header');
$footerLogo = $this->attachment('footer');
$tenant = Tenant::query()->create([
'codigo' => 'fiesta_futbol_infantil',
'nombre' => 'Fiesta Fútbol Infantil',
@@ -56,189 +37,55 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
'footer_logo_id' => $footerLogo->id,
]);
$this->seed([
AttributeSeeder::class,
FiestaFutbolInfantilProductSeeder::class,
]);
$this->seed([
AttributeSeeder::class,
FiestaFutbolInfantilProductSeeder::class,
]);
$attributes = Attribute::query()
->where('tenant_codigo', $tenant->codigo)
->with('options.validityTime')
->orderBy('id')
->get();
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
$this->assertSame(
['event_date', 'servicio', 'color', 'horario', 'talle'],
$attributes->pluck('codigo')->all(),
['color', 'event_date', 'horario', 'servicio', 'talle', 'tipo_alojamiento'],
Attribute::query()->where('tenant_codigo', $tenant->codigo)->orderBy('codigo')->pluck('codigo')->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(0, Category::query()->where('tenant_code', $tenant->codigo)->count());
$this->assertSame(
[
['Desayuno', ValidityTimeType::TimeWindow, '07:00:00', '12:00:00'],
['Almuerzo', ValidityTimeType::TimeWindow, '12:00:00', '15:00:00'],
['Cena', ValidityTimeType::TimeWindow, '20:00:00', '24:00:00'],
],
$attributes[3]->options
->map(fn ($option): array => [
$option->value,
$option->validityTime->type,
$option->validityTime->start_time,
$option->validityTime->end_time,
])
->all(),
['abono', 'alojamiento', 'camiseta', 'comida'],
CatalogItem::query()->where('tenant_code', $tenant->codigo)->orderBy('slug')->pluck('slug')->all(),
);
$tenant->refresh()->load('eventDates');
$this->assertSame('Fiesta Nacional del Fútbol Infantil', $tenant->event_title);
$this->assertSame('Sunchales, Santa Fe', $tenant->event_location);
$this->assertCount(4, $tenant->eventDates);
$this->assertSame(4, EventDate::query()->where('tenant_code', $tenant->codigo)->count());
$generalAdmission = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('slug', 'entrada-general')
->with('variants.eventDate', 'itemAttributes')
->sole();
$this->assertNull($generalAdmission->inventory_id);
$this->assertSame(EventProductType::Entry, $generalAdmission->event_product_type);
$this->assertCount(1, $generalAdmission->itemAttributes);
$this->assertCount(4, $generalAdmission->variants);
$this->assertEqualsCanonicalizing(
['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'],
$generalAdmission->variants
->map(fn ($variant) => $variant->eventDate->date->format('Y-m-d'))
->all()
);
$this->assertSame('2026-10-09 00:00:00', $generalAdmission->validityTime->fixed_starts_at->format('Y-m-d H:i:s'));
$this->assertSame('2026-10-12 23:59:59', $generalAdmission->validityTime->fixed_expires_at->format('Y-m-d H:i:s'));
$standardItems = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('type', CatalogItemType::Standard->value)
->get();
$this->assertCount(7, $standardItems);
foreach ($standardItems as $standardItem) {
$this->assertSame(
'2026-10-09 00:00:00',
$standardItem->validityTime->fixed_starts_at->format('Y-m-d H:i:s'),
);
$this->assertSame(
'2026-10-12 23:59:59',
$standardItem->validityTime->fixed_expires_at->format('Y-m-d H:i:s'),
);
$expectedVariantCounts = [
'camiseta' => 12,
'alojamiento' => 2,
'comida' => 24,
'abono' => 15,
];
foreach ($expectedVariantCounts as $slug => $count) {
$item = CatalogItem::query()->where('tenant_code', $tenant->codigo)->where('slug', $slug)->sole();
$this->assertCount($count, $item->variants);
$this->assertNull($item->category_id);
}
$allDaysItem = CatalogItem::query()
$abono = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('nombre', 'Entrada General - Todos los días')
->with('bundleComponents.variant.eventDate')
->where('slug', 'abono')
->with('itemAttributes.attribute', 'variants.eventDates')
->sole();
$this->assertSame(CatalogItemType::Bundle, $allDaysItem->type);
$this->assertSame('40000.00', $allDaysItem->precio);
$this->assertNull($allDaysItem->inventory_id);
$this->assertFalse($allDaysItem->has_tickets);
$this->assertSame(EventProductType::Entry, $allDaysItem->event_product_type);
$this->assertCount(4, $allDaysItem->bundleComponents);
$dateAttribute = $abono->itemAttributes->firstWhere('attribute.codigo', 'event_date');
$this->assertTrue($dateAttribute->allow_multi_select);
$this->assertEqualsCanonicalizing(
['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'],
$allDaysItem->bundleComponents
->map(fn ($component) => $component->variant->eventDate->date->format('Y-m-d'))
->all()
[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4],
$abono->variants->map(fn ($variant): int => $variant->eventDates->count())->all(),
);
$foodCombo = CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('nombre', 'Combo 2 Panchos + 2 Hamburguesas')
->with('bundleComponents.catalogItem')
->sole();
$featuredGroup = FeaturedGroup::query()->where('tenant_code', $tenant->codigo)->sole();
$this->assertSame(FeaturedGroupSource::All, $featuredGroup->source_type);
$this->assertNull($featuredGroup->category_id);
}
$this->assertSame(CatalogItemType::Bundle, $foodCombo->type);
$this->assertSame('24000.00', $foodCombo->precio);
$this->assertNull($foodCombo->inventory_id);
$this->assertSame(EventProductType::Product, $foodCombo->event_product_type);
$this->assertSame(
[
'hamburguesa-papa-frita' => 2,
'pancho' => 2,
],
$foodCombo->bundleComponents
->mapWithKeys(fn ($component): array => [
$component->catalogItem->slug => $component->quantity,
])
->sortKeys()
->all()
);
$this->assertSame(9, CatalogItem::query()->where('tenant_code', $tenant->codigo)->count());
$this->assertSame(10, Inventory::query()->count());
$this->assertSame(
['Alojamientos', 'Comida', 'Entradas', 'Merchandising'],
Category::query()
->where('tenant_code', $tenant->codigo)
->orderBy('nombre')
->pluck('nombre')
->all(),
);
$this->assertSame(
'Merchandising',
CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->where('slug', 'remera-oficial')
->with('category')
->sole()
->category
->nombre,
);
$featuredGroups = FeaturedGroup::query()
->where('tenant_code', $tenant->codigo)
->with('category.catalogItems')
->orderBy('group_order')
->get();
$this->assertSame(
[
'Entradas' => ['entrada-general', 'entrada-general-todos-los-dias'],
'Alojamientos' => ['alojamiento-por-noche', 'alojamiento-fin-de-semana'],
'Merchandising' => ['remera-oficial', 'gorra-oficial'],
'Comida' => ['hamburguesa-papa-frita', 'pancho', 'combo-2-panchos-2-hamburguesas'],
],
$featuredGroups
->mapWithKeys(fn (FeaturedGroup $group): array => [
$group->group_name => $group->category->catalogItems->pluck('slug')->all(),
])
->all()
);
$this->assertSame(
[
['Entradas', FeaturedGroupSource::Category, 'Entradas', ProductLayout::Row, GroupLayout::SimpleVertical, 0],
['Alojamientos', FeaturedGroupSource::Category, 'Alojamientos', ProductLayout::ColumnWithCart, GroupLayout::Simple, 1],
['Merchandising', FeaturedGroupSource::Category, 'Merchandising', ProductLayout::ColumnWithCart, GroupLayout::Simple, 2],
['Comida', FeaturedGroupSource::Category, 'Comida', ProductLayout::ColumnWithCart, GroupLayout::Simple, 3],
],
$featuredGroups
->map(fn (FeaturedGroup $group): array => [
$group->group_name,
$group->source_type,
$group->category->nombre,
$group->product_layout,
$group->group_layout,
$group->group_order,
])
->all()
);
private function attachment(string $name): Attachment
{
return Attachment::query()->create([
'path' => "tests/{$name}.png",
'filename' => "{$name}.png",
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
}
}

View File

@@ -235,6 +235,31 @@ class TicketGeneratorServiceTest extends TestCase
$this->assertSame('2026-08-21 02:00:00', $fixedWindow->fixed_expires_at->format('Y-m-d H:i:s'));
}
public function test_a_multi_date_variant_generates_one_ticket_for_each_selected_date(): void
{
$item = $this->createTicketableItem('multi-date-pass');
$dates = collect(['2026-08-20', '2026-08-21'])->map(fn (string $date) => EventDate::query()->create([
'tenant_code' => $this->tenant->codigo,
'date' => $date,
'time_start' => '00:00:00',
'time_end' => '23:59:59',
]));
$variant = $item->variants()->create([
'inventory_id' => Inventory::query()->create()->id,
]);
$variant->eventDates()->sync($dates->pluck('id'));
$tickets = $this->service->generate($item, $this->user, 1, $variant->id);
$tickets->each->loadMissing('validityTime');
$this->assertCount(2, $tickets);
$this->assertSame(
['2026-08-20 00:00:00', '2026-08-21 00:00:00'],
$tickets->map(fn ($ticket): string => $ticket->validityTime->fixed_starts_at->format('Y-m-d H:i:s'))->all(),
);
$this->assertSame([$variant->id], $tickets->pluck('source_variant_id')->unique()->values()->all());
}
public function test_marking_a_purchase_as_paid_ignores_items_without_tickets(): void
{
Event::fake([TicketsAvailable::class]);