feat(cart): implement InvalidateEventDateCartsService to handle cart invalidation for rescheduled event dates
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Authorization\Enums\RoleCode;
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\StockReservation;
|
||||
@@ -421,6 +422,43 @@ class AdminAppEventControllerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function test_rescheduling_invalidates_reserved_carts_before_replacing_variants(): void
|
||||
{
|
||||
Event::fake([EventDateRescheduled::class]);
|
||||
$tenant = $this->createActiveEvent($this->createTenant('acme'), 'Festival Acme');
|
||||
$source = $tenant->eventDates()->create([
|
||||
'date' => '2027-10-09', 'time_start' => '09:00', 'time_end' => '18:30',
|
||||
]);
|
||||
$variant = $this->createVariant($tenant, $source->id);
|
||||
$variant->inventory->update(['real_stock' => 5, 'reserved_stock' => 2]);
|
||||
$reservation = StockReservation::query()->create([
|
||||
'status' => StockReservation::STATUS_ACTIVE, 'expires_at' => now()->addHour(),
|
||||
]);
|
||||
StockReservationLine::query()->create([
|
||||
'stock_reservation_id' => $reservation->id, 'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 2, 'tracks_inventory' => true,
|
||||
]);
|
||||
$admin = $this->createAdminAppUser($tenant);
|
||||
$cart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo, 'user_id' => $admin->id,
|
||||
'status' => Cart::STATUS_ACTIVE, 'origin' => Cart::ORIGIN_USER,
|
||||
'current_stock_reservation_id' => $reservation->id,
|
||||
]);
|
||||
$cart->items()->create([
|
||||
'catalog_item_id' => $variant->catalog_item_id, 'variant_id' => $variant->id, 'cantidad' => 2,
|
||||
]);
|
||||
Sanctum::actingAs($admin);
|
||||
|
||||
$this->postJson("/api/v1/adminapp/tenant/event-dates/{$source->id}/reschedule", [
|
||||
'date' => '2027-10-20',
|
||||
])->assertOk();
|
||||
|
||||
$this->assertSame(Cart::STATUS_EXPIRED, $cart->fresh()->status);
|
||||
$this->assertSame(StockReservation::STATUS_RELEASED, $reservation->fresh()->status);
|
||||
$this->assertSame(0, $variant->fresh()->replacement->inventory->reserved_stock);
|
||||
$this->getJson('/api/tenants/acme/cart')->assertOk()->assertJsonCount(0, 'data.items');
|
||||
}
|
||||
|
||||
public function test_rescheduling_reuses_an_equivalent_destination_variant(): void
|
||||
{
|
||||
Event::fake([EventDateRescheduled::class]);
|
||||
|
||||
Reference in New Issue
Block a user