fix(cart): propagate expired checkout purchase errors
This commit is contained in:
parent
944cf8ee18
commit
23163a6eb8
|
|
@ -7,6 +7,7 @@ use App\Domains\Cart\Models\Cart;
|
||||||
use App\Domains\Cart\Models\CartItem;
|
use App\Domains\Cart\Models\CartItem;
|
||||||
use App\Domains\Catalog\Services\StockReservationService;
|
use App\Domains\Catalog\Services\StockReservationService;
|
||||||
use App\Domains\Purchase\Models\Purchase;
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
|
use App\Domains\Purchase\Services\PurchaseStateGuard;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
@ -20,6 +21,7 @@ class CartService
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly StockReservationService $reservations,
|
private readonly StockReservationService $reservations,
|
||||||
|
private readonly PurchaseStateGuard $purchaseState,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function show(Tenant $tenant, Request $request): Cart
|
public function show(Tenant $tenant, Request $request): Cart
|
||||||
|
|
@ -116,10 +118,6 @@ class CartService
|
||||||
->where('cart_id', $cart->getKey())
|
->where('cart_id', $cart->getKey())
|
||||||
->where('tenant_codigo', $tenant->codigo)
|
->where('tenant_codigo', $tenant->codigo)
|
||||||
->where('user_id', $user->getKey())
|
->where('user_id', $user->getKey())
|
||||||
->whereIn('status', [
|
|
||||||
Purchase::STATUS_CREATED,
|
|
||||||
Purchase::STATUS_PENDING_PAYMENT,
|
|
||||||
])
|
|
||||||
->whereDoesntHave('items')
|
->whereDoesntHave('items')
|
||||||
->lockForUpdate()
|
->lockForUpdate()
|
||||||
->first();
|
->first();
|
||||||
|
|
@ -128,10 +126,13 @@ class CartService
|
||||||
throw new NotFoundHttpException('Checkout cart not found.');
|
throw new NotFoundHttpException('Checkout cart not found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($purchase->expires_at !== null && $purchase->expires_at->isPast()) {
|
$this->purchaseState->assertNotExpired($purchase);
|
||||||
throw ValidationException::withMessages([
|
|
||||||
'cart' => __('api.purchase.not_editable'),
|
if (! in_array($purchase->status, [
|
||||||
]);
|
Purchase::STATUS_CREATED,
|
||||||
|
Purchase::STATUS_PENDING_PAYMENT,
|
||||||
|
], true)) {
|
||||||
|
throw new NotFoundHttpException('Checkout cart not found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Cart|null $checkoutCart */
|
/** @var Cart|null $checkoutCart */
|
||||||
|
|
@ -246,10 +247,6 @@ class CartService
|
||||||
->where('cart_id', $cart->getKey())
|
->where('cart_id', $cart->getKey())
|
||||||
->where('tenant_codigo', $tenant->codigo)
|
->where('tenant_codigo', $tenant->codigo)
|
||||||
->where('user_id', $user->getKey())
|
->where('user_id', $user->getKey())
|
||||||
->whereIn('status', [
|
|
||||||
Purchase::STATUS_CREATED,
|
|
||||||
Purchase::STATUS_PENDING_PAYMENT,
|
|
||||||
])
|
|
||||||
->whereDoesntHave('items')
|
->whereDoesntHave('items')
|
||||||
->lockForUpdate()
|
->lockForUpdate()
|
||||||
->first();
|
->first();
|
||||||
|
|
@ -258,10 +255,13 @@ class CartService
|
||||||
throw new NotFoundHttpException('Checkout cart not found.');
|
throw new NotFoundHttpException('Checkout cart not found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($purchase->expires_at !== null && $purchase->expires_at->isPast()) {
|
$this->purchaseState->assertNotExpired($purchase);
|
||||||
throw ValidationException::withMessages([
|
|
||||||
'cart' => __('api.purchase.not_editable'),
|
if (! in_array($purchase->status, [
|
||||||
]);
|
Purchase::STATUS_CREATED,
|
||||||
|
Purchase::STATUS_PENDING_PAYMENT,
|
||||||
|
], true)) {
|
||||||
|
throw new NotFoundHttpException('Checkout cart not found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Cart|null $checkoutCart */
|
/** @var Cart|null $checkoutCart */
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,6 @@ Route::prefix('tenants/{tenant:codigo}')
|
||||||
Route::prefix('tenants/{tenant:codigo}')
|
Route::prefix('tenants/{tenant:codigo}')
|
||||||
->middleware('auth:sanctum')
|
->middleware('auth:sanctum')
|
||||||
->group(function (): void {
|
->group(function (): void {
|
||||||
Route::patch('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'updateCheckoutItem']);
|
Route::patch('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'updateCheckoutItem'])->withTrashed();
|
||||||
Route::delete('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'removeCheckoutItem']);
|
Route::delete('checkout-carts/{cart}/items/{cartItem}', [CartController::class, 'removeCheckoutItem'])->withTrashed();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue