Squashed commit of the following:
commitcd99a6489bMerge:ebcbbb1e63a036Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 26 10:12:44 2026 -0300 Merge branch 'fix/simplify_stock_reservations' of https://gitea.quo.ar/tbianchini/shopit-back into fix/simplify_stock_reservations commitebcbbb1774Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:33:39 2026 -0300 fix(cart): reconcile expired cart mutations commit3dd378ed06Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:15 2026 -0300 docs(stock): describe associated entity expiration commit4f705175bdAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:14 2026 -0300 test(stock): cover propagated reservation expiration commit82570fe941Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:09:52 2026 -0300 refactor(stock): propagate reservation expiration commitd2a2b323e0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 docs(stock): document terminal reservation recovery commitec67623b7bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 test(stock): cover terminal reservation lifecycle commitf6f138e180Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:41 2026 -0300 refactor(stock): make expired reservations terminal commitd612b7a118Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:30 2026 -0300 docs(stock): describe unified expiration ownership commit49f42d4507Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:23 2026 -0300 test(stock): cover authoritative reservation expiration commit1881cc1d4bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:17 2026 -0300 refactor(stock): make reservation expiration authoritative commit00ec37d8a0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:34 2026 -0300 docs(stock): document centralized reservation flow commitd9f374c718Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:29 2026 -0300 test(stock): cover aggregate reservation lifecycle commitbbfdf8f342Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:23 2026 -0300 refactor(stock): centralize reservation aggregate commit8a0f29bdaeAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:33:39 2026 -0300 fix(cart): reconcile expired cart mutations commit198ed400a9Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:15 2026 -0300 docs(stock): describe associated entity expiration commit23d83166e6Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:14 2026 -0300 test(stock): cover propagated reservation expiration commitcb090402d0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:09:52 2026 -0300 refactor(stock): propagate reservation expiration commitc120b6f7c0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 docs(stock): document terminal reservation recovery commit1106fa28b4Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 test(stock): cover terminal reservation lifecycle commit896fb81bcfAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:41 2026 -0300 refactor(stock): make expired reservations terminal commit8545fbc645Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:30 2026 -0300 docs(stock): describe unified expiration ownership commitdd6dcb708aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:23 2026 -0300 test(stock): cover authoritative reservation expiration commit624b246ff5Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:17 2026 -0300 refactor(stock): make reservation expiration authoritative commit007b95b904Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:34 2026 -0300 docs(stock): document centralized reservation flow commit0b3c8c93beAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:29 2026 -0300 test(stock): cover aggregate reservation lifecycle commitede718e448Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:23 2026 -0300 refactor(stock): centralize reservation aggregate commite63a03601aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:33:39 2026 -0300 fix(cart): reconcile expired cart mutations commit389373ad8cAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:15 2026 -0300 docs(stock): describe associated entity expiration commitace02a3133Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:14 2026 -0300 test(stock): cover propagated reservation expiration commitd69a6210f6Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:09:52 2026 -0300 refactor(stock): propagate reservation expiration commit3c0b43fea3Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 docs(stock): document terminal reservation recovery commit9b0008626bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 test(stock): cover terminal reservation lifecycle commit61861e331aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:41 2026 -0300 refactor(stock): make expired reservations terminal commit2cc9d7dd97Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:30 2026 -0300 docs(stock): describe unified expiration ownership commitf982bcead1Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:23 2026 -0300 test(stock): cover authoritative reservation expiration commit7a85e1731dAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:17 2026 -0300 refactor(stock): make reservation expiration authoritative commit6db99e775aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:34 2026 -0300 docs(stock): document centralized reservation flow commite5f7ba3615Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:29 2026 -0300 test(stock): cover aggregate reservation lifecycle commit24bfef431bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:23 2026 -0300 refactor(stock): centralize reservation aggregate
This commit is contained in:
@@ -32,6 +32,82 @@ class StorePurchaseTest extends TestCase
|
||||
Queue::fake();
|
||||
}
|
||||
|
||||
public function test_checkout_keeps_the_cart_reservation_and_refreshes_its_expiration(): void
|
||||
{
|
||||
config()->set('catalog.stock_reservation_expiration_minutes', 5);
|
||||
config()->set('purchase.checkout_expiration_minutes', 30);
|
||||
$now = now()->startOfSecond();
|
||||
$this->travelTo($now);
|
||||
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$cart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$cart->addItem($variant->catalog_item_id, $variant->id, 2);
|
||||
$reservationId = $cart->fresh()->current_stock_reservation_id;
|
||||
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'active',
|
||||
'expires_at' => $now->copy()->addMinutes(5)->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$this->travel(2)->minutes();
|
||||
$purchase = app(CheckoutService::class)->startCheckout($tenant, $user->id, [
|
||||
'cart_id' => $cart->id,
|
||||
]);
|
||||
|
||||
$this->assertSame($reservationId, $purchase->stock_reservation_id);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'active',
|
||||
'expires_at' => $now->copy()->addMinutes(32)->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_checkout_cannot_replace_an_overdue_cart_reservation(): void
|
||||
{
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$cart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$cart->addItem($variant->catalog_item_id, $variant->id, 2);
|
||||
$reservationId = $cart->fresh()->current_stock_reservation_id;
|
||||
$expiredAt = now()->subMinute()->startOfSecond();
|
||||
$cart->currentStockReservation()->update(['expires_at' => $expiredAt]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/compras/start-checkout', [
|
||||
'cart_id' => $cart->id,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertExactJson([
|
||||
'code' => 'stock_reservation.expired',
|
||||
'message' => __('api.cart.reservation_expired'),
|
||||
]);
|
||||
|
||||
$this->assertDatabaseCount('compras', 0);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'active',
|
||||
'expires_at' => $expiredAt->toDateTimeString(),
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cart->id,
|
||||
'current_stock_reservation_id' => $reservationId,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_starts_checkout_from_cart_with_purchase_item_snapshots(): void
|
||||
{
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
@@ -119,12 +195,7 @@ class StorePurchaseTest extends TestCase
|
||||
'precio_unitario' => '50.00',
|
||||
'total' => '100.00',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $inventory->id,
|
||||
'purchase_id' => $purchaseId,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchaseId, $inventory->id, 2, 'active');
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'user_id' => $user->id,
|
||||
@@ -168,12 +239,7 @@ class StorePurchaseTest extends TestCase
|
||||
'id' => $cartId,
|
||||
'current_purchase_id' => $replacementPurchaseId,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $inventory->id,
|
||||
'purchase_id' => $replacementPurchaseId,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($replacementPurchaseId, $inventory->id, 2, 'active');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $inventory->id,
|
||||
'reserved_stock' => 2,
|
||||
@@ -227,12 +293,7 @@ class StorePurchaseTest extends TestCase
|
||||
'precio_unitario' => '50.00',
|
||||
'total' => '150.00',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'purchase_id' => $response->json('data.id'),
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($response->json('data.id'), $variant->inventory_id, 3, 'active');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 10,
|
||||
@@ -244,10 +305,7 @@ class StorePurchaseTest extends TestCase
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
|
||||
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $response->json('data.id'),
|
||||
'status' => 'released',
|
||||
]);
|
||||
$this->assertPurchaseReservation($response->json('data.id'), $variant->inventory_id, 3, 'released');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 10,
|
||||
@@ -296,18 +354,8 @@ class StorePurchaseTest extends TestCase
|
||||
'origin' => Cart::ORIGIN_DIRECT_CHECKOUT,
|
||||
]);
|
||||
$this->assertDatabaseCount('compra_items', 2);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $firstVariant->inventory_id,
|
||||
'purchase_id' => $purchaseId,
|
||||
'quantity' => 1,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $secondVariant->inventory_id,
|
||||
'purchase_id' => $purchaseId,
|
||||
'quantity' => 1,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchaseId, $firstVariant->inventory_id, 1, 'active');
|
||||
$this->assertPurchaseReservation($purchaseId, $secondVariant->inventory_id, 1, 'active');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $firstVariant->inventory_id,
|
||||
'reserved_stock' => 1,
|
||||
@@ -430,7 +478,7 @@ class StorePurchaseTest extends TestCase
|
||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$firstVariant = $this->createVariantForTenant('sonder', 20, '50.00');
|
||||
$firstVariant->catalogItem->update(['max_units_per_user' => 3]);
|
||||
$firstVariant->catalogItem->update(['max_units_per_user' => 4]);
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 20]);
|
||||
$secondVariant = Variant::query()->create([
|
||||
'catalog_item_id' => $firstVariant->catalog_item_id,
|
||||
@@ -443,6 +491,7 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$cart->addItem($firstVariant->catalog_item_id, $firstVariant->id, 2);
|
||||
$cart->addItem($secondVariant->catalog_item_id, $secondVariant->id, 2);
|
||||
$firstVariant->catalogItem->update(['max_units_per_user' => 3]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/compras/start-checkout', [
|
||||
@@ -468,6 +517,7 @@ class StorePurchaseTest extends TestCase
|
||||
$user = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$purchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 3);
|
||||
$reservationId = $purchase->stock_reservation_id;
|
||||
$activeCart = Cart::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('status', 'active')
|
||||
@@ -500,27 +550,36 @@ class StorePurchaseTest extends TestCase
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
$activeCart->refresh();
|
||||
$this->assertSame($reservationId, $activeCart->current_stock_reservation_id);
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $purchase->id,
|
||||
'stock_reservation_id' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $cartItemId,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'id' => $activeCart->current_stock_reservation_id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'stock_reservation_id' => $activeCart->current_stock_reservation_id,
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 3,
|
||||
]);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
}
|
||||
|
||||
public function test_it_reassigns_a_terminal_purchase_reservation_and_rejects_a_late_confirmation(): void
|
||||
public function test_it_reuses_the_cart_reservation_for_a_new_checkout_and_rejects_a_late_confirmation(): void
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$previousPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2);
|
||||
$cart = $previousPurchase->cart;
|
||||
$previousReservationId = $previousPurchase->stock_reservation_id;
|
||||
|
||||
$previousPurchase->update([
|
||||
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||
'payment_method' => 'transfer',
|
||||
'expires_at' => now()->addMinutes(30),
|
||||
]);
|
||||
|
||||
$currentPurchase = app(CheckoutService::class)->startCheckout(
|
||||
@@ -532,17 +591,22 @@ class StorePurchaseTest extends TestCase
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $previousPurchase->id,
|
||||
'status' => Purchase::STATUS_SUPERSEDED,
|
||||
'expires_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cart->id,
|
||||
'current_purchase_id' => $currentPurchase->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $currentPurchase->id,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
$this->assertSame($previousReservationId, $currentPurchase->stock_reservation_id);
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $previousPurchase->id,
|
||||
'stock_reservation_id' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $previousReservationId,
|
||||
'status' => 'active',
|
||||
'release_reason' => null,
|
||||
]);
|
||||
$this->assertPurchaseReservation($currentPurchase->id, $variant->inventory_id, 2, 'active');
|
||||
|
||||
try {
|
||||
app(CheckoutService::class)->confirmPaidPurchase($previousPurchase->fresh());
|
||||
@@ -556,11 +620,7 @@ class StorePurchaseTest extends TestCase
|
||||
'reserved_stock' => 2,
|
||||
'sold_units' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $currentPurchase->id,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($currentPurchase->id, $variant->inventory_id, 2, 'active');
|
||||
}
|
||||
|
||||
public function test_removing_a_checkout_item_supersedes_the_purchase_and_restores_the_user_quota(): void
|
||||
@@ -589,11 +649,12 @@ class StorePurchaseTest extends TestCase
|
||||
'current_purchase_id' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'cart_item_id' => null,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 0,
|
||||
'status' => 'released',
|
||||
'release_reason' => 'cart_empty',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
@@ -607,7 +668,7 @@ class StorePurchaseTest extends TestCase
|
||||
$this->assertSame(3, $remaining);
|
||||
}
|
||||
|
||||
public function test_it_expires_the_purchase_without_mutating_the_active_cart(): void
|
||||
public function test_it_expires_the_purchase_and_preserves_the_expired_cart_items(): void
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
@@ -618,7 +679,7 @@ class StorePurchaseTest extends TestCase
|
||||
->where('status', 'active')
|
||||
->firstOrFail();
|
||||
$cartItemId = $purchase->cart->items()->firstOrFail()->id;
|
||||
$purchase->update(['expires_at' => now()->subMinute()]);
|
||||
$purchase->stockReservation()->update(['expires_at' => now()->subMinute()]);
|
||||
|
||||
$this->artisan('reservations:expire')->assertSuccessful();
|
||||
|
||||
@@ -628,22 +689,60 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $activeCart->id,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
'status' => Cart::STATUS_EXPIRED,
|
||||
'current_purchase_id' => null,
|
||||
'current_stock_reservation_id' => $purchase->stock_reservation_id,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 3, 'expired');
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/compras/start-checkout', [
|
||||
'cart_id' => $activeCart->id,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertExactJson([
|
||||
'code' => 'stock_reservation.expired',
|
||||
'message' => __('api.cart.reservation_expired'),
|
||||
]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson("/api/tenants/sonder/compras/{$purchase->id}/cancel")
|
||||
->assertUnprocessable()
|
||||
->assertJsonPath('code', 'purchase.expired');
|
||||
}
|
||||
|
||||
public function test_an_overdue_purchase_cannot_be_cancelled_before_the_expiration_job_runs(): void
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$purchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 1);
|
||||
$purchase->stockReservation()->update(['expires_at' => now()->subMinute()]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson("/api/tenants/sonder/compras/{$purchase->id}/cancel")
|
||||
->assertUnprocessable()
|
||||
->assertExactJson([
|
||||
'code' => 'stock_reservation.expired',
|
||||
'message' => __('api.cart.reservation_expired'),
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $purchase->id,
|
||||
'status' => Purchase::STATUS_CREATED,
|
||||
'stock_reservation_id' => $purchase->stock_reservation_id,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $cartItemId,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'id' => $purchase->stock_reservation_id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
}
|
||||
|
||||
public function test_it_keeps_cart_items_during_checkout_and_updates_customer_data(): void
|
||||
@@ -848,7 +947,6 @@ class StorePurchaseTest extends TestCase
|
||||
$purchase->update([
|
||||
'payment_method' => 'transfer',
|
||||
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||
'expires_at' => now()->addMinutes(30),
|
||||
]);
|
||||
|
||||
$url = "/api/tenants/sonder/compras/{$purchase->id}/review";
|
||||
@@ -862,6 +960,10 @@ class StorePurchaseTest extends TestCase
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $purchase->id,
|
||||
'status' => Purchase::STATUS_IN_REVIEW,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $purchase->stock_reservation_id,
|
||||
'status' => 'active',
|
||||
'expires_at' => null,
|
||||
]);
|
||||
|
||||
@@ -912,10 +1014,7 @@ class StorePurchaseTest extends TestCase
|
||||
'current_purchase_id' => null,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $purchase->id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 2, 'active');
|
||||
}
|
||||
|
||||
public function test_it_rejects_review_for_a_purchase_that_is_not_awaiting_payment(): void
|
||||
@@ -936,7 +1035,7 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_expires_an_abandoned_purchase_without_mutating_its_active_cart(): void
|
||||
public function test_it_expires_a_purchase_and_its_associated_cart(): void
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
@@ -947,7 +1046,7 @@ class StorePurchaseTest extends TestCase
|
||||
->where('status', 'active')
|
||||
->firstOrFail();
|
||||
|
||||
$this->assertNotNull($purchase->expires_at);
|
||||
$this->assertNotNull($purchase->stockReservation->expires_at);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
@@ -957,7 +1056,7 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 1')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('compras', [
|
||||
@@ -968,28 +1067,26 @@ class StorePurchaseTest extends TestCase
|
||||
'compra_id' => $purchase->id,
|
||||
'cantidad' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 3, 'expired');
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 10,
|
||||
'reserved_stock' => 3,
|
||||
'reserved_stock' => 0,
|
||||
'sold_units' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $activeCart->id,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
'status' => Cart::STATUS_EXPIRED,
|
||||
'current_purchase_id' => null,
|
||||
'current_stock_reservation_id' => $purchase->stock_reservation_id,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
}
|
||||
|
||||
@@ -1006,6 +1103,7 @@ class StorePurchaseTest extends TestCase
|
||||
'source_catalog_item_id' => $variant->catalog_item_id,
|
||||
'source_variant_id' => $variant->id,
|
||||
'nombre' => 'Inconsistent item',
|
||||
'item_nombre' => 'Inconsistent item',
|
||||
'slug' => 'inconsistent-item',
|
||||
'cantidad' => 1,
|
||||
'precio_unitario' => '50.00',
|
||||
@@ -1018,7 +1116,7 @@ class StorePurchaseTest extends TestCase
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 2')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('compras', [
|
||||
@@ -1160,12 +1258,7 @@ class StorePurchaseTest extends TestCase
|
||||
'reserved_stock' => 0,
|
||||
'sold_units' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $purchase->id,
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 2,
|
||||
'status' => 'committed',
|
||||
]);
|
||||
$this->assertPurchaseReservation($purchase->id, $variant->inventory_id, 2, 'committed');
|
||||
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
@@ -1440,6 +1533,26 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
protected function assertPurchaseReservation(
|
||||
int $purchaseId,
|
||||
int $inventoryId,
|
||||
int $quantity,
|
||||
string $status,
|
||||
): void {
|
||||
$reservationId = Purchase::query()->findOrFail($purchaseId)->stock_reservation_id;
|
||||
|
||||
$this->assertNotNull($reservationId);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => $status,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'stock_reservation_id' => $reservationId,
|
||||
'inventory_id' => $inventoryId,
|
||||
'quantity' => $quantity,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
||||
{
|
||||
$hdrKey = (string) Str::uuid();
|
||||
|
||||
Reference in New Issue
Block a user