Refactor entry reservation handling by generalizing ticket reservations

- Renamed `desfile_entry_reservations` to `entry_reservations` and `desfile_reservation_batches` to `reservation_batches`.
- Updated models, services, and requests to use the new `EntryReservation` model from the `Ticket` domain instead of the deleted `Desfile` model.
- Introduced a new `EntryReservationPaymentType` enum to manage payment types.
- Modified database migrations to ensure data integrity during the transition.
- Updated tests to reflect changes in the database schema and model relationships.
- Ensured that existing functionality remains intact while improving code organization and clarity.
This commit is contained in:
2026-10-02 11:03:00 -03:00
parent 856b9a6706
commit 47b2e8e030
27 changed files with 442 additions and 103 deletions

View File

@@ -136,6 +136,7 @@ class EntryReservationServiceTest extends TestCase
(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' => 'desfile_pura_tendencia']);
DB::table('users')->insert(['id' => 1, 'tenant_codigo' => 'desfile_pura_tendencia']);
DB::table('catalog_items')->insert([
@@ -200,8 +201,8 @@ class EntryReservationServiceTest extends TestCase
$replayed = $service->reserve($user, $key, $this->rows());
$this->assertSame($result->pluck('id')->all(), $replayed->pluck('id')->all());
$this->assertDatabaseCount('tickets', 2);
$this->assertDatabaseCount('desfile_entry_reservations', 2);
$this->assertDatabaseCount('desfile_reservation_batches', 1);
$this->assertDatabaseCount('entry_reservations', 2);
$this->assertDatabaseCount('reservation_batches', 1);
foreach (Inventory::all() as $inventory) {
$this->assertSame(1, $inventory->entry_reserved_stock);
$this->assertSame(1, $inventory->real_stock);
@@ -223,7 +224,7 @@ class EntryReservationServiceTest extends TestCase
$this->assertStringContainsString('Tipo: NORMAL, Sector: A, Fila: 3, Asiento: 17', $error->errors()['rows.1.variant_id'][0]);
}
$this->assertDatabaseCount('tickets', 0);
$this->assertDatabaseCount('desfile_entry_reservations', 0);
$this->assertDatabaseCount('entry_reservations', 0);
$this->assertSame(0, (int) Inventory::sum('entry_reserved_stock'));
}
@@ -236,8 +237,8 @@ class EntryReservationServiceTest extends TestCase
$this->assertSame('Ticket generation failed', $error->getMessage());
}
$this->assertDatabaseCount('tickets', 0);
$this->assertDatabaseCount('desfile_entry_reservations', 0);
$this->assertDatabaseCount('desfile_reservation_batches', 0);
$this->assertDatabaseCount('entry_reservations', 0);
$this->assertDatabaseCount('reservation_batches', 0);
$this->assertSame(0, (int) Inventory::sum('entry_reserved_stock'));
}
@@ -258,7 +259,7 @@ class EntryReservationServiceTest extends TestCase
$service->cancel($user, $reservation->id);
$this->assertSoftDeleted('desfile_entry_reservations', ['id' => $reservation->id]);
$this->assertSoftDeleted('entry_reservations', ['id' => $reservation->id]);
$this->assertNotNull(Ticket::findOrFail($reservation->ticket_id)->cancelled_at);
$this->assertSame(0, Inventory::findOrFail(1)->entry_reserved_stock);
$this->assertSame(1, Inventory::findOrFail(1)->availableStock());
@@ -280,7 +281,7 @@ class EntryReservationServiceTest extends TestCase
$this->assertArrayHasKey('status', $error->errors());
}
$this->assertDatabaseHas('desfile_entry_reservations', [
$this->assertDatabaseHas('entry_reservations', [
'id' => $reservation->id,
'deleted_at' => null,
]);
@@ -443,6 +444,7 @@ class EntryReservationServiceTest extends TestCase
$service = new EntryReservationService(new TicketGeneratorService($validity));
$result = $service->reserve(User::findOrFail(1), (string) Str::uuid(), $this->rows());
foreach ($result as $reservation) {
$this->assertSame(1, $reservation->catalog_item_id);
$this->assertDatabaseHas('tickets', [
'id' => $reservation->ticket_id, 'user_id' => 1,
'source_variant_id' => $reservation->variant_id,

View File

@@ -99,7 +99,7 @@ class AdminAppDesfileEntryReservationFormControllerTest extends TestCase
]);
}
if ($state === 'administrative') {
$variant->desfileEntryReservations()->create([
$variant->entryReservations()->create([
'fecha_reserva' => now(), 'importe' => 0, 'tipo_pago' => 'sin_cargo',
]);
}
@@ -111,7 +111,7 @@ class AdminAppDesfileEntryReservationFormControllerTest extends TestCase
->assertOk()->assertJsonCount(1, 'data.variants')
->assertJsonPath('data.variants.0.id', $availableId)
->assertJsonPath('data.fields.3.options', [['value' => '1', 'label' => '1']]);
$this->assertDatabaseCount('desfile_entry_reservations', 1);
$this->assertDatabaseCount('entry_reservations', 1);
$this->assertSame(1, (int) Inventory::query()->sum('reserved_stock'));
}

View File

@@ -0,0 +1,206 @@
<?php
namespace Tests\Feature\Migrations;
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Models\ReservationBatch;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
/** Tests the upgrade on the guarded SQLite :memory: connection. */
class GeneralizeTicketReservationsTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Schema::create('tenants', function (Blueprint $table): void {
$table->id();
$table->string('codigo');
});
Schema::create('users', function (Blueprint $table): void {
$table->id();
$table->softDeletes();
});
Schema::create('catalog_items', function (Blueprint $table): void {
$table->id();
$table->string('tenant_code');
$table->softDeletes();
});
Schema::create('variantes', function (Blueprint $table): void {
$table->id();
$table->unsignedBigInteger('catalog_item_id');
$table->softDeletes();
});
Schema::create('tickets', fn (Blueprint $table) => $table->id());
Schema::create('inventories', fn (Blueprint $table) => $table->id());
(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();
DB::table('users')->insert(['id' => 1]);
DB::table('tenants')->insert([
['codigo' => 'desfile_pura_tendencia'], ['codigo' => 'other'],
]);
DB::table('catalog_items')->insert([
['id' => 1, 'tenant_code' => 'desfile_pura_tendencia'],
['id' => 2, 'tenant_code' => 'other'],
]);
DB::table('variantes')->insert([
['id' => 1, 'catalog_item_id' => 1, 'deleted_at' => null],
['id' => 2, 'catalog_item_id' => 2, 'deleted_at' => now()],
]);
DB::table('tickets')->insert(['id' => 10]);
DB::table('inventories')->insert(['id' => 20, 'entry_reserved_stock' => 1]);
DB::table('desfile_reservation_batches')->insert([
'id' => 30, 'user_id' => 1, 'tenant_code' => 'desfile_pura_tendencia',
'idempotency_key' => 'aab7c678-7f4d-486a-87ec-7f613ad6f58d',
'request_hash' => str_repeat('a', 64),
'created_at' => now(), 'updated_at' => now(),
]);
DB::table('desfile_entry_reservations')->insert([
['id' => 40, 'variant_id' => 1, 'ticket_id' => 10, 'inventory_id' => 20,
'batch_id' => 30, 'fecha_reserva' => now(), 'importe' => 250,
'tipo_pago' => 'otro_metodo', 'deleted_at' => null],
['id' => 41, 'variant_id' => 2, 'ticket_id' => null, 'inventory_id' => null,
'batch_id' => null, 'fecha_reserva' => now(), 'importe' => 0,
'tipo_pago' => 'sin_cargo', 'deleted_at' => now()],
]);
}
private function migration(): Migration
{
return require database_path('migrations/2026_10_02_000000_generalize_ticket_reservations.php');
}
public function test_upgrade_preserves_reservations_batches_stock_and_deleted_history(): void
{
$this->migration()->up();
$this->assertFalse(Schema::hasTable('desfile_entry_reservations'));
$this->assertFalse(Schema::hasTable('desfile_reservation_batches'));
$this->assertDatabaseCount('entry_reservations', 2);
$this->assertDatabaseCount('reservation_batches', 1);
$reservation = EntryReservation::findOrFail(40);
$this->assertSame(1, $reservation->catalog_item_id);
$this->assertSame(1, $reservation->catalogItem->id);
$this->assertSame(30, $reservation->batch->id);
$this->assertSame(40, $reservation->batch->reservations->sole()->id);
$this->assertSame(10, $reservation->ticket->id);
$this->assertSame('250.00', $reservation->importe);
$this->assertSame(str_repeat('a', 64), $reservation->batch->request_hash);
$this->assertSame(1, $reservation->inventory->entry_reserved_stock);
$historical = EntryReservation::withTrashed()->findOrFail(41);
$this->assertTrue($historical->trashed());
$this->assertTrue($historical->variant->trashed());
$this->assertSame(2, $historical->catalog_item_id);
}
public function test_reservations_can_belong_to_other_tenants_and_have_no_variant(): void
{
$this->migration()->up();
$batch = ReservationBatch::create([
'user_id' => 1, 'tenant_code' => 'other',
'idempotency_key' => '9495eb8e-a424-4f1b-8911-e05c51e1caf7',
'request_hash' => str_repeat('b', 64),
]);
$reservation = $batch->reservations()->create([
'catalog_item_id' => 2, 'fecha_reserva' => now(),
'tipo_pago' => 'sin_cargo', 'importe' => 0,
]);
$this->assertNull($reservation->variant_id);
$this->assertSame('other', $reservation->catalogItem->tenant_code);
$this->assertSame('other', $reservation->batch->tenant->codigo);
$this->assertSame(1, $reservation->batch->user->id);
}
public function test_variant_based_callers_infer_the_catalog_item(): void
{
$this->migration()->up();
$reservation = EntryReservation::create([
'variant_id' => 1, 'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
]);
$this->assertSame(1, $reservation->fresh()->catalog_item_id);
}
public function test_a_variant_from_another_catalog_item_is_rejected(): void
{
$this->migration()->up();
$this->expectException(ValidationException::class);
EntryReservation::create([
'catalog_item_id' => 2, 'variant_id' => 1,
'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
]);
}
public function test_the_catalog_item_is_required_at_the_database_level(): void
{
$this->migration()->up();
$this->expectException(QueryException::class);
DB::table('entry_reservations')->insert([
'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
]);
}
public function test_upgrade_preserves_foreign_keys_and_batch_idempotency(): void
{
$this->migration()->up();
$this->assertSame([], DB::select('PRAGMA foreign_key_check'));
foreach (['catalog_items' => 1, 'variantes' => 1, 'tickets' => 10, 'inventories' => 20, 'reservation_batches' => 30] as $table => $id) {
try {
DB::table($table)->where('id', $id)->delete();
$this->fail("Expected {$table} to remain referenced");
} catch (QueryException) {
$this->assertDatabaseHas($table, ['id' => $id]);
}
}
$batch = ReservationBatch::findOrFail(30);
$this->expectException(QueryException::class);
ReservationBatch::create($batch->only(['user_id', 'tenant_code', 'idempotency_key', 'request_hash']));
}
public function test_rollback_restores_the_original_schema_and_keeps_data(): void
{
$migration = $this->migration();
$migration->up();
$migration->down();
$this->assertFalse(Schema::hasTable('entry_reservations'));
$this->assertFalse(Schema::hasTable('reservation_batches'));
$this->assertDatabaseCount('desfile_entry_reservations', 2);
$this->assertDatabaseCount('desfile_reservation_batches', 1);
$this->assertFalse(Schema::hasColumn('desfile_entry_reservations', 'catalog_item_id'));
$this->assertSame([], DB::select('PRAGMA foreign_key_check'));
$migration->up();
$this->assertSame(1, EntryReservation::findOrFail(40)->catalog_item_id);
}
public function test_rollback_does_not_discard_reservations_without_variants(): void
{
$migration = $this->migration();
$migration->up();
EntryReservation::create([
'catalog_item_id' => 2, 'fecha_reserva' => now(), 'tipo_pago' => 'sin_cargo',
]);
try {
$migration->down();
$this->fail('Expected incompatible rollback to be rejected');
} catch (\LogicException) {
$this->assertDatabaseCount('entry_reservations', 3);
$this->assertTrue(Schema::hasTable('reservation_batches'));
}
}
public function test_an_orphaned_catalog_is_rejected_before_renaming_tables(): void
{
DB::table('variantes')->where('id', 2)->update(['catalog_item_id' => 999]);
try {
$this->migration()->up();
$this->fail('Expected orphaned catalog to be rejected');
} catch (\LogicException) {
$this->assertDatabaseCount('desfile_entry_reservations', 2);
$this->assertFalse(Schema::hasTable('entry_reservations'));
}
}
}

View File

@@ -13,8 +13,8 @@ use App\Domains\Core\Authorization\Enums\RoleCode;
use App\Domains\Core\Menu\Models\Menu;
use App\Domains\Core\Tenant\Models\AdminWebsiteType;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType;
use App\Domains\Ticketing\Desfile\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Domains\Ticketing\Ticket\Models\TicketRefund;
use App\Shared\Attachable\Enums\AttachmentType;

View File

@@ -72,7 +72,7 @@ class AdminAppTicketEventScopeTest extends TestCase
$table->unsignedBigInteger('purchase_item_id');
$table->decimal('amount', 12, 2);
});
Schema::create('desfile_entry_reservations', function (Blueprint $table): void {
Schema::create('entry_reservations', function (Blueprint $table): void {
$table->id();
$table->unsignedBigInteger('ticket_id');
$table->softDeletes();

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Feature\Desfile;
namespace Tests\Feature\Ticket;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
@@ -12,8 +12,9 @@ class EntryReservationSchemaTest extends TestCase
public function test_the_entry_reservations_table_has_the_requested_columns(): void
{
$this->assertTrue(Schema::hasColumns('desfile_entry_reservations', [
$this->assertTrue(Schema::hasColumns('entry_reservations', [
'id',
'catalog_item_id',
'variant_id',
'fecha_reserva',
'importe',