Compare commits
3 Commits
fix/expira
...
homo
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a2dc8b7e0 | |||
| 0510c1aa12 | |||
| 9d870be294 |
@@ -118,6 +118,37 @@ class StockReservationService
|
||||
]);
|
||||
}
|
||||
|
||||
public function restore(CartItem $cartItem, CatalogItem|Variant $selection): void
|
||||
{
|
||||
DB::transaction(function () use ($cartItem, $selection): void {
|
||||
$requirements = $this->inventory->requirementsFor(
|
||||
$selection,
|
||||
(int) $cartItem->cantidad,
|
||||
);
|
||||
$activeReservations = StockReservation::query()
|
||||
->where('cart_item_id', $cartItem->getKey())
|
||||
->where('status', StockReservation::STATUS_ACTIVE)
|
||||
->lockForUpdate()
|
||||
->get()
|
||||
->keyBy('inventory_id');
|
||||
|
||||
$hasCompleteReservation = collect($requirements)->every(
|
||||
fn (int $quantity, int $inventoryId): bool => (int) ($activeReservations->get($inventoryId)?->quantity ?? 0) === $quantity,
|
||||
);
|
||||
|
||||
if ($hasCompleteReservation) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($activeReservations->isNotEmpty()) {
|
||||
throw new \InvalidArgumentException('La reserva de stock del carrito es inconsistente.');
|
||||
}
|
||||
|
||||
$this->inventory->reserve($selection, (int) $cartItem->cantidad);
|
||||
$this->recordIncrease($cartItem, $selection, (int) $cartItem->cantidad);
|
||||
});
|
||||
}
|
||||
|
||||
public function syncPurchaseExpiration(Purchase $purchase): void
|
||||
{
|
||||
StockReservation::query()
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace App\Domains\Purchase\Services\Checkout;
|
||||
use App\Domains\Catalog\Models\StockReservation;
|
||||
use App\Domains\Catalog\Services\StockReservationService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Services\PurchaseStateGuard;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -16,22 +15,21 @@ class ReleaseCheckoutService
|
||||
public function __construct(
|
||||
private readonly StockReservationService $reservations,
|
||||
private readonly SourceCartService $sourceCart,
|
||||
private readonly PurchaseStateGuard $purchaseState,
|
||||
) {}
|
||||
|
||||
public function cancel(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED);
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED, restoreCart: true);
|
||||
}
|
||||
|
||||
public function cancelWithoutRestoringCart(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED);
|
||||
return $this->release($purchase, Purchase::STATUS_CANCELLED, restoreCart: false);
|
||||
}
|
||||
|
||||
public function expire(Purchase $purchase): Purchase
|
||||
{
|
||||
return $this->release($purchase, Purchase::STATUS_EXPIRED);
|
||||
return $this->release($purchase, Purchase::STATUS_EXPIRED, restoreCart: false);
|
||||
}
|
||||
|
||||
public function expireOverdue(): int
|
||||
@@ -68,12 +66,12 @@ class ReleaseCheckoutService
|
||||
return $expiredCount;
|
||||
}
|
||||
|
||||
private function release(Purchase $purchase, string $targetStatus): Purchase
|
||||
private function release(Purchase $purchase, string $targetStatus, bool $restoreCart): Purchase
|
||||
{
|
||||
return DB::transaction(function () use ($purchase, $targetStatus): Purchase {
|
||||
return DB::transaction(function () use ($purchase, $targetStatus, $restoreCart): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
|
||||
if ($targetStatus !== Purchase::STATUS_EXPIRED) {
|
||||
if ($targetStatus !== Purchase::STATUS_EXPIRED && ! $restoreCart) {
|
||||
$this->purchaseState->assertNotExpired($purchase);
|
||||
}
|
||||
|
||||
@@ -87,7 +85,7 @@ class ReleaseCheckoutService
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->isAlreadyReleased($purchase)) {
|
||||
if ($this->isAlreadyReleased($purchase) && ! ($restoreCart && $purchase->status === Purchase::STATUS_EXPIRED)) {
|
||||
return $this->loadPurchase($purchase);
|
||||
}
|
||||
|
||||
@@ -104,7 +102,8 @@ class ReleaseCheckoutService
|
||||
]);
|
||||
}
|
||||
|
||||
$this->releaseCartReservations($purchase, $targetStatus);
|
||||
$reservationReturnedToCart = $restoreCart && $this->sourceCart->restore($purchase);
|
||||
$this->releaseCartReservations($purchase, $reservationReturnedToCart, $targetStatus);
|
||||
|
||||
$purchase->update(['status' => $targetStatus]);
|
||||
|
||||
@@ -114,8 +113,15 @@ class ReleaseCheckoutService
|
||||
|
||||
private function releaseCartReservations(
|
||||
Purchase $purchase,
|
||||
bool $reservationReturnedToCart,
|
||||
string $targetStatus,
|
||||
): void {
|
||||
if ($reservationReturnedToCart) {
|
||||
$this->reservations->detachFromPurchase($purchase);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$cart = $purchase->cart()->withTrashed()->lockForUpdate()->first();
|
||||
if ($cart === null) {
|
||||
return;
|
||||
|
||||
@@ -3,10 +3,86 @@
|
||||
namespace App\Domains\Purchase\Services\Checkout;
|
||||
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Cart\Models\CartItem;
|
||||
use App\Domains\Catalog\Services\StockReservationService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
|
||||
class SourceCartService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly StockReservationService $reservations,
|
||||
) {}
|
||||
|
||||
public function restore(Purchase $purchase): bool
|
||||
{
|
||||
$sourceCart = $this->findSourceCart($purchase);
|
||||
|
||||
if ($sourceCart === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($sourceCart->origin === Cart::ORIGIN_DIRECT_CHECKOUT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->restoreReservations($sourceCart);
|
||||
|
||||
/** @var Cart|null $activeCart */
|
||||
$activeCart = Cart::query()
|
||||
->where('tenant_codigo', $purchase->tenant_codigo)
|
||||
->where('user_id', $purchase->user_id)
|
||||
->where('status', 'active')
|
||||
->where('id', '!=', $sourceCart->getKey())
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if ($activeCart !== null) {
|
||||
$this->mergeIntoActiveCart($sourceCart, $activeCart);
|
||||
|
||||
$sourceCart->update([
|
||||
'status' => 'converted',
|
||||
'guest_token' => null,
|
||||
]);
|
||||
|
||||
if (! $sourceCart->trashed()) {
|
||||
$sourceCart->delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($sourceCart->trashed()) {
|
||||
$sourceCart->restore();
|
||||
}
|
||||
|
||||
$sourceCart->update([
|
||||
'status' => 'active',
|
||||
'user_id' => $purchase->user_id,
|
||||
'guest_token' => null,
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function restoreReservations(Cart $cart): void
|
||||
{
|
||||
$items = $cart->items()->orderBy('id')->lockForUpdate()->get();
|
||||
$items->load([
|
||||
'catalogItem.inventory',
|
||||
'catalogItem.bundleComponents.catalogItem.inventory',
|
||||
'catalogItem.bundleComponents.variant.inventory',
|
||||
'variant.inventory',
|
||||
'variant.catalogItem',
|
||||
]);
|
||||
|
||||
foreach ($items as $item) {
|
||||
$selection = $item->selectedItem();
|
||||
if ($selection !== null) {
|
||||
$this->reservations->restore($item, $selection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function syncItemQuantity(
|
||||
Purchase $purchase,
|
||||
PurchaseItem $purchaseItem,
|
||||
@@ -116,4 +192,30 @@ class SourceCartService
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
}
|
||||
|
||||
private function mergeIntoActiveCart(Cart $sourceCart, Cart $activeCart): void
|
||||
{
|
||||
$sourceItems = $sourceCart->items()->lockForUpdate()->get();
|
||||
|
||||
foreach ($sourceItems as $sourceItem) {
|
||||
/** @var CartItem|null $activeItem */
|
||||
$activeItem = $activeCart->items()
|
||||
->where('catalog_item_id', $sourceItem->catalog_item_id)
|
||||
->where('variant_id', $sourceItem->variant_id)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if ($activeItem === null) {
|
||||
$activeItem = $activeCart->items()->create([
|
||||
'catalog_item_id' => $sourceItem->catalog_item_id,
|
||||
'variant_id' => $sourceItem->variant_id,
|
||||
'cantidad' => $sourceItem->cantidad,
|
||||
]);
|
||||
} else {
|
||||
$activeItem->increment('cantidad', (int) $sourceItem->cantidad);
|
||||
}
|
||||
|
||||
$this->reservations->transfer($sourceItem, $activeItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,7 +412,7 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_releases_and_closes_the_checkout_cart_when_purchase_is_cancelled(): void
|
||||
public function test_it_restores_the_source_cart_when_checkout_is_cancelled(): void
|
||||
{
|
||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||
$user = User::factory()->create();
|
||||
@@ -430,10 +430,11 @@ class StorePurchaseTest extends TestCase
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Purchase::STATUS_CANCELLED);
|
||||
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'converted',
|
||||
'status' => 'active',
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'cart_id' => $purchase->cart_id,
|
||||
@@ -443,16 +444,47 @@ class StorePurchaseTest extends TestCase
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'purchase_id' => $purchase->id,
|
||||
'quantity' => 0,
|
||||
'status' => 'released',
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_keeps_a_new_active_cart_separate_when_checkout_is_cancelled(): void
|
||||
public function test_it_restores_and_reserves_the_source_cart_when_an_expired_checkout_is_modified(): 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);
|
||||
$purchase->update(['expires_at' => now()->subMinute()]);
|
||||
|
||||
$this->artisan('reservations:expire')->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('compras', [
|
||||
'id' => $purchase->id,
|
||||
'status' => Purchase::STATUS_EXPIRED,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
|
||||
$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,
|
||||
'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();
|
||||
@@ -484,15 +516,11 @@ class StorePurchaseTest extends TestCase
|
||||
'cart_id' => $activeCartId,
|
||||
'catalog_item_id' => $variant->catalog_item_id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
]);
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
'id' => $purchase->cart_id,
|
||||
'status' => 'converted',
|
||||
'cantidad' => 3,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 1,
|
||||
'reserved_stock' => 3,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user