feat(checkout): restore source cart on cancellation
This commit is contained in:
@@ -16,22 +16,21 @@ class ReleaseCheckoutService
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly StockReservationService $reservations,
|
private readonly StockReservationService $reservations,
|
||||||
private readonly SourceCartService $sourceCart,
|
private readonly SourceCartService $sourceCart,
|
||||||
private readonly PurchaseStateGuard $purchaseState,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function cancel(Purchase $purchase): Purchase
|
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
|
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
|
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
|
public function expireOverdue(): int
|
||||||
@@ -68,9 +67,9 @@ class ReleaseCheckoutService
|
|||||||
return $expiredCount;
|
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);
|
$purchase = $this->lockPurchase($purchase);
|
||||||
|
|
||||||
if ($targetStatus !== Purchase::STATUS_EXPIRED) {
|
if ($targetStatus !== Purchase::STATUS_EXPIRED) {
|
||||||
@@ -104,7 +103,8 @@ class ReleaseCheckoutService
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->releaseCartReservations($purchase, $targetStatus);
|
$reservationReturnedToCart = $restoreCart && $this->sourceCart->restore($purchase);
|
||||||
|
$this->releaseCartReservations($purchase, $reservationReturnedToCart, $targetStatus);
|
||||||
|
|
||||||
$purchase->update(['status' => $targetStatus]);
|
$purchase->update(['status' => $targetStatus]);
|
||||||
|
|
||||||
@@ -114,8 +114,15 @@ class ReleaseCheckoutService
|
|||||||
|
|
||||||
private function releaseCartReservations(
|
private function releaseCartReservations(
|
||||||
Purchase $purchase,
|
Purchase $purchase,
|
||||||
|
bool $reservationReturnedToCart,
|
||||||
string $targetStatus,
|
string $targetStatus,
|
||||||
): void {
|
): void {
|
||||||
|
if ($reservationReturnedToCart) {
|
||||||
|
$this->reservations->detachFromPurchase($purchase);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$cart = $purchase->cart()->withTrashed()->lockForUpdate()->first();
|
$cart = $purchase->cart()->withTrashed()->lockForUpdate()->first();
|
||||||
if ($cart === null) {
|
if ($cart === null) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -3,10 +3,65 @@
|
|||||||
namespace App\Domains\Purchase\Services\Checkout;
|
namespace App\Domains\Purchase\Services\Checkout;
|
||||||
|
|
||||||
use App\Domains\Cart\Models\Cart;
|
use App\Domains\Cart\Models\Cart;
|
||||||
|
use App\Domains\Cart\Models\CartItem;
|
||||||
|
use App\Domains\Catalog\Services\StockReservationService;
|
||||||
use App\Domains\Purchase\Models\Purchase;
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
|
|
||||||
class SourceCartService
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @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;
|
||||||
|
}
|
||||||
|
|
||||||
public function syncItemQuantity(
|
public function syncItemQuantity(
|
||||||
Purchase $purchase,
|
Purchase $purchase,
|
||||||
PurchaseItem $purchaseItem,
|
PurchaseItem $purchaseItem,
|
||||||
@@ -116,4 +171,30 @@ class SourceCartService
|
|||||||
->lockForUpdate()
|
->lockForUpdate()
|
||||||
->first();
|
->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user