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

@@ -5,6 +5,7 @@ namespace App\Domains\Ticket\Services;
use App\Domains\Auth\Models\User;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Event\Models\EventDate;
use App\Domains\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticket\Exceptions\TicketGenerationException;
use App\Domains\Ticket\Models\Ticket;
@@ -44,9 +45,11 @@ class TicketGeneratorService
): Ticket {
$item = $target['catalog_item'];
$variant = $target['variant'];
$eventDate = $target['event_date'];
$validityTime = $this->resolveValidityTime($item, $variant);
$validityTime = $this->materializeEventDateValidityTime(
$variant,
$eventDate,
$validityTime,
$fixedValidityTimes,
);
@@ -68,7 +71,7 @@ class TicketGeneratorService
}
/**
* @return Collection<int, array{catalog_item: CatalogItem, variant: Variant|null}>
* @return Collection<int, array{catalog_item: CatalogItem, variant: Variant|null, event_date: EventDate|null}>
*/
private function resolveTargets(
CatalogItem $catalogItem,
@@ -79,10 +82,7 @@ class TicketGeneratorService
$variant = $this->resolveVariant($catalogItem, $sourceVariantId);
$this->validateTarget($catalogItem, $variant);
return Collection::times($quantity, fn (): array => [
'catalog_item' => $catalogItem,
'variant' => $variant,
]);
return $this->targetsForVariant($catalogItem, $variant, $quantity);
}
$catalogItem->loadMissing([
@@ -100,17 +100,46 @@ class TicketGeneratorService
$variant = $component->variant;
$this->validateTarget($componentItem, $variant);
return Collection::times(
return $this->targetsForVariant(
$componentItem,
$variant,
$quantity * $component->quantity,
fn (): array => [
'catalog_item' => $componentItem,
'variant' => $variant,
],
);
})
->values();
}
/**
* @return Collection<int, array{catalog_item: CatalogItem, variant: Variant|null, event_date: EventDate|null}>
*/
private function targetsForVariant(
CatalogItem $catalogItem,
?Variant $variant,
int $quantity,
): Collection {
if ($variant === null) {
return Collection::times($quantity, fn (): array => [
'catalog_item' => $catalogItem,
'variant' => null,
'event_date' => null,
]);
}
$variant->loadMissing(['eventDates', 'eventDate']);
$selectedEventDates = $variant->selectedEventDates();
$eventDates = $selectedEventDates->isEmpty()
? collect([null])
: $selectedEventDates;
return Collection::times($quantity)
->flatMap(fn () => $eventDates->map(fn (?EventDate $eventDate): array => [
'catalog_item' => $catalogItem,
'variant' => $variant,
'event_date' => $eventDate,
]))
->values();
}
private function resolveVariant(
CatalogItem $catalogItem,
?int $sourceVariantId,
@@ -185,32 +214,29 @@ class TicketGeneratorService
*/
private function materializeEventDateValidityTime(
?Variant $variant,
?EventDate $eventDate,
?ValidityTime $validityTime,
array &$fixedValidityTimes,
): ?ValidityTime {
if (
$variant === null
|| $validityTime === null
|| $validityTime->type !== ValidityTimeType::TimeWindow
|| $eventDate === null
) {
return $validityTime;
}
$variant->loadMissing('eventDate');
$eventDate = $variant->eventDate;
if ($eventDate === null) {
if ($validityTime !== null && $validityTime->type !== ValidityTimeType::TimeWindow) {
return $validityTime;
}
$cacheKey = $eventDate->getKey().':'.$validityTime->getKey();
$cacheKey = $eventDate->getKey().':'.($validityTime?->getKey() ?? 'event');
if (isset($fixedValidityTimes[$cacheKey])) {
return $fixedValidityTimes[$cacheKey];
}
$startsAt = $validityTime->startsAt($eventDate->date);
$expiresAt = $validityTime->expiresAt($eventDate->date);
$startsAt = $validityTime?->startsAt($eventDate->date) ?? $eventDate->startsAt();
$expiresAt = $validityTime?->expiresAt($eventDate->date) ?? $eventDate->endsAt();
if (
$startsAt !== null