refactor(checkout): keep source cart active
This commit is contained in:
@@ -126,7 +126,8 @@ class StorePurchaseTest extends TestCase
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'checkout',
|
||||
'status' => 'active',
|
||||
'origin' => Cart::ORIGIN_USER,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
@@ -141,6 +142,21 @@ class StorePurchaseTest extends TestCase
|
||||
'reserved_stock' => 2,
|
||||
]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->getJson('/api/tenants/sonder/cart')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.id', $cartId)
|
||||
->assertJsonPath('data.status', 'active')
|
||||
->assertJsonCount(1, 'data.items')
|
||||
->assertJsonPath('data.items.0.cantidad', 2);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/compras/start-checkout', [
|
||||
'cart_id' => $cartId,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors('cart_id');
|
||||
|
||||
$catalogItem->update([
|
||||
'nombre' => 'Updated Product',
|
||||
'precio' => '75.00',
|
||||
@@ -423,18 +439,26 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_restores_the_source_cart_when_checkout_is_cancelled(): void
|
||||
public function test_it_keeps_the_source_cart_active_and_only_cancels_the_purchase(): 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, 3);
|
||||
$activeCart = Cart::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('status', 'active')
|
||||
->firstOrFail();
|
||||
$cartItemId = $purchase->cart->items()->firstOrFail()->id;
|
||||
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
'status' => 'checkout',
|
||||
'status' => 'active',
|
||||
'origin' => Cart::ORIGIN_USER,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseCount('carrito_items', 1);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson("/api/tenants/sonder/compras/{$purchase->id}/cancel")
|
||||
@@ -442,29 +466,35 @@ class StorePurchaseTest extends TestCase
|
||||
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
|
||||
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
'id' => $activeCart->id,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'cart_id' => $purchase->cart_id,
|
||||
'catalog_item_id' => $variant->catalog_item_id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $cartItemId,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
}
|
||||
|
||||
public function test_it_restores_and_reserves_the_source_cart_when_an_expired_checkout_is_modified(): void
|
||||
public function test_it_expires_the_purchase_without_mutating_the_active_cart(): 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, 3);
|
||||
$activeCart = Cart::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('status', 'active')
|
||||
->firstOrFail();
|
||||
$cartItemId = $purchase->cart->items()->firstOrFail()->id;
|
||||
$purchase->update(['expires_at' => now()->subMinute()]);
|
||||
|
||||
$this->artisan('reservations:expire')->assertSuccessful();
|
||||
@@ -475,64 +505,21 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 0,
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson("/api/tenants/sonder/compras/{$purchase->id}/cancel")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
|
||||
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
'id' => $activeCart->id,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_merges_the_checkout_cart_when_the_user_created_another_active_cart(): 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, 2);
|
||||
|
||||
$activeCartId = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/sonder/cart/items', [
|
||||
'catalog_item_id' => $variant->catalog_item_id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk()
|
||||
->json('data.id');
|
||||
|
||||
$this->assertNotSame($purchase->cart_id, $activeCartId);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->postJson("/api/tenants/sonder/compras/{$purchase->id}/cancel")
|
||||
->assertOk();
|
||||
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $activeCartId,
|
||||
'user_id' => $user->id,
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $cartItemId,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'cart_id' => $activeCartId,
|
||||
'catalog_item_id' => $variant->catalog_item_id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
}
|
||||
|
||||
public function test_it_keeps_cart_items_during_checkout_and_updates_customer_data(): void
|
||||
@@ -783,12 +770,16 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_expires_an_abandoned_purchase_and_releases_its_checkout_cart(): void
|
||||
public function test_it_expires_an_abandoned_purchase_without_mutating_its_active_cart(): 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, 3);
|
||||
$activeCart = Cart::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('status', 'active')
|
||||
->firstOrFail();
|
||||
|
||||
$this->assertNotNull($purchase->expires_at);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
@@ -812,21 +803,23 @@ class StorePurchaseTest extends TestCase
|
||||
'cantidad' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $purchase->id,
|
||||
'quantity' => 0,
|
||||
'status' => 'expired',
|
||||
'purchase_id' => null,
|
||||
'quantity' => 3,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'real_stock' => 10,
|
||||
'reserved_stock' => 0,
|
||||
'reserved_stock' => 3,
|
||||
'sold_units' => 0,
|
||||
]);
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $activeCart->id,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'converted',
|
||||
'status' => 'active',
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertSame(1, $activeCart->items()->count());
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
@@ -838,9 +831,10 @@ class StorePurchaseTest extends TestCase
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
$secondUser = User::factory()->create();
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
$inconsistentPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 1);
|
||||
$validPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 1);
|
||||
$validPurchase = $this->createCheckoutPurchase($secondUser, 'sonder', $variant, 1);
|
||||
|
||||
$inconsistentPurchase->items()->create([
|
||||
'source_catalog_item_id' => $variant->catalog_item_id,
|
||||
@@ -956,12 +950,10 @@ class StorePurchaseTest extends TestCase
|
||||
public function test_created_and_pending_payment_purchase_details_ignore_later_cart_changes(): void
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create([
|
||||
'email' => 'buyer@example.com',
|
||||
]);
|
||||
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||
|
||||
foreach ([Purchase::STATUS_CREATED, Purchase::STATUS_PENDING_PAYMENT] as $status) {
|
||||
$user = User::factory()->create();
|
||||
$purchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2);
|
||||
$purchase->cart->items()->update(['cantidad' => 3]);
|
||||
$purchase->update(['status' => $status]);
|
||||
|
||||
Reference in New Issue
Block a user