feat(checkout): enforce editing policy on items
This commit is contained in:
@@ -61,13 +61,13 @@ class EditCheckoutService
|
||||
$tenant = $purchase->tenant()->firstOrFail();
|
||||
$finalQuantity = $quantity ?? (int) $purchaseItem->cantidad;
|
||||
|
||||
if ($quantity !== null && ! $tenant->cart_editing_policy->allowsQuantityChanges()) {
|
||||
if ($quantity !== null && ! $tenant->checkout_editing_policy->allowsQuantityChanges()) {
|
||||
throw ValidationException::withMessages([
|
||||
'quantity' => __('api.cart.editing_disabled'),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($updateVariant && ! $tenant->cart_editing_policy->allowsVariantChanges()) {
|
||||
if ($updateVariant && ! $tenant->checkout_editing_policy->allowsVariantChanges()) {
|
||||
throw ValidationException::withMessages([
|
||||
'variant_id' => __('api.purchase.variant_change_disabled'),
|
||||
]);
|
||||
@@ -184,7 +184,7 @@ class EditCheckoutService
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
$this->assertEditable($purchase);
|
||||
|
||||
if (! $purchase->tenant()->firstOrFail()->cart_editing_policy->allowsModification()) {
|
||||
if (! $purchase->tenant()->firstOrFail()->checkout_editing_policy->allowsModification()) {
|
||||
throw ValidationException::withMessages([
|
||||
'purchase' => __('api.cart.editing_disabled'),
|
||||
]);
|
||||
@@ -204,6 +204,31 @@ class EditCheckoutService
|
||||
});
|
||||
}
|
||||
|
||||
public function removeItem(Purchase $purchase, PurchaseItem $purchaseItem): Purchase
|
||||
{
|
||||
return DB::transaction(function () use ($purchase, $purchaseItem): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
$this->assertEditable($purchase);
|
||||
|
||||
if (! $purchase->tenant()->firstOrFail()->checkout_editing_policy->allowsRemoval()) {
|
||||
throw ValidationException::withMessages([
|
||||
'item' => __('api.cart.editing_disabled'),
|
||||
]);
|
||||
}
|
||||
|
||||
$purchaseItem = $this->lockPurchaseItem($purchase, $purchaseItem);
|
||||
$selection = $this->selections->resolvePurchaseItem($purchase->tenant, $purchaseItem);
|
||||
$this->inventory->release($selection, (int) $purchaseItem->cantidad);
|
||||
$this->sourceCart->removeItem($purchase, $purchaseItem);
|
||||
$purchaseItem->delete();
|
||||
$purchase->update([
|
||||
'total' => $purchase->calculateCurrentTotalAmount(),
|
||||
]);
|
||||
|
||||
return $this->loadPurchase($purchase);
|
||||
});
|
||||
}
|
||||
|
||||
private function adjustReservation(
|
||||
Purchase $purchase,
|
||||
PurchaseItem $purchaseItem,
|
||||
|
||||
Reference in New Issue
Block a user