feat(ticket): implement DesfileEntryReservationController and related services for ticket reservations

This commit is contained in:
2026-10-02 11:50:47 -03:00
parent ab5bc43760
commit f10237eb1f
9 changed files with 365 additions and 264 deletions

View File

@@ -9,15 +9,18 @@ use App\Domains\Ticketing\Desfile\Requests\ExportEntryReservationsRequest;
use App\Domains\Ticketing\Desfile\Requests\IndexEntryReservationsRequest;
use App\Domains\Ticketing\Desfile\Requests\StoreEntryReservationsRequest;
use App\Domains\Ticketing\Desfile\Resources\EntryReservationResource;
use App\Domains\Ticketing\Desfile\Services\DesfileEntryReservationPolicy;
use App\Domains\Ticketing\Desfile\Services\DesfileEntryReservationService;
use App\Domains\Ticketing\Desfile\Services\EntryReservationExcelService;
use App\Domains\Ticketing\Desfile\Services\EntryReservationPdfService;
use App\Domains\Ticketing\Desfile\Services\EntryReservationReportService;
use App\Domains\Ticketing\Desfile\Services\EntryReservationService;
use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Domains\Ticketing\Ticket\Services\EntryReservationService as TicketEntryReservationService;
use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity;
use App\Domains\Ticketing\Ticket\Services\TicketGeneratorService;
use App\Domains\Ticketing\Ticket\Services\TicketValidityResolver;
use Barryvdh\DomPDF\ServiceProvider as DomPdfServiceProvider;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -166,7 +169,12 @@ class EntryReservationServiceTest extends TestCase
}
}
private function service(bool $failSecond = false): EntryReservationService
private function service(bool $failSecond = false): DesfileEntryReservationService
{
return new DesfileEntryReservationService($this->ticketService($failSecond), new DesfileEntryReservationPolicy);
}
private function ticketService(bool $failSecond = false): TicketEntryReservationService
{
$generator = Mockery::mock(TicketGeneratorService::class);
$generator->shouldReceive('generate')->andReturnUsing(function ($entry, $user, $quantity, $variantId) use ($failSecond) {
@@ -183,7 +191,7 @@ class EntryReservationServiceTest extends TestCase
return collect([Ticket::query()->findOrFail($id)]);
});
return new EntryReservationService($generator);
return new TicketEntryReservationService($generator);
}
private function rows(): array
@@ -251,6 +259,120 @@ class EntryReservationServiceTest extends TestCase
$service->reserve($user, (string) Str::uuid(), $this->rows());
}
public function test_a_seat_cannot_be_reserved_twice_even_when_inventory_has_more_stock(): void
{
DB::table('inventories')->where('id', 1)->update(['real_stock' => 3]);
$service = $this->service();
$user = User::findOrFail(1);
$row = $this->rows()[0];
$reservation = $service->reserve($user, (string) Str::uuid(), [$row])->sole();
try {
$service->reserve($user, (string) Str::uuid(), [$row]);
$this->fail('Expected a unique seat despite remaining stock');
} catch (ValidationException $error) {
$this->assertArrayHasKey('rows.0.variant_id', $error->errors());
$this->assertStringContainsString('Asiento: 17', $error->errors()['rows.0.variant_id'][0]);
}
$this->assertDatabaseCount('tickets', 1);
$this->assertDatabaseCount('reservation_batches', 1);
$this->assertSame(1, Inventory::findOrFail(1)->entry_reserved_stock);
$service->cancel($user, $reservation->id);
$replacement = $service->reserve($user, (string) Str::uuid(), [$row])->sole();
$this->assertNotSame($reservation->id, $replacement->id);
$this->assertSame(1, Inventory::findOrFail(1)->entry_reserved_stock);
}
public function test_duplicate_seats_in_one_batch_are_rejected_without_changing_stock(): void
{
DB::table('inventories')->where('id', 1)->update(['real_stock' => 3]);
$row = $this->rows()[0];
try {
$this->service()->reserve(User::findOrFail(1), (string) Str::uuid(), [$row, $row]);
$this->fail('Expected duplicate seat validation');
} catch (ValidationException $error) {
$this->assertArrayHasKey('rows.1.variant_id', $error->errors());
}
$this->assertDatabaseCount('tickets', 0);
$this->assertDatabaseCount('reservation_batches', 0);
$this->assertSame(0, Inventory::findOrFail(1)->entry_reserved_stock);
}
public function test_legacy_batches_replay_after_cancellation_and_an_active_event_change(): void
{
$service = $this->service();
$user = User::findOrFail(1);
$key = (string) Str::uuid();
$result = $service->reserve($user, $key, $this->rows());
DB::table('reservation_batches')->update([
'request_hash' => hash('sha256', json_encode($this->rows(), JSON_THROW_ON_ERROR)),
]);
$service->cancel($user, $result->first()->id);
DB::table('tenants')->update(['active_event_id' => 20]);
$replay = $service->reserve($user, $key, array_reverse($this->rows()));
$this->assertSame($result->last()->id, $replay->sole()->id);
$this->assertSame($result->last()->id, $service->reservations($user)->sole()->id);
$this->assertSame($result->last()->ticket_id, $service->reservationTicket($user, $result->last()->id)->id);
$this->assertSame('NORMAL', $replay->sole()->variant->selectionValues()->get('tipo'));
$this->assertDatabaseCount('tickets', 2);
$this->assertDatabaseCount('reservation_batches', 1);
}
public function test_desfile_cannot_list_export_download_or_cancel_other_catalog_item_reservations(): void
{
DB::table('catalog_items')->insert([
'id' => 2, 'tenant_code' => 'desfile_pura_tendencia', 'slug' => 'other-ticket',
'inventory_policy' => 'tracked', 'precio' => 100,
]);
DB::table('inventories')->insert(['id' => 3, 'real_stock' => 1]);
DB::table('variantes')->insert(['id' => 3, 'catalog_item_id' => 2, 'inventory_id' => 3, 'precio' => 100]);
$user = User::findOrFail(1);
$other = $this->ticketService()->reserve($user, (string) Str::uuid(), [
['catalog_item_id' => 2, 'variant_id' => 3, 'tipo_pago' => 'sin_cargo'],
])->sole();
$service = $this->service();
$own = $service->reserve($user, (string) Str::uuid(), [$this->rows()[0]])->sole();
$this->assertSame([$own->id], $service->reservations($user)->pluck('id')->all());
$this->assertSame([$own->id], $service->reservationsForExport($user)->pluck('id')->all());
foreach (['reservationTicket', 'cancel'] as $method) {
try {
$service->$method($user, $other->id);
$this->fail('Expected the Desfile catalog scope');
} catch (ModelNotFoundException) {
$this->assertNull(Ticket::findOrFail($other->ticket_id)->cancelled_at);
}
}
DB::table('catalog_items')->where('id', 1)->update(['deleted_at' => now()]);
$this->assertSame(0, $service->reservations($user)->total());
$this->assertCount(0, $service->reservationsForExport($user));
}
public function test_desfile_service_rejects_other_tenants_for_every_operation(): void
{
$user = new User(['tenant_codigo' => 'other']);
$service = $this->service();
$operations = [
fn () => $service->reservations($user),
fn () => $service->reservationsForExport($user),
fn () => $service->reservationTicket($user, 1),
fn () => $service->cancel($user, 1),
fn () => $service->reserve($user, (string) Str::uuid(), $this->rows()),
];
foreach ($operations as $operation) {
try {
$operation();
$this->fail('Expected the Desfile tenant restriction');
} catch (HttpException $error) {
$this->assertSame(403, $error->getStatusCode());
}
}
}
public function test_it_cancels_the_ticket_releases_stock_and_soft_deletes_the_reservation(): void
{
$service = $this->service();
@@ -441,7 +563,10 @@ class EntryReservationServiceTest extends TestCase
$validity->shouldReceive('resolveVariant')->andReturn(
ResolvedTicketValidity::unrestricted(),
);
$service = new EntryReservationService(new TicketGeneratorService($validity));
$service = new DesfileEntryReservationService(
new TicketEntryReservationService(new TicketGeneratorService($validity)),
new DesfileEntryReservationPolicy,
);
$result = $service->reserve(User::findOrFail(1), (string) Str::uuid(), $this->rows());
foreach ($result as $reservation) {
$this->assertSame(1, $reservation->catalog_item_id);