refactor(checkout): materialize purchase items on confirmation
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Domains\Purchase\Services\Checkout;
|
||||
|
||||
use App\Domains\Cart\Models\CartItem;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Catalog\Services\CatalogInventoryService;
|
||||
use App\Domains\Catalog\Services\StockReservationService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Models\PurchaseItem;
|
||||
use App\Domains\Purchase\Services\UserPurchaseLimitService;
|
||||
@@ -15,6 +17,7 @@ class EditCheckoutService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CatalogInventoryService $inventory,
|
||||
private readonly StockReservationService $reservations,
|
||||
private readonly UserPurchaseLimitService $purchaseLimits,
|
||||
private readonly CatalogSelectionResolver $selections,
|
||||
private readonly SourceCartService $sourceCart,
|
||||
@@ -35,10 +38,10 @@ class EditCheckoutService
|
||||
|
||||
public function updateItemQuantity(
|
||||
Purchase $purchase,
|
||||
PurchaseItem $purchaseItem,
|
||||
int $itemId,
|
||||
int $quantity,
|
||||
): Purchase {
|
||||
return DB::transaction(function () use ($purchase, $purchaseItem, $quantity): Purchase {
|
||||
return DB::transaction(function () use ($purchase, $itemId, $quantity): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
|
||||
if ($purchase->status !== Purchase::STATUS_CREATED || $this->hasExpired($purchase)) {
|
||||
@@ -47,7 +50,11 @@ class EditCheckoutService
|
||||
]);
|
||||
}
|
||||
|
||||
$purchaseItem = $this->lockPurchaseItem($purchase, $purchaseItem);
|
||||
if (! $purchase->items()->exists()) {
|
||||
return $this->updateCartItemQuantity($purchase, $itemId, $quantity);
|
||||
}
|
||||
|
||||
$purchaseItem = $this->lockPurchaseItem($purchase, $itemId);
|
||||
$difference = $quantity - (int) $purchaseItem->cantidad;
|
||||
|
||||
if ($difference !== 0) {
|
||||
@@ -84,10 +91,66 @@ class EditCheckoutService
|
||||
),
|
||||
]);
|
||||
|
||||
if (! $purchase->items()->exists()) {
|
||||
$this->attachCartReservations($purchase);
|
||||
}
|
||||
|
||||
return $this->loadPurchase($purchase);
|
||||
});
|
||||
}
|
||||
|
||||
private function updateCartItemQuantity(Purchase $purchase, int $itemId, int $quantity): Purchase
|
||||
{
|
||||
$cart = $purchase->cart()->lockForUpdate()->first();
|
||||
if ($cart === null || $cart->status !== 'checkout') {
|
||||
throw new NotFoundHttpException('Checkout cart not found.');
|
||||
}
|
||||
|
||||
/** @var CartItem|null $cartItem */
|
||||
$cartItem = $cart->items()->whereKey($itemId)->lockForUpdate()->first();
|
||||
if ($cartItem === null) {
|
||||
throw new NotFoundHttpException('Checkout item not found.');
|
||||
}
|
||||
|
||||
$selection = $this->selections->resolve(
|
||||
$purchase->tenant,
|
||||
(int) $cartItem->catalog_item_id,
|
||||
$cartItem->variant_id === null ? null : (int) $cartItem->variant_id,
|
||||
'item',
|
||||
);
|
||||
$difference = $quantity - (int) $cartItem->cantidad;
|
||||
|
||||
try {
|
||||
if ($difference > 0) {
|
||||
$otherItemQuantity = (int) $cart->items()
|
||||
->where('catalog_item_id', $cartItem->catalog_item_id)
|
||||
->whereKeyNot($cartItem->getKey())
|
||||
->sum('cantidad');
|
||||
$catalogItem = $selection instanceof Variant ? $selection->catalogItem : $selection;
|
||||
$this->purchaseLimits->assertCanPurchase(
|
||||
$catalogItem,
|
||||
(int) $purchase->user_id,
|
||||
$otherItemQuantity + $quantity,
|
||||
$purchase->getKey(),
|
||||
);
|
||||
$this->reservations->reserve($cartItem, $selection, $difference);
|
||||
} elseif ($difference < 0) {
|
||||
$this->reservations->release($cartItem, $selection, abs($difference));
|
||||
}
|
||||
} catch (\InvalidArgumentException) {
|
||||
throw ValidationException::withMessages([
|
||||
'quantity' => __('api.purchase.insufficient_stock'),
|
||||
]);
|
||||
}
|
||||
|
||||
$cartItem->update(['cantidad' => $quantity]);
|
||||
$cart->unsetRelation('items');
|
||||
$purchase->setRelation('cart', $cart);
|
||||
$purchase->update(['total' => $cart->getTotalAmount()]);
|
||||
|
||||
return $this->loadPurchase($purchase);
|
||||
}
|
||||
|
||||
private function adjustReservation(
|
||||
Purchase $purchase,
|
||||
PurchaseItem $purchaseItem,
|
||||
@@ -121,11 +184,11 @@ class EditCheckoutService
|
||||
}
|
||||
}
|
||||
|
||||
private function lockPurchaseItem(Purchase $purchase, PurchaseItem $item): PurchaseItem
|
||||
private function lockPurchaseItem(Purchase $purchase, int $itemId): PurchaseItem
|
||||
{
|
||||
/** @var PurchaseItem|null $lockedItem */
|
||||
$lockedItem = $purchase->items()
|
||||
->whereKey($item->getKey())
|
||||
->whereKey($itemId)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
@@ -142,6 +205,20 @@ class EditCheckoutService
|
||||
return $lockedItem;
|
||||
}
|
||||
|
||||
private function attachCartReservations(Purchase $purchase): void
|
||||
{
|
||||
$cartItems = $purchase->cart?->items()->lockForUpdate()->get() ?? collect();
|
||||
foreach ($cartItems as $cartItem) {
|
||||
$selection = $this->selections->resolve(
|
||||
$purchase->tenant,
|
||||
(int) $cartItem->catalog_item_id,
|
||||
$cartItem->variant_id === null ? null : (int) $cartItem->variant_id,
|
||||
'item',
|
||||
);
|
||||
$this->reservations->attachToPurchase($cartItem, $selection, $purchase);
|
||||
}
|
||||
}
|
||||
|
||||
private function assertEditable(Purchase $purchase): void
|
||||
{
|
||||
if (
|
||||
@@ -170,6 +247,15 @@ class EditCheckoutService
|
||||
|
||||
private function loadPurchase(Purchase $purchase): Purchase
|
||||
{
|
||||
return $purchase->load(['items.imageAttachment']);
|
||||
return $purchase->load([
|
||||
'items.imageAttachment',
|
||||
'cart.items.catalogItem.inventory',
|
||||
'cart.items.catalogItem.attachments',
|
||||
'cart.items.variant.inventory',
|
||||
'cart.items.variant.attachments',
|
||||
'cart.items.variant.definitions.itemAttribute.attribute',
|
||||
'cart.items.variant.eventDates',
|
||||
'cart.items.variant.eventDate',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user