feat(ticket): implement ticket generation policies and validity time management

- Added `ticket_generation_policy` column to `catalog_items` table to manage ticket generation strategies.
- Created `ticket_validity_times` table to allow multiple validity times per ticket.
- Introduced `validity_time_id` column in `event_dates` table to associate event dates with validity times.
- Established `ticket_validity_groups` and `ticket_validity_group_times` tables to group validity times for tickets.
- Updated seeder to set default ticket generation policy for specific tenant.
- Enhanced tests to cover new functionality, including ticket generation based on event dates and validity times.
- Refactored ticket validity checks to accommodate multiple validity times in a group.
This commit is contained in:
2026-08-11 12:29:32 -03:00
parent 2115d2384d
commit 5197862a62
41 changed files with 950 additions and 102 deletions

View File

@@ -6,10 +6,12 @@ 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\Enums\TicketGenerationPolicy;
use App\Domains\Ticket\Exceptions\TicketGenerationException;
use App\Domains\Ticket\Models\Ticket;
use App\Domains\Ticket\Models\TicketValidityGroup;
use App\Domains\Ticket\Models\ValidityTime;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
@@ -36,25 +38,22 @@ class TicketGeneratorService
$quantity,
$sourceVariantId,
);
$fixedValidityTimes = [];
return $targets->map(function (array $target) use (
&$fixedValidityTimes,
$sourcePurchaseId,
$user,
): Ticket {
$item = $target['catalog_item'];
$variant = $target['variant'];
$eventDate = $target['event_date'];
$validityTime = $this->resolveValidityTime($item, $variant);
$validityTime = $this->materializeEventDateValidityTime(
$validityGroups = $this->buildTicketValidityGroups(
$item,
$variant,
$eventDate,
$validityTime,
$fixedValidityTimes,
$this->resolveValidityTime($item, $variant),
);
return Ticket::query()->create([
$ticket = Ticket::query()->create([
'tenant_code' => $item->tenant_code,
'ticket' => (string) Str::uuid(),
'name' => $this->ticketName($item, $variant, $eventDate),
@@ -62,10 +61,28 @@ class TicketGeneratorService
'source_purchase_id' => $sourcePurchaseId,
'source_catalog_item_id' => $item->getKey(),
'source_variant_id' => $variant?->getKey(),
'validity_time_id' => $validityTime?->getKey(),
'used_at' => null,
'user_id' => $user->getKey(),
]);
$groups = $validityGroups->map(function (Collection $validityTimes) use ($ticket): TicketValidityGroup {
$group = $ticket->validityGroups()->create();
$group->validityTimes()->attach(
$validityTimes
->map(fn (ValidityTime $validityTime): int => $validityTime->getKey())
->all()
);
$group->setRelation(
'validityTimes',
new EloquentCollection($validityTimes->all()),
);
return $group;
});
$ticket->setRelation('validityGroups', new EloquentCollection($groups->all()));
return $ticket;
});
});
}
@@ -125,8 +142,21 @@ class TicketGeneratorService
]);
}
$variant->loadMissing(['eventDates', 'eventDate']);
$variant->loadMissing(['eventDates.validityTime', 'eventDate.validityTime']);
$selectedEventDates = $variant->selectedEventDates();
if ($catalogItem->ticket_generation_policy === TicketGenerationPolicy::OnePerUnit) {
$eventDate = $selectedEventDates->count() === 1
? $selectedEventDates->first()
: null;
return Collection::times($quantity, fn (): array => [
'catalog_item' => $catalogItem,
'variant' => $variant,
'event_date' => $eventDate,
]);
}
$eventDates = $selectedEventDates->isEmpty()
? collect([null])
: $selectedEventDates;
@@ -246,52 +276,37 @@ class TicketGeneratorService
return $catalogItem->nombre.' ('.$properties->implode(', ').')';
}
/**
* Convert a recurring time window into the fixed date and time purchased by
* the customer. Equal tickets generated together share the same snapshot.
*
* @param array<string, ValidityTime> $fixedValidityTimes
*/
private function materializeEventDateValidityTime(
/** @return Collection<int, Collection<int, ValidityTime>> */
private function buildTicketValidityGroups(
CatalogItem $catalogItem,
?Variant $variant,
?EventDate $eventDate,
?ValidityTime $validityTime,
array &$fixedValidityTimes,
): ?ValidityTime {
if (
$variant === null
|| $eventDate === null
) {
return $validityTime;
}
if ($validityTime !== null && $validityTime->type !== ValidityTimeType::TimeWindow) {
return $validityTime;
}
$cacheKey = $eventDate->getKey().':'.($validityTime?->getKey() ?? 'event');
if (isset($fixedValidityTimes[$cacheKey])) {
return $fixedValidityTimes[$cacheKey];
}
$startsAt = $validityTime?->startsAt($eventDate->date) ?? $eventDate->startsAt();
$expiresAt = $validityTime?->expiresAt($eventDate->date) ?? $eventDate->endsAt();
): Collection {
$eventDates = collect([$eventDate]);
if (
$startsAt !== null
&& $expiresAt !== null
&& $expiresAt->lessThanOrEqualTo($startsAt)
$catalogItem->ticket_generation_policy === TicketGenerationPolicy::OnePerUnit
&& $variant !== null
) {
$expiresAt = $expiresAt->addDay();
$variant->loadMissing(['eventDates.validityTime', 'eventDate.validityTime']);
$selectedEventDates = $variant->selectedEventDates();
if ($selectedEventDates->isNotEmpty()) {
$eventDates = $selectedEventDates;
}
}
return $fixedValidityTimes[$cacheKey] = ValidityTime::query()->create([
'type' => ValidityTimeType::FixedWindow,
'start_time' => null,
'end_time' => null,
'fixed_starts_at' => $startsAt,
'fixed_expires_at' => $expiresAt,
]);
return $eventDates
->map(function (?EventDate $date) use ($validityTime): Collection {
$date?->loadMissing('validityTime');
return collect([$date?->validityTime, $validityTime])
->filter()
->unique(fn (ValidityTime $time): int => $time->getKey())
->values();
})
->filter(fn (Collection $group): bool => $group->isNotEmpty())
->values();
}
}