feat(checkout): enhance cancellation logic to handle unavailable variants and update cart status
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Domains\Purchase\Services\Checkout;
|
||||
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Cart\Models\CartItem;
|
||||
use App\Domains\Catalog\Models\StockReservation;
|
||||
use App\Domains\Catalog\Services\StockReservationService;
|
||||
use App\Domains\Purchase\Exceptions\PurchaseExpiredException;
|
||||
@@ -91,7 +92,16 @@ class ReleaseCheckoutService
|
||||
Purchase::STATUS_CREATED,
|
||||
Purchase::STATUS_PENDING_PAYMENT,
|
||||
], true)) {
|
||||
$this->reservations->returnToCart($purchase, $cart);
|
||||
if ($this->hasUnavailableVariants($cart)) {
|
||||
$this->releasePurchaseReservations($purchase, $targetStatus, $cart);
|
||||
$cart->update([
|
||||
'status' => Cart::STATUS_EXPIRED,
|
||||
'current_purchase_id' => null,
|
||||
'current_stock_reservation_id' => null,
|
||||
]);
|
||||
} else {
|
||||
$this->reservations->returnToCart($purchase, $cart);
|
||||
}
|
||||
$purchase->update(['status' => Purchase::STATUS_CANCELLED]);
|
||||
|
||||
return $this->loadPurchase($purchase);
|
||||
@@ -115,6 +125,17 @@ class ReleaseCheckoutService
|
||||
});
|
||||
}
|
||||
|
||||
private function hasUnavailableVariants(Cart $cart): bool
|
||||
{
|
||||
return $cart->items()
|
||||
->whereNotNull('variant_id')
|
||||
->with(['variant.eventDate', 'variant.eventDates'])
|
||||
->lockForUpdate()
|
||||
->get()
|
||||
->contains(fn (CartItem $item): bool => $item->variant === null
|
||||
|| ! $item->variant->isSellable());
|
||||
}
|
||||
|
||||
private function releasePurchaseReservations(
|
||||
Purchase $purchase,
|
||||
string $targetStatus,
|
||||
|
||||
Reference in New Issue
Block a user