refactor(checkout): remove legacy purchase item reservations

This commit is contained in:
2026-08-19 12:14:57 -03:00
parent f1649e0e4b
commit aed99bd05e
13 changed files with 67 additions and 217 deletions

View File

@@ -3,19 +3,15 @@
namespace App\Domains\Purchase\Services\Checkout;
use App\Domains\Catalog\Models\StockReservation;
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 Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class ReleaseCheckoutService
{
public function __construct(
private readonly CatalogInventoryService $inventory,
private readonly StockReservationService $reservations,
private readonly CatalogSelectionResolver $selections,
private readonly SourceCartService $sourceCart,
) {}
@@ -80,29 +76,15 @@ class ReleaseCheckoutService
return $this->loadPurchase($purchase);
}
$items = $purchase->items()
->where('reservation_status', PurchaseItem::RESERVATION_ACTIVE)
->lockForUpdate()
->get();
$reservationReturnedToCart = $restoreCart && $this->sourceCart->restore($purchase);
if ($items->isEmpty() && ! $purchase->items()->exists()) {
$this->releaseCartReservations($purchase, $reservationReturnedToCart, $targetStatus);
$purchase->update(['status' => $targetStatus]);
return $this->loadPurchase($purchase);
}
foreach ($items as $item) {
if (! $reservationReturnedToCart) {
$this->releaseInventory($purchase, $item);
}
$item->update([
'reservation_status' => PurchaseItem::RESERVATION_RELEASED,
if ($purchase->items()->exists()) {
throw ValidationException::withMessages([
'items' => __('api.purchase.inconsistent_reservation'),
]);
}
$reservationReturnedToCart = $restoreCart && $this->sourceCart->restore($purchase);
$this->releaseCartReservations($purchase, $reservationReturnedToCart, $targetStatus);
$purchase->update(['status' => $targetStatus]);
return $this->loadPurchase($purchase);
@@ -162,19 +144,6 @@ class ReleaseCheckoutService
}
}
private function releaseInventory(Purchase $purchase, PurchaseItem $item): void
{
$selection = $this->selections->resolvePurchaseItem($purchase->tenant, $item);
try {
$this->inventory->release($selection, (int) $item->cantidad);
} catch (\InvalidArgumentException) {
throw ValidationException::withMessages([
'items' => __('api.purchase.inconsistent_reservation'),
]);
}
}
private function isAlreadyReleased(Purchase $purchase): bool
{
return in_array($purchase->status, [