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

@@ -8,6 +8,7 @@ use App\Domains\Commerce\Catalog\Enums\InventorySubject;
use App\Domains\Commerce\Catalog\Services\CatalogInventoryService; use App\Domains\Commerce\Catalog\Services\CatalogInventoryService;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Event\Models\Event; use App\Domains\Ticketing\Event\Models\Event;
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Models\Ticket; use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Shared\Attachable\Models\Attachment; use App\Shared\Attachable\Models\Attachment;
use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Attributes\Fillable;
@@ -147,6 +148,12 @@ class CatalogItem extends Model
return $this->hasMany(Ticket::class, 'source_catalog_item_id'); return $this->hasMany(Ticket::class, 'source_catalog_item_id');
} }
/** @return HasMany<EntryReservation, $this> */
public function entryReservations(): HasMany
{
return $this->hasMany(EntryReservation::class);
}
/** @return BelongsToMany<Attribute, $this> */ /** @return BelongsToMany<Attribute, $this> */
public function attributes(): BelongsToMany public function attributes(): BelongsToMany
{ {
@@ -266,13 +273,13 @@ class CatalogItem extends Model
{ {
$this->variants $this->variants
->filter(fn (Variant $variant): bool => $variant->exists) ->filter(fn (Variant $variant): bool => $variant->exists)
->loadMissing('desfileEntryReservations'); ->loadMissing('entryReservations');
return $this->variants return $this->variants
->each(fn (Variant $variant) => $variant->setRelation('catalogItem', $this)) ->each(fn (Variant $variant) => $variant->setRelation('catalogItem', $this))
->filter(fn (Variant $variant): bool => $variant->hasOnlyActiveEventDates() ->filter(fn (Variant $variant): bool => $variant->hasOnlyActiveEventDates()
&& (! $variant->relationLoaded('desfileEntryReservations') && (! $variant->relationLoaded('entryReservations')
|| $variant->desfileEntryReservations->isEmpty()) || $variant->entryReservations->isEmpty())
&& (($includedVariantId !== null && $variant->id === $includedVariantId) && (($includedVariantId !== null && $variant->id === $includedVariantId)
|| ($variant->isSellable() && ( || ($variant->isSellable() && (
$this->inventory_policy === InventoryPolicy::Unlimited $this->inventory_policy === InventoryPolicy::Unlimited

View File

@@ -2,8 +2,8 @@
namespace App\Domains\Commerce\Catalog\Models; namespace App\Domains\Commerce\Catalog\Models;
use App\Domains\Ticketing\Desfile\Models\EntryReservation;
use App\Domains\Ticketing\Event\Models\EventDate; use App\Domains\Ticketing\Event\Models\EventDate;
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Models\Ticket; use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Shared\Attachable\Models\Attachment; use App\Shared\Attachable\Models\Attachment;
use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Attributes\Fillable;
@@ -76,7 +76,7 @@ class Variant extends Model
} }
/** @return HasMany<EntryReservation, $this> */ /** @return HasMany<EntryReservation, $this> */
public function desfileEntryReservations(): HasMany public function entryReservations(): HasMany
{ {
return $this->hasMany(EntryReservation::class); return $this->hasMany(EntryReservation::class);
} }

View File

@@ -7,8 +7,8 @@ use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Commerce\Catalog\Models\StockReservation; use App\Domains\Commerce\Catalog\Models\StockReservation;
use App\Domains\Commerce\Catalog\Models\StockReservationLine; use App\Domains\Commerce\Catalog\Models\StockReservationLine;
use App\Domains\Commerce\Catalog\Models\Variant; use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Ticketing\Desfile\Models\EntryReservation;
use App\Domains\Ticketing\Event\Models\EventDate; use App\Domains\Ticketing\Event\Models\EventDate;
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class VariantReplacementService class VariantReplacementService

View File

@@ -25,9 +25,8 @@ class TenantTransactionResetService
'carts' => $scope['cart_ids']->count(), 'carts' => $scope['cart_ids']->count(),
'cart_items' => $scope['cart_item_ids']->count(), 'cart_items' => $scope['cart_item_ids']->count(),
'tickets' => DB::table('tickets')->where('tenant_code', $tenantCode)->count(), 'tickets' => DB::table('tickets')->where('tenant_code', $tenantCode)->count(),
'entry_reservations' => DB::table('desfile_entry_reservations') 'entry_reservations' => DB::table('entry_reservations')
->whereIn('variant_id', DB::table('variantes')->whereIn('catalog_item_id', ->whereIn('catalog_item_id', DB::table('catalog_items')->where('tenant_code', $tenantCode)->select('id'))->count(),
DB::table('catalog_items')->where('tenant_code', $tenantCode)->select('id'))->select('id'))->count(),
'stock_reservations' => $this->reservationQuery($scope)->count(), 'stock_reservations' => $this->reservationQuery($scope)->count(),
'purchase_changes' => DB::table('value_changes') 'purchase_changes' => DB::table('value_changes')
->where('tenant_code', $tenantCode) ->where('tenant_code', $tenantCode)
@@ -50,10 +49,9 @@ class TenantTransactionResetService
$telepagosQr = DB::table('telepagos_qr')->whereIn('compra_id', $scope['purchase_ids'])->count(); $telepagosQr = DB::table('telepagos_qr')->whereIn('compra_id', $scope['purchase_ids'])->count();
$summary = [ $summary = [
'stock_reservations_deleted' => $this->reservationQuery($scope)->delete(), 'stock_reservations_deleted' => $this->reservationQuery($scope)->delete(),
'entry_reservations_deleted' => DB::table('desfile_entry_reservations') 'entry_reservations_deleted' => DB::table('entry_reservations')
->whereIn('variant_id', DB::table('variantes')->whereIn('catalog_item_id', ->whereIn('catalog_item_id', DB::table('catalog_items')->where('tenant_code', $tenantCode)->select('id'))->delete(),
DB::table('catalog_items')->where('tenant_code', $tenantCode)->select('id'))->select('id'))->delete(), 'entry_reservation_batches_deleted' => DB::table('reservation_batches')->where('tenant_code', $tenantCode)->delete(),
'entry_reservation_batches_deleted' => DB::table('desfile_reservation_batches')->where('tenant_code', $tenantCode)->delete(),
'tickets_deleted' => DB::table('tickets')->where('tenant_code', $tenantCode)->delete(), 'tickets_deleted' => DB::table('tickets')->where('tenant_code', $tenantCode)->delete(),
'purchase_changes_deleted' => DB::table('value_changes') 'purchase_changes_deleted' => DB::table('value_changes')
->where('tenant_code', $tenantCode) ->where('tenant_code', $tenantCode)

View File

@@ -1,54 +0,0 @@
<?php
namespace App\Domains\Ticketing\Desfile\Models;
use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType;
use App\Domains\Ticketing\Ticket\Models\Ticket;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
#[Fillable([
'variant_id',
'ticket_id',
'inventory_id',
'batch_id',
'fecha_reserva',
'importe',
'tipo_pago',
])]
class EntryReservation extends Model
{
use SoftDeletes;
protected $table = 'desfile_entry_reservations';
protected function casts(): array
{
return [
'variant_id' => 'integer',
'fecha_reserva' => 'datetime',
'importe' => 'decimal:2',
'tipo_pago' => EntryReservationPaymentType::class,
];
}
/** @return BelongsTo<Variant, $this> */
public function variant(): BelongsTo
{
return $this->belongsTo(Variant::class)->withTrashed();
}
public function ticket(): BelongsTo
{
return $this->belongsTo(Ticket::class);
}
public function inventory(): BelongsTo
{
return $this->belongsTo(Inventory::class);
}
}

View File

@@ -2,7 +2,7 @@
namespace App\Domains\Ticketing\Desfile\Requests; namespace App\Domains\Ticketing\Desfile\Requests;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType; use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;

View File

@@ -2,7 +2,7 @@
namespace App\Domains\Ticketing\Desfile\Requests; namespace App\Domains\Ticketing\Desfile\Requests;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType; use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;

View File

@@ -15,6 +15,7 @@ class EntryReservationResource extends JsonResource
return [ return [
'id' => $this->id, 'id' => $this->id,
'catalog_item_id' => $this->catalog_item_id,
'variant_id' => $this->variant_id, 'variant_id' => $this->variant_id,
'ticket_id' => $this->ticket_id, 'ticket_id' => $this->ticket_id,
'fecha_reserva' => $this->fecha_reserva->toIso8601String(), 'fecha_reserva' => $this->fecha_reserva->toIso8601String(),

View File

@@ -3,7 +3,7 @@
namespace App\Domains\Ticketing\Desfile\Services; namespace App\Domains\Ticketing\Desfile\Services;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Desfile\Models\EntryReservation; use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Shared\Date;

View File

@@ -3,7 +3,7 @@
namespace App\Domains\Ticketing\Desfile\Services; namespace App\Domains\Ticketing\Desfile\Services;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Desfile\Models\EntryReservation; use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use Barryvdh\DomPDF\Facade\Pdf; use Barryvdh\DomPDF\Facade\Pdf;
use Barryvdh\DomPDF\PDF as DomPdf; use Barryvdh\DomPDF\PDF as DomPdf;
use Illuminate\Http\Response; use Illuminate\Http\Response;

View File

@@ -2,7 +2,7 @@
namespace App\Domains\Ticketing\Desfile\Services; namespace App\Domains\Ticketing\Desfile\Services;
use App\Domains\Ticketing\Desfile\Models\EntryReservation; use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class EntryReservationReportService class EntryReservationReportService

View File

@@ -7,9 +7,10 @@ use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Domains\Commerce\Catalog\Models\Inventory; use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Commerce\Catalog\Models\Variant; use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Core\Auth\Models\User; use App\Domains\Core\Auth\Models\User;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType; use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
use App\Domains\Ticketing\Desfile\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Exceptions\TicketGenerationException; use App\Domains\Ticketing\Ticket\Exceptions\TicketGenerationException;
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Models\ReservationBatch;
use App\Domains\Ticketing\Ticket\Models\Ticket; use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Domains\Ticketing\Ticket\Services\TicketGeneratorService; use App\Domains\Ticketing\Ticket\Services\TicketGeneratorService;
use App\Domains\Ticketing\Ticket\Services\TicketPresentationResolver; use App\Domains\Ticketing\Ticket\Services\TicketPresentationResolver;
@@ -139,7 +140,7 @@ class EntryReservationService
return DB::transaction(function () use ($user, $key, $rows, $hash): Collection { return DB::transaction(function () use ($user, $key, $rows, $hash): Collection {
// Serialize retries by the same administrator, including the first insert. // Serialize retries by the same administrator, including the first insert.
User::query()->whereKey($user->id)->lockForUpdate()->firstOrFail(); User::query()->whereKey($user->id)->lockForUpdate()->firstOrFail();
$batch = DB::table('desfile_reservation_batches') $batch = ReservationBatch::query()
->where('user_id', $user->id)->where('idempotency_key', $key)->lockForUpdate()->first(); ->where('user_id', $user->id)->where('idempotency_key', $key)->lockForUpdate()->first();
if ($batch !== null) { if ($batch !== null) {
abort_unless($batch->tenant_code === $user->tenant_codigo && hash_equals($batch->request_hash, $hash), 409, abort_unless($batch->tenant_code === $user->tenant_codigo && hash_equals($batch->request_hash, $hash), 409,
@@ -157,7 +158,7 @@ class EntryReservationService
->orderBy('id')->lockForUpdate()->get()->keyBy('id'); ->orderBy('id')->lockForUpdate()->get()->keyBy('id');
$variants->load([ $variants->load([
'eventDates', 'eventDate', 'eventDates', 'eventDate',
'desfileEntryReservations' => fn ($query) => $query->lockForUpdate(), 'entryReservations' => fn ($query) => $query->lockForUpdate(),
]); ]);
foreach ($variants as $variant) { foreach ($variants as $variant) {
$variant->setRelation('inventory', $inventories->get($variant->inventory_id)); $variant->setRelation('inventory', $inventories->get($variant->inventory_id));
@@ -193,10 +194,9 @@ class EntryReservationService
throw ValidationException::withMessages($errors); throw ValidationException::withMessages($errors);
} }
$batchId = DB::table('desfile_reservation_batches')->insertGetId([ $batch = ReservationBatch::query()->create([
'user_id' => $user->id, 'tenant_code' => $tenant->codigo, 'user_id' => $user->id, 'tenant_code' => $tenant->codigo,
'idempotency_key' => $key, 'request_hash' => $hash, 'idempotency_key' => $key, 'request_hash' => $hash,
'created_at' => now(), 'updated_at' => now(),
]); ]);
foreach ($requirements as $inventoryId => $quantity) { foreach ($requirements as $inventoryId => $quantity) {
$inventories[$inventoryId]->reserveEntry($quantity, $tracked); $inventories[$inventoryId]->reserveEntry($quantity, $tracked);
@@ -213,7 +213,8 @@ class EntryReservationService
]); ]);
} }
$reservation = EntryReservation::query()->create([ $reservation = EntryReservation::query()->create([
'batch_id' => $batchId, 'ticket_id' => $ticket->id, 'batch_id' => $batch->id, 'ticket_id' => $ticket->id,
'catalog_item_id' => $entry->id,
'variant_id' => $variant->id, 'inventory_id' => $variant->inventory_id, 'variant_id' => $variant->id, 'inventory_id' => $variant->inventory_id,
'fecha_reserva' => now(), 'tipo_pago' => $payment, 'fecha_reserva' => now(), 'tipo_pago' => $payment,
'importe' => $payment === EntryReservationPaymentType::Free ? 0 : $variant->getPrice(), 'importe' => $payment === EntryReservationPaymentType::Free ? 0 : $variant->getPrice(),

View File

@@ -1,6 +1,6 @@
<?php <?php
namespace App\Domains\Ticketing\Desfile\Enums; namespace App\Domains\Ticketing\Ticket\Enums;
enum EntryReservationPaymentType: string enum EntryReservationPaymentType: string
{ {

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Domains\Ticketing\Ticket\Models;
use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Validation\ValidationException;
#[Fillable([
'catalog_item_id',
'variant_id',
'ticket_id',
'inventory_id',
'batch_id',
'fecha_reserva',
'importe',
'tipo_pago',
])]
class EntryReservation extends Model
{
use SoftDeletes;
protected $table = 'entry_reservations';
protected function casts(): array
{
return [
'catalog_item_id' => 'integer',
'variant_id' => 'integer',
'fecha_reserva' => 'datetime',
'importe' => 'decimal:2',
'tipo_pago' => EntryReservationPaymentType::class,
];
}
protected static function booted(): void
{
static::saving(function (self $reservation): void {
if ($reservation->variant_id === null) {
return;
}
$variant = Variant::withTrashed()->findOrFail($reservation->variant_id);
// Existing variant-based callers can keep creating reservations as before.
$reservation->catalog_item_id ??= $variant->catalog_item_id;
if ($reservation->catalog_item_id !== $variant->catalog_item_id) {
throw ValidationException::withMessages([
'variant_id' => 'La variante no pertenece al ítem del catálogo seleccionado.',
]);
}
});
}
/** @return BelongsTo<CatalogItem, $this> */
public function catalogItem(): BelongsTo
{
return $this->belongsTo(CatalogItem::class)->withTrashed();
}
/** @return BelongsTo<ReservationBatch, $this> */
public function batch(): BelongsTo
{
return $this->belongsTo(ReservationBatch::class, 'batch_id');
}
/** @return BelongsTo<Variant, $this> */
public function variant(): BelongsTo
{
return $this->belongsTo(Variant::class)->withTrashed();
}
public function ticket(): BelongsTo
{
return $this->belongsTo(Ticket::class);
}
public function inventory(): BelongsTo
{
return $this->belongsTo(Inventory::class);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Domains\Ticketing\Ticket\Models;
use App\Domains\Core\Auth\Models\User;
use App\Domains\Core\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable(['user_id', 'tenant_code', 'idempotency_key', 'request_hash'])]
class ReservationBatch extends Model
{
protected function casts(): array
{
return ['user_id' => 'integer'];
}
/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class)->withTrashed();
}
/** @return BelongsTo<Tenant, $this> */
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
}
/** @return HasMany<EntryReservation, $this> */
public function reservations(): HasMany
{
return $this->hasMany(EntryReservation::class, 'batch_id');
}
}

View File

@@ -7,7 +7,6 @@ use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Commerce\Purchase\Models\PurchaseItem; use App\Domains\Commerce\Purchase\Models\PurchaseItem;
use App\Domains\Core\Auth\Models\User; use App\Domains\Core\Auth\Models\User;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Desfile\Models\EntryReservation;
use App\Domains\Ticketing\Event\Models\Event; use App\Domains\Ticketing\Event\Models\Event;
use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity; use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity;
use App\Domains\Ticketing\Ticket\Services\ResolvedValidityGroup; use App\Domains\Ticketing\Ticket\Services\ResolvedValidityGroup;

View File

@@ -5,7 +5,7 @@ namespace App\Shared\Forms\Services;
use App\Domains\Commerce\Catalog\Models\CatalogItem; use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Domains\Commerce\Catalog\Models\Variant; use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType; use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
class DesfileEntryReservationFormService class DesfileEntryReservationFormService
{ {

View File

@@ -0,0 +1,53 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
// Validate before changing the schema, including soft-deleted reservations.
if (DB::table('desfile_entry_reservations as reservations')
->leftJoin('variantes', 'variantes.id', '=', 'reservations.variant_id')
->leftJoin('catalog_items', 'catalog_items.id', '=', 'variantes.catalog_item_id')
->whereNull('catalog_items.id')->exists()) {
throw new LogicException('No se puede determinar el catálogo de todas las reservas existentes.');
}
Schema::rename('desfile_reservation_batches', 'reservation_batches');
Schema::rename('desfile_entry_reservations', 'entry_reservations');
Schema::table('entry_reservations', function (Blueprint $table): void {
$table->foreignId('catalog_item_id')->nullable()
->constrained('catalog_items')->restrictOnDelete();
$table->unsignedBigInteger('variant_id')->nullable()->change();
});
DB::table('entry_reservations')->update([
'catalog_item_id' => DB::raw('(SELECT catalog_item_id FROM variantes WHERE variantes.id = entry_reservations.variant_id)'),
]);
Schema::table('entry_reservations', function (Blueprint $table): void {
$table->unsignedBigInteger('catalog_item_id')->nullable(false)->change();
});
}
public function down(): void
{
// The old schema cannot represent tickets without variants. Never discard them.
if (DB::table('entry_reservations')->whereNull('variant_id')->exists()) {
throw new LogicException('No se puede revertir mientras existan reservas sin variante.');
}
Schema::table('entry_reservations', function (Blueprint $table): void {
$table->dropConstrainedForeignId('catalog_item_id');
$table->unsignedBigInteger('variant_id')->nullable(false)->change();
});
Schema::rename('entry_reservations', 'desfile_entry_reservations');
Schema::rename('reservation_batches', 'desfile_reservation_batches');
}
};

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

View File

@@ -99,7 +99,7 @@ class AdminAppDesfileEntryReservationFormControllerTest extends TestCase
]); ]);
} }
if ($state === 'administrative') { if ($state === 'administrative') {
$variant->desfileEntryReservations()->create([ $variant->entryReservations()->create([
'fecha_reserva' => now(), 'importe' => 0, 'tipo_pago' => 'sin_cargo', 'fecha_reserva' => now(), 'importe' => 0, 'tipo_pago' => 'sin_cargo',
]); ]);
} }
@@ -111,7 +111,7 @@ class AdminAppDesfileEntryReservationFormControllerTest extends TestCase
->assertOk()->assertJsonCount(1, 'data.variants') ->assertOk()->assertJsonCount(1, 'data.variants')
->assertJsonPath('data.variants.0.id', $availableId) ->assertJsonPath('data.variants.0.id', $availableId)
->assertJsonPath('data.fields.3.options', [['value' => '1', 'label' => '1']]); ->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')); $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\Menu\Models\Menu;
use App\Domains\Core\Tenant\Models\AdminWebsiteType; use App\Domains\Core\Tenant\Models\AdminWebsiteType;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType; use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
use App\Domains\Ticketing\Desfile\Models\EntryReservation; use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Models\Ticket; use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Domains\Ticketing\Ticket\Models\TicketRefund; use App\Domains\Ticketing\Ticket\Models\TicketRefund;
use App\Shared\Attachable\Enums\AttachmentType; use App\Shared\Attachable\Enums\AttachmentType;

View File

@@ -72,7 +72,7 @@ class AdminAppTicketEventScopeTest extends TestCase
$table->unsignedBigInteger('purchase_item_id'); $table->unsignedBigInteger('purchase_item_id');
$table->decimal('amount', 12, 2); $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->id();
$table->unsignedBigInteger('ticket_id'); $table->unsignedBigInteger('ticket_id');
$table->softDeletes(); $table->softDeletes();

View File

@@ -1,6 +1,6 @@
<?php <?php
namespace Tests\Feature\Desfile; namespace Tests\Feature\Ticket;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema; 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 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', 'id',
'catalog_item_id',
'variant_id', 'variant_id',
'fecha_reserva', 'fecha_reserva',
'importe', 'importe',

View File

@@ -6,7 +6,7 @@ use App\Domains\Commerce\Catalog\Enums\InventoryPolicy;
use App\Domains\Commerce\Catalog\Models\CatalogItem; use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Domains\Commerce\Catalog\Models\Inventory; use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Commerce\Catalog\Models\Variant; use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Ticketing\Desfile\Models\EntryReservation; use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Tests\TestCase; use Tests\TestCase;
@@ -16,10 +16,10 @@ class VariantAvailabilityTest extends TestCase
{ {
$reserved = new Variant; $reserved = new Variant;
$reserved->id = 1; $reserved->id = 1;
$reserved->setRelation('desfileEntryReservations', new Collection([new EntryReservation])); $reserved->setRelation('entryReservations', new Collection([new EntryReservation]));
$available = new Variant; $available = new Variant;
$available->id = 2; $available->id = 2;
$available->setRelation('desfileEntryReservations', new Collection); $available->setRelation('entryReservations', new Collection);
$item = new CatalogItem(['inventory_policy' => InventoryPolicy::Unlimited]); $item = new CatalogItem(['inventory_policy' => InventoryPolicy::Unlimited]);
$item->setRelation('variants', new Collection([$reserved, $available])); $item->setRelation('variants', new Collection([$reserved, $available]));

View File

@@ -1,9 +1,9 @@
<?php <?php
namespace Tests\Unit\Desfile; namespace Tests\Unit\Ticket;
use App\Domains\Ticketing\Desfile\Enums\EntryReservationPaymentType; use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
use App\Domains\Ticketing\Desfile\Models\EntryReservation; use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class EntryReservationTest extends TestCase class EntryReservationTest extends TestCase
@@ -21,6 +21,7 @@ class EntryReservationTest extends TestCase
$reservation = new EntryReservation; $reservation = new EntryReservation;
$this->assertSame([ $this->assertSame([
'catalog_item_id',
'variant_id', 'variant_id',
'ticket_id', 'ticket_id',
'inventory_id', 'inventory_id',

View File

@@ -6,8 +6,8 @@ use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Domains\Commerce\Catalog\Models\Variant; use App\Domains\Commerce\Catalog\Models\Variant;
use App\Domains\Core\Auth\Models\User; use App\Domains\Core\Auth\Models\User;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Desfile\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Enums\ValidityTimeType; use App\Domains\Ticketing\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
use App\Domains\Ticketing\Ticket\Models\Ticket; use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Domains\Ticketing\Ticket\Models\ValidityTime; use App\Domains\Ticketing\Ticket\Models\ValidityTime;
use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity; use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity;