From 786cf2db522d737c368d0b252ada2282741b6ed5 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Thu, 20 Aug 2026 17:01:21 -0300 Subject: [PATCH] fix(cart): propagate expired checkout purchase errors --- app/Domains/Cart/Services/CartService.php | 32 +++++++++++------------ app/Domains/Cart/routes/api.php | 4 +-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/Domains/Cart/Services/CartService.php b/app/Domains/Cart/Services/CartService.php index 044b11a..c6b1e79 100644 --- a/app/Domains/Cart/Services/CartService.php +++ b/app/Domains/Cart/Services/CartService.php @@ -7,6 +7,7 @@ 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\Services\PurchaseStateGuard; use App\Domains\Tenant\Models\Tenant; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -20,6 +21,7 @@ class CartService { public function __construct( private readonly StockReservationService $reservations, + private readonly PurchaseStateGuard $purchaseState, ) {} public function show(Tenant $tenant, Request $request): Cart @@ -116,10 +118,6 @@ class CartService ->where('cart_id', $cart->getKey()) ->where('tenant_codigo', $tenant->codigo) ->where('user_id', $user->getKey()) - ->whereIn('status', [ - Purchase::STATUS_CREATED, - Purchase::STATUS_PENDING_PAYMENT, - ]) ->whereDoesntHave('items') ->lockForUpdate() ->first(); @@ -128,10 +126,13 @@ class CartService throw new NotFoundHttpException('Checkout cart not found.'); } - if ($purchase->expires_at !== null && $purchase->expires_at->isPast()) { - throw ValidationException::withMessages([ - 'cart' => __('api.purchase.not_editable'), - ]); + $this->purchaseState->assertNotExpired($purchase); + + if (! in_array($purchase->status, [ + Purchase::STATUS_CREATED, + Purchase::STATUS_PENDING_PAYMENT, + ], true)) { + throw new NotFoundHttpException('Checkout cart not found.'); } /** @var Cart|null $checkoutCart */ @@ -246,10 +247,6 @@ class CartService ->where('cart_id', $cart->getKey()) ->where('tenant_codigo', $tenant->codigo) ->where('user_id', $user->getKey()) - ->whereIn('status', [ - Purchase::STATUS_CREATED, - Purchase::STATUS_PENDING_PAYMENT, - ]) ->whereDoesntHave('items') ->lockForUpdate() ->first(); @@ -258,10 +255,13 @@ class CartService throw new NotFoundHttpException('Checkout cart not found.'); } - if ($purchase->expires_at !== null && $purchase->expires_at->isPast()) { - throw ValidationException::withMessages([ - 'cart' => __('api.purchase.not_editable'), - ]); + $this->purchaseState->assertNotExpired($purchase); + + if (! in_array($purchase->status, [ + Purchase::STATUS_CREATED, + Purchase::STATUS_PENDING_PAYMENT, + ], true)) { + throw new NotFoundHttpException('Checkout cart not found.'); } /** @var Cart|null $checkoutCart */ diff --git a/app/Domains/Cart/routes/api.php b/app/Domains/Cart/routes/api.php index 2e03f58..07e73f0 100644 --- a/app/Domains/Cart/routes/api.php +++ b/app/Domains/Cart/routes/api.php @@ -14,6 +14,6 @@ Route::prefix('tenants/{tenant:codigo}') Route::prefix('tenants/{tenant:codigo}') ->middleware('auth:sanctum') ->group(function (): void { - Route::patch('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'updateCheckoutItem']); - Route::delete('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'removeCheckoutItem']); + Route::patch('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'updateCheckoutItem'])->withTrashed(); + Route::delete('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'removeCheckoutItem'])->withTrashed(); });