feat(ticket): implement EntryReservationService for managing ticket reservations

This commit is contained in:
2026-10-02 11:50:39 -03:00
parent 47b2e8e030
commit ab5bc43760
2 changed files with 760 additions and 0 deletions

View File

@@ -0,0 +1,469 @@
<?php
namespace Tests\Feature\Ticket;
use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Core\Auth\Models\User;
use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Domains\Ticketing\Ticket\Services\EntryReservationService;
use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity;
use App\Domains\Ticketing\Ticket\Services\TicketGeneratorService;
use App\Domains\Ticketing\Ticket\Services\TicketValidityResolver;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Mockery;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\TestCase;
/** Uses real transactions on the guarded SQLite :memory: connection. */
class EntryReservationServiceTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
// Minimal domain schema isolates this workflow from unrelated legacy migrations.
Schema::create('users', function (Blueprint $table): void {
$table->id();
$table->string('tenant_codigo');
$table->softDeletes();
});
Schema::create('tenants', function (Blueprint $table): void {
$table->id();
$table->string('codigo')->unique();
$table->unsignedBigInteger('active_event_id')->nullable();
});
Schema::create('catalog_items', function (Blueprint $table): void {
$table->id();
$table->string('tenant_code');
$table->string('slug');
$table->string('nombre')->default('Entrada');
$table->string('type')->default('standard');
$table->unsignedBigInteger('inventory_id')->nullable();
$table->string('inventory_policy');
$table->decimal('precio', 10, 2);
$table->timestamp('sales_end_at')->nullable();
$table->softDeletes();
$table->boolean('has_tickets')->default(true);
$table->unsignedBigInteger('event_id')->nullable();
});
Schema::create('inventories', function (Blueprint $table): void {
$table->id();
foreach (['real_stock', 'reserved_stock', 'sold_units', 'refunded_units'] as $column) {
$table->integer($column)->default(0);
}
});
Schema::create('variantes', function (Blueprint $table): void {
$table->id();
$table->foreignId('catalog_item_id');
$table->foreignId('inventory_id');
$table->decimal('precio', 10, 2);
$table->unsignedBigInteger('event_date_id')->nullable();
$table->unsignedBigInteger('replaced_by_variant_id')->nullable();
$table->timestamp('sales_disabled_at')->nullable();
$table->softDeletes();
});
Schema::create('event_dates', function (Blueprint $table): void {
$table->id();
$table->date('date');
$table->time('time_start')->nullable();
$table->unsignedBigInteger('rescheduled_to_event_date_id')->nullable();
$table->timestamp('suspended_at')->nullable();
});
Schema::create('attribute', function (Blueprint $table): void {
$table->id();
$table->string('codigo');
$table->string('type')->default('string');
});
Schema::create('item_attributes', function (Blueprint $table): void {
$table->id();
$table->foreignId('catalog_item_id');
$table->foreignId('attribute_id');
$table->boolean('allow_multi_select')->default(false);
$table->integer('sort_order')->default(0);
});
Schema::create('attribute_options', function (Blueprint $table): void {
$table->id();
$table->foreignId('attribute_id');
$table->string('value');
$table->string('label');
$table->integer('sort_order')->default(0);
});
Schema::create('variant_values', function (Blueprint $table): void {
$table->id();
$table->foreignId('variant_id');
$table->foreignId('item_attribute_id');
$table->string('value');
});
Schema::create('variant_event_dates', function (Blueprint $table): void {
$table->foreignId('variant_id');
$table->foreignId('event_date_id');
});
Schema::create('tickets', function (Blueprint $table): void {
$table->id();
$table->foreignId('user_id');
$table->foreignId('source_variant_id')->nullable();
$table->string('tenant_code')->nullable();
$table->uuid('ticket')->nullable()->unique();
$table->unsignedBigInteger('source_catalog_item_id')->nullable();
$table->unsignedBigInteger('source_purchase_item_id')->nullable();
$table->unsignedBigInteger('event_id')->nullable();
$table->timestamp('used_at')->nullable();
$table->timestamp('disabled_at')->nullable();
$table->timestamp('cancelled_at')->nullable();
$table->timestamp('refunded_at')->nullable();
});
Schema::create('value_changes', function (Blueprint $table): void {
$table->id();
$table->morphs('trackable');
$table->string('tenant_code');
$table->string('attribute');
$table->text('old_value')->nullable();
$table->text('new_value')->nullable();
$table->timestamp('changed_at');
$table->string('actor_type');
$table->foreignId('user_id')->nullable();
});
(require database_path('migrations/2026_09_23_010000_create_desfile_entry_reservations_table.php'))->up();
(require database_path('migrations/2026_09_24_000000_add_administrative_entry_reservation_stock.php'))->up();
(require database_path('migrations/2026_09_24_010000_add_soft_deletes_to_desfile_entry_reservations_table.php'))->up();
(require database_path('migrations/2026_10_02_000000_generalize_ticket_reservations.php'))->up();
DB::table('tenants')->insert(['codigo' => 'ticket_shop']);
DB::table('users')->insert(['id' => 1, 'tenant_codigo' => 'ticket_shop']);
DB::table('catalog_items')->insert([
'id' => 1, 'tenant_code' => 'ticket_shop', 'slug' => 'entrada',
'inventory_policy' => 'tracked', 'precio' => 100,
]);
foreach ([1, 2] as $id) {
DB::table('inventories')->insert(['id' => $id, 'real_stock' => 3]);
DB::table('variantes')->insert(['id' => $id, 'catalog_item_id' => 1, 'inventory_id' => $id, 'precio' => 250]);
}
DB::table('tenants')->insert(['codigo' => 'other']);
DB::table('users')->insert(['id' => 2, 'tenant_codigo' => 'other']);
DB::table('inventories')->insert(['id' => 3, 'real_stock' => 3]);
DB::table('catalog_items')->insert([
['id' => 2, 'tenant_code' => 'ticket_shop', 'slug' => 'general', 'inventory_policy' => 'tracked', 'inventory_id' => 3, 'precio' => 700],
['id' => 3, 'tenant_code' => 'other', 'slug' => 'other', 'inventory_policy' => 'unlimited', 'inventory_id' => null, 'precio' => 900],
['id' => 4, 'tenant_code' => 'ticket_shop', 'slug' => 'unlimited', 'inventory_policy' => 'unlimited', 'inventory_id' => null, 'precio' => 500],
]);
foreach (['tipo' => 'NORMAL', 'sector' => 'A', 'fila' => '3', 'asiento' => '17'] as $code => $value) {
$attributeId = DB::table('attribute')->insertGetId(['codigo' => $code]);
$itemAttributeId = DB::table('item_attributes')->insertGetId([
'catalog_item_id' => 1,
'attribute_id' => $attributeId,
]);
DB::table('attribute_options')->insert([
'attribute_id' => $attributeId,
'value' => $value,
'label' => $value,
]);
foreach ([1, 2] as $variantId) {
DB::table('variant_values')->insert([
'variant_id' => $variantId, 'item_attribute_id' => $itemAttributeId, 'value' => $value,
]);
}
}
}
private function service(bool $failSecond = false): EntryReservationService
{
$generator = Mockery::mock(TicketGeneratorService::class);
$generator->shouldReceive('generate')->andReturnUsing(function ($item, $user, $quantity, $variantId) use ($failSecond) {
if ($failSecond && $item->id === 2) {
throw new \RuntimeException('Ticket generation failed');
}
return collect([Ticket::create([
'tenant_code' => $item->tenant_code, 'user_id' => $user->id,
'source_catalog_item_id' => $item->id, 'source_variant_id' => $variantId,
'ticket' => (string) Str::uuid(),
])]);
});
return new EntryReservationService($generator);
}
private function rows(): array
{
return [
['catalog_item_id' => 1, 'variant_id' => 1, 'tipo_pago' => 'sin_cargo'],
['catalog_item_id' => 2, 'tipo_pago' => 'otro_metodo'],
];
}
public function test_reserves_multiple_items_with_and_without_variants_and_replays_the_batch(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$key = (string) Str::uuid();
$result = $service->reserve($user, $key, $this->rows());
$this->assertSame([1, 2], $result->pluck('catalog_item_id')->all());
$this->assertSame([1, null], $result->pluck('variant_id')->all());
$this->assertSame(['0.00', '700.00'], $result->pluck('importe')->all());
$replay = $service->reserve($user, $key, array_reverse($this->rows()));
$this->assertSame($result->pluck('id')->all(), $replay->pluck('id')->all());
$this->assertDatabaseCount('tickets', 2);
$this->assertDatabaseCount('entry_reservations', 2);
$this->assertDatabaseCount('reservation_batches', 1);
$this->assertSame(1, Inventory::findOrFail(1)->entry_reserved_stock);
$this->assertSame(1, Inventory::findOrFail(3)->entry_reserved_stock);
}
public function test_repeated_variants_use_stock_instead_of_assuming_a_unique_seat(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$row = $this->rows()[0];
$service->reserve($user, (string) Str::uuid(), [$row, $row]);
$service->reserve($user, (string) Str::uuid(), [$row]);
$this->assertSame(3, Inventory::findOrFail(1)->entry_reserved_stock);
$this->expectException(ValidationException::class);
$service->reserve($user, (string) Str::uuid(), [$row]);
}
public function test_shared_inventory_is_checked_for_the_entire_batch(): void
{
DB::table('catalog_items')->where('id', 2)->update(['inventory_id' => 1]);
DB::table('inventories')->where('id', 1)->update(['real_stock' => 1]);
try {
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), $this->rows());
$this->fail('Expected insufficient shared stock');
} catch (ValidationException $error) {
$this->assertSame(['rows.0.variant_id', 'rows.1.catalog_item_id'], array_keys($error->errors()));
}
$this->assertDatabaseCount('tickets', 0);
$this->assertDatabaseCount('reservation_batches', 0);
$this->assertSame(0, (int) Inventory::sum('entry_reserved_stock'));
}
public function test_ticket_failure_rolls_back_the_entire_batch(): void
{
try {
$this->service(true)->reserve(User::findOrFail(1), (string) Str::uuid(), $this->rows());
$this->fail('Expected ticket failure');
} catch (\RuntimeException $error) {
$this->assertSame('Ticket generation failed', $error->getMessage());
}
$this->assertDatabaseCount('tickets', 0);
$this->assertDatabaseCount('entry_reservations', 0);
$this->assertDatabaseCount('reservation_batches', 0);
$this->assertSame(0, (int) Inventory::sum('entry_reserved_stock'));
}
public function test_an_item_from_another_tenant_is_rejected(): void
{
$this->expectException(ValidationException::class);
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), [
['catalog_item_id' => 3, 'tipo_pago' => 'sin_cargo'],
]);
}
public function test_a_variant_from_another_item_is_rejected(): void
{
$this->expectException(ValidationException::class);
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), [
['catalog_item_id' => 2, 'variant_id' => 1, 'tipo_pago' => 'sin_cargo'],
]);
}
public function test_a_variant_is_required_when_the_item_has_variants(): void
{
$this->expectException(ValidationException::class);
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), [
['catalog_item_id' => 1, 'tipo_pago' => 'sin_cargo'],
]);
}
public function test_disabled_variants_and_items_without_tickets_are_rejected(): void
{
DB::table('variantes')->where('id', 1)->update(['sales_disabled_at' => now()]);
DB::table('catalog_items')->where('id', 2)->update(['has_tickets' => false]);
try {
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), $this->rows());
$this->fail('Expected unavailable selection');
} catch (ValidationException $error) {
$this->assertSame(['rows.0.variant_id', 'rows.1.catalog_item_id'], array_keys($error->errors()));
}
$this->assertDatabaseCount('entry_reservations', 0);
}
public function test_the_active_event_limits_the_catalog_for_new_reservations(): void
{
DB::table('tenants')->where('codigo', 'ticket_shop')->update(['active_event_id' => 10]);
DB::table('catalog_items')->where('id', 1)->update(['event_id' => 10]);
DB::table('catalog_items')->where('id', 2)->update(['event_id' => 20]);
$this->expectException(ValidationException::class);
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), $this->rows());
}
public function test_unlimited_items_can_emit_tickets_without_inventory(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$reservation = $service->reserve($user, (string) Str::uuid(), [
['catalog_item_id' => 4, 'tipo_pago' => 'otro_metodo'],
])->sole();
$this->assertNull($reservation->inventory_id);
$this->assertNull($reservation->variant_id);
$this->assertSame('500.00', $reservation->importe);
$service->cancel($user, $reservation->id);
$this->assertSoftDeleted('entry_reservations', ['id' => $reservation->id]);
}
public function test_filters_and_exports_are_scoped_by_tenant_and_catalog_item(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$service->reserve($user, (string) Str::uuid(), $this->rows());
$service->reserve(User::findOrFail(2), (string) Str::uuid(), [
['catalog_item_id' => 3, 'tipo_pago' => 'sin_cargo'],
]);
$this->assertSame(2, $service->reservations($user)->total());
$this->assertSame(1, $service->reservations($user, ['catalog_item_id' => 2, 'tipo_pago' => 'otro_metodo'])->total());
$this->assertSame(1, $service->reservations($user, ['variant_id' => 1, 'per_page' => 1])->total());
$this->assertSame(2, $service->reservationsForExport($user)->count());
$this->assertSame(2, $service->reservationsForExport($user, ['catalog_item_id' => 2])->sole()->catalog_item_id);
$this->assertSame(0, $service->reservations($user, ['catalog_item_id' => 3])->total());
}
public function test_cancel_releases_stock_and_cancels_the_ticket_without_a_variant(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$reservation = $service->reserve($user, (string) Str::uuid(), [$this->rows()[1]])->sole();
$service->cancel($user, $reservation->id, 2);
$this->assertSoftDeleted('entry_reservations', ['id' => $reservation->id]);
$this->assertNotNull(Ticket::findOrFail($reservation->ticket_id)->cancelled_at);
$this->assertSame(0, Inventory::findOrFail(3)->entry_reserved_stock);
$this->assertSame(0, $service->reservations($user)->total());
}
public function test_another_tenant_cannot_cancel_a_reservation(): void
{
$service = $this->service();
$reservation = $service->reserve(User::findOrFail(1), (string) Str::uuid(), [$this->rows()[0]])->sole();
$this->expectException(ModelNotFoundException::class);
$service->cancel(User::findOrFail(2), $reservation->id);
}
public function test_cancellation_can_be_restricted_to_one_catalog_item_by_an_adapter(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$reservation = $service->reserve($user, (string) Str::uuid(), [$this->rows()[0]])->sole();
$this->expectException(ModelNotFoundException::class);
$service->cancel($user, $reservation->id, 2);
}
public function test_cancellation_does_not_release_stock_for_a_used_ticket(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$reservation = $service->reserve($user, (string) Str::uuid(), [$this->rows()[0]])->sole();
Ticket::whereKey($reservation->ticket_id)->update(['used_at' => now()]);
try {
$service->cancel($user, $reservation->id);
$this->fail('Expected active ticket requirement');
} catch (ValidationException $error) {
$this->assertArrayHasKey('status', $error->errors());
}
$this->assertSame(1, Inventory::findOrFail(1)->entry_reserved_stock);
$this->assertDatabaseHas('entry_reservations', ['id' => $reservation->id, 'deleted_at' => null]);
}
public function test_a_reused_key_with_a_different_item_is_rejected(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$key = (string) Str::uuid();
$service->reserve($user, $key, [$this->rows()[1]]);
try {
$service->reserve($user, $key, [['catalog_item_id' => 4, 'tipo_pago' => 'otro_metodo']]);
$this->fail('Expected idempotency conflict');
} catch (HttpException $error) {
$this->assertSame(409, $error->getStatusCode());
}
$this->assertDatabaseCount('tickets', 1);
}
public function test_clients_cannot_supply_the_price(): void
{
$this->expectException(ValidationException::class);
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), [
[...$this->rows()[0], 'importe' => 10],
]);
}
public function test_old_variant_only_batch_hashes_replay_including_cancelled_history(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$key = (string) Str::uuid();
$rows = [
['catalog_item_id' => 1, 'variant_id' => 1, 'tipo_pago' => 'sin_cargo'],
['catalog_item_id' => 1, 'variant_id' => 2, 'tipo_pago' => 'otro_metodo'],
];
$result = $service->reserve($user, $key, $rows);
$legacyRows = array_map(fn ($row) => ['variant_id' => $row['variant_id'], 'tipo_pago' => $row['tipo_pago']], $rows);
DB::table('reservation_batches')->update([
'request_hash' => hash('sha256', json_encode($legacyRows, JSON_THROW_ON_ERROR)),
]);
$service->cancel($user, $result->first()->id);
$replay = $service->reserve($user, $key, array_reverse($rows));
$this->assertSame($result->last()->id, $replay->sole()->id);
$this->assertDatabaseCount('tickets', 2);
$this->assertDatabaseCount('reservation_batches', 1);
$this->assertSame(0, Inventory::findOrFail(1)->entry_reserved_stock);
$this->assertSame(1, Inventory::findOrFail(2)->entry_reserved_stock);
}
public function test_old_batch_hashes_cannot_replay_with_a_different_catalog_item(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$key = (string) Str::uuid();
$service->reserve($user, $key, [$this->rows()[0]]);
DB::table('reservation_batches')->update([
'request_hash' => hash('sha256', json_encode([
['variant_id' => 1, 'tipo_pago' => 'sin_cargo'],
], JSON_THROW_ON_ERROR)),
]);
try {
$service->reserve($user, $key, [
['catalog_item_id' => 2, 'variant_id' => 1, 'tipo_pago' => 'sin_cargo'],
]);
$this->fail('Expected legacy idempotency conflict');
} catch (HttpException $error) {
$this->assertSame(409, $error->getStatusCode());
}
$this->assertDatabaseCount('tickets', 1);
}
public function test_ticket_download_selection_uses_tenant_and_catalog_item_scope(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$reservation = $service->reserve($user, (string) Str::uuid(), [$this->rows()[1]])->sole();
$this->assertSame($reservation->ticket_id, $service->reservationTicket($user, $reservation->id, 2)->id);
$this->expectException(ModelNotFoundException::class);
$service->reservationTicket(User::findOrFail(2), $reservation->id);
}
public function test_real_ticket_generator_links_each_catalog_item_and_optional_variant(): void
{
$validity = Mockery::mock(TicketValidityResolver::class);
$validity->shouldReceive('resolveVariant')->andReturn(ResolvedTicketValidity::unrestricted());
$service = new EntryReservationService(new TicketGeneratorService($validity));
$result = $service->reserve(User::findOrFail(1), (string) Str::uuid(), $this->rows());
foreach ($result as $reservation) {
$this->assertDatabaseHas('tickets', [
'id' => $reservation->ticket_id, 'tenant_code' => 'ticket_shop',
'source_catalog_item_id' => $reservation->catalog_item_id,
'source_variant_id' => $reservation->variant_id, 'source_purchase_item_id' => null,
]);
}
}
}