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

@@ -5,6 +5,7 @@ namespace Tests\Unit\Event;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Event\Models\EventDate;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Models\ValidityTime;
use Tests\TestCase;
class EventModelsTest extends TestCase
@@ -14,6 +15,7 @@ class EventModelsTest extends TestCase
$eventDate = new EventDate;
$eventDate->setRawAttributes([
'tenant_code' => 'acme',
'validity_time_id' => '12',
'date' => '2026-10-09',
'time_start' => '09:00:00',
'time_end' => '18:30:00',
@@ -21,9 +23,12 @@ class EventModelsTest extends TestCase
$this->assertFalse($eventDate->usesTimestamps());
$this->assertSame('acme', $eventDate->tenant_code);
$this->assertSame(12, $eventDate->validity_time_id);
$this->assertSame('2026-10-09 09:00:00', $eventDate->startsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-10-09 18:30:00', $eventDate->endsAt()->format('Y-m-d H:i:s'));
$this->assertInstanceOf(Tenant::class, $eventDate->tenant()->getRelated());
$this->assertInstanceOf(ValidityTime::class, $eventDate->validityTime()->getRelated());
$this->assertInstanceOf(EventDate::class, (new ValidityTime)->eventDate()->getRelated());
$this->assertInstanceOf(Variant::class, $eventDate->variants()->getRelated());
}

View File

@@ -8,7 +8,9 @@ use App\Domains\Catalog\Models\Variant;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Enums\ValidityTimeType;
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\Carbon;
use Tests\TestCase;
@@ -27,7 +29,6 @@ class TicketTest extends TestCase
$ticket->setRawAttributes([
'source_catalog_item_id' => '20',
'source_variant_id' => '30',
'validity_time_id' => '40',
'used_at' => null,
'scanner_user_id' => '15',
'user_id' => '10',
@@ -37,7 +38,6 @@ class TicketTest extends TestCase
$this->assertFalse($ticket->usesTimestamps());
$this->assertSame(20, $ticket->source_catalog_item_id);
$this->assertSame(30, $ticket->source_variant_id);
$this->assertSame(40, $ticket->validity_time_id);
$this->assertNull($ticket->used_at);
$this->assertSame(15, $ticket->scanner_user_id);
$this->assertSame(10, $ticket->user_id);
@@ -46,7 +46,8 @@ class TicketTest extends TestCase
$this->assertInstanceOf(User::class, $ticket->scannerUser()->getRelated());
$this->assertInstanceOf(CatalogItem::class, $ticket->sourceCatalogItem()->getRelated());
$this->assertInstanceOf(Variant::class, $ticket->sourceVariant()->getRelated());
$this->assertInstanceOf(ValidityTime::class, $ticket->validityTime()->getRelated());
$this->assertInstanceOf(TicketValidityGroup::class, $ticket->validityGroups()->getRelated());
$this->assertInstanceOf(ValidityTime::class, (new TicketValidityGroup)->validityTimes()->getRelated());
}
public function test_unused_ticket_without_validity_time_is_valid(): void
@@ -73,12 +74,11 @@ class TicketTest extends TestCase
public function test_time_window_is_resolved_for_current_date(): void
{
Carbon::setTestNow('2026-07-21 12:00:00');
$ticket = new Ticket;
$ticket->setRelation('validityTime', new ValidityTime([
$ticket = $this->ticketWithValidityGroups([[new ValidityTime([
'type' => ValidityTimeType::TimeWindow,
'start_time' => '10:00:00',
'end_time' => '14:00:00',
]));
])]]);
$this->assertSame('2026-07-21 10:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-07-21 14:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
@@ -98,6 +98,49 @@ class TicketTest extends TestCase
$this->assertSame(Ticket::STATUS_EXPIRED, $ticket->status);
}
public function test_ticket_is_valid_when_any_validity_time_is_active(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = $this->ticketWithValidityGroups([
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subDays(2),
'fixed_expires_at' => now()->subDay(),
])],
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subHour(),
'fixed_expires_at' => now()->addHour(),
])],
]);
$this->assertTrue($ticket->isValid());
$this->assertFalse($ticket->is_expired);
$this->assertSame('2026-07-19 10:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-07-21 11:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
}
public function test_ticket_is_invalid_but_not_expired_between_validity_times(): void
{
Carbon::setTestNow('2026-07-21 10:00:00');
$ticket = $this->ticketWithValidityGroups([
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->subDays(2),
'fixed_expires_at' => now()->subDay(),
])],
[new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => now()->addDay(),
'fixed_expires_at' => now()->addDays(2),
])],
]);
$this->assertFalse($ticket->isValid());
$this->assertFalse($ticket->is_expired);
$this->assertSame(Ticket::STATUS_ACTIVE, $ticket->status);
}
public function test_used_ticket_is_invalid_and_not_reported_as_expired(): void
{
$ticket = $this->ticketWithValidityTime(new ValidityTime([
@@ -112,10 +155,48 @@ class TicketTest extends TestCase
$this->assertSame(Ticket::STATUS_USED, $ticket->status);
}
public function test_all_validity_times_in_the_same_group_must_be_active(): void
{
Carbon::setTestNow('2026-08-20 13:00:00');
$ticket = $this->ticketWithValidityGroups([[
new ValidityTime([
'type' => ValidityTimeType::FixedWindow,
'fixed_starts_at' => '2026-08-20 09:00:00',
'fixed_expires_at' => '2026-08-20 18:00:00',
]),
new ValidityTime([
'type' => ValidityTimeType::TimeWindow,
'start_time' => '12:00:00',
'end_time' => '15:00:00',
]),
]]);
$this->assertTrue($ticket->isValid());
$this->assertSame('2026-08-20 12:00:00', $ticket->getEffectiveStartsAt()->format('Y-m-d H:i:s'));
$this->assertSame('2026-08-20 15:00:00', $ticket->getEffectiveExpiresAt()->format('Y-m-d H:i:s'));
Carbon::setTestNow('2026-08-20 16:00:00');
$this->assertFalse($ticket->isValid());
$this->assertTrue($ticket->is_expired);
}
private function ticketWithValidityTime(ValidityTime $validityTime): Ticket
{
return $this->ticketWithValidityGroups([[$validityTime]]);
}
/** @param array<int, array<int, ValidityTime>> $validityGroups */
private function ticketWithValidityGroups(array $validityGroups): Ticket
{
$ticket = new Ticket;
$ticket->setRelation('validityTime', $validityTime);
$groups = collect($validityGroups)->map(function (array $validityTimes): TicketValidityGroup {
$group = new TicketValidityGroup;
$group->setRelation('validityTimes', new EloquentCollection($validityTimes));
return $group;
});
$ticket->setRelation('validityGroups', new EloquentCollection($groups->all()));
return $ticket;
}

View File

@@ -5,7 +5,7 @@ namespace Tests\Unit\Ticket;
use App\Domains\Catalog\Models\AttributeOption;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticket\Models\Ticket;
use App\Domains\Ticket\Models\TicketValidityGroup;
use App\Domains\Ticket\Models\ValidityTime;
use Illuminate\Support\Carbon;
use Tests\TestCase;
@@ -26,7 +26,10 @@ class ValidityTimeTest extends TestCase
$this->assertInstanceOf(Carbon::class, $validityTime->fixed_expires_at);
$this->assertInstanceOf(CatalogItem::class, $validityTime->catalogItems()->getRelated());
$this->assertInstanceOf(AttributeOption::class, $validityTime->attributeOptions()->getRelated());
$this->assertInstanceOf(Ticket::class, $validityTime->tickets()->getRelated());
$this->assertInstanceOf(
TicketValidityGroup::class,
$validityTime->ticketValidityGroups()->getRelated(),
);
}
public function test_it_exposes_supported_type_values(): void