refactor(cart): own checkout item editing
This commit is contained in:
@@ -2,23 +2,12 @@
|
||||
|
||||
namespace App\Domains\Purchase\Services\Checkout;
|
||||
|
||||
use App\Domains\Cart\Models\CartItem;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Catalog\Services\StockReservationService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Services\UserPurchaseLimitService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class EditCheckoutService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly StockReservationService $reservations,
|
||||
private readonly UserPurchaseLimitService $purchaseLimits,
|
||||
private readonly CatalogSelectionResolver $selections,
|
||||
) {}
|
||||
|
||||
/** @param array<string, string> $customerData */
|
||||
public function updateCustomer(Purchase $purchase, array $customerData): Purchase
|
||||
{
|
||||
@@ -32,112 +21,6 @@ class EditCheckoutService
|
||||
});
|
||||
}
|
||||
|
||||
public function updateItemQuantity(
|
||||
Purchase $purchase,
|
||||
int $itemId,
|
||||
int $quantity,
|
||||
): Purchase {
|
||||
return DB::transaction(function () use ($purchase, $itemId, $quantity): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
|
||||
if ($purchase->status !== Purchase::STATUS_CREATED || $this->hasExpired($purchase)) {
|
||||
throw ValidationException::withMessages([
|
||||
'purchase' => __('api.purchase.not_editable'),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->updateCartItemQuantity($purchase, $itemId, $quantity);
|
||||
});
|
||||
}
|
||||
|
||||
public function prepareItemEditing(Purchase $purchase): Purchase
|
||||
{
|
||||
return DB::transaction(function () use ($purchase): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
$this->assertEditable($purchase);
|
||||
|
||||
$purchase->telepagosQr()->delete();
|
||||
$purchase->update([
|
||||
'status' => Purchase::STATUS_CREATED,
|
||||
'payment_method' => null,
|
||||
'transfer_payer_dni' => null,
|
||||
'expires_at' => now()->addMinutes(
|
||||
max(1, (int) config('purchase.checkout_expiration_minutes', 30)),
|
||||
),
|
||||
]);
|
||||
|
||||
$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 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 (
|
||||
|
||||
Reference in New Issue
Block a user