refactor(stock): centralize reservation aggregate

This commit is contained in:
2026-08-25 15:05:23 -03:00
parent 8a0f29bdae
commit bbfdf8f342
6 changed files with 64 additions and 142 deletions

View File

@@ -206,7 +206,7 @@ class StartCheckoutService
$cart->getKey(),
);
$cart->update(['current_purchase_id' => $purchase->getKey()]);
$this->reservations->attachToPurchase($cart, $purchase, $this->checkoutExpiration());
$this->reservations->attachToPurchase($cart, $purchase);
$cartItems = $cart->items()->orderBy('id')->lockForUpdate()->get();
$this->loadCartItems($cartItems);
@@ -272,7 +272,7 @@ class StartCheckoutService
$cart->getKey(),
);
$cart->update(['current_purchase_id' => $purchase->getKey()]);
$this->reservations->attachToPurchase($cart, $purchase, $this->checkoutExpiration());
$this->reservations->attachToPurchase($cart, $purchase);
$purchase->items()->createMany($this->snapshots->fromCartItems($cartItems));
return $this->loadPurchase($purchase);
@@ -319,6 +319,14 @@ class StartCheckoutService
$currentPurchase->update([
'status' => Purchase::STATUS_SUPERSEDED,
]);
$this->reservations->releaseForPurchase(
$currentPurchase,
reason: StockReservationService::REASON_PURCHASE_SUPERSEDED,
);
$cart->update([
'current_purchase_id' => null,
'current_stock_reservation_id' => null,
]);
}
return $cart;