feat(cart): implement user purchase limit validation for cart items and add related tests

This commit is contained in:
2026-08-06 16:43:04 -03:00
parent 5ad61eb217
commit f578c6e24a
6 changed files with 251 additions and 30 deletions

View File

@@ -7,6 +7,7 @@ use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Catalog\Services\CatalogInventoryService;
use App\Domains\Purchase\Services\UserPurchaseLimitService;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -86,6 +87,10 @@ class Cart extends Model
return DB::transaction(function () use ($catalogItemId, $variantId, $quantity): CartItem {
self::query()->whereKey($this->getKey())->lockForUpdate()->firstOrFail();
$selectedItem = $this->resolveScopedItem($catalogItemId, $variantId, true);
$cartQuantity = (int) $this->items()
->where('catalog_item_id', $catalogItemId)
->sum('cantidad');
$this->assertUserPurchaseLimit($selectedItem, $cartQuantity + $quantity);
$inventoryService = app(CatalogInventoryService::class);
$availableQuantity = $inventoryService->availableQuantity($selectedItem);
@@ -141,6 +146,18 @@ class Cart extends Model
);
$inventoryService = app(CatalogInventoryService::class);
$delta = $quantity - $item->cantidad;
if ($delta > 0) {
$otherVariantsQuantity = (int) $this->items()
->where('catalog_item_id', $item->catalog_item_id)
->whereKeyNot($item->getKey())
->sum('cantidad');
$this->assertUserPurchaseLimit(
$selectedItem,
$otherVariantsQuantity + $quantity,
);
}
$availableQuantity = $inventoryService->availableQuantity($selectedItem);
if ($delta > 0 && $availableQuantity !== null && $availableQuantity < $delta) {
@@ -256,6 +273,26 @@ class Cart extends Model
return $variant;
}
private function assertUserPurchaseLimit(
CatalogItem|Variant $selectedItem,
int $cartQuantity,
): void {
if ($this->user_id === null) {
return;
}
$catalogItem = $selectedItem instanceof Variant
? $selectedItem->catalogItem
: $selectedItem;
app(UserPurchaseLimitService::class)->assertCanPurchase(
$catalogItem,
$this->user_id,
$cartQuantity,
field: 'cantidad',
);
}
protected function resolveInventory(int $inventoryId, bool $lockForUpdate): Inventory
{
$query = Inventory::query()->whereKey($inventoryId);

View File

@@ -21,6 +21,7 @@ class CheckoutService
{
public function __construct(
private readonly CatalogInventoryService $catalogInventoryService,
private readonly UserPurchaseLimitService $userPurchaseLimitService,
) {}
public function startCheckout(Tenant $tenant, int $userId, array $purchaseData): Purchase
@@ -709,34 +710,13 @@ class CheckoutService
?int $excludedPurchaseId = null,
string $field = 'quantity',
): void {
$limit = $catalogItem->max_units_per_user;
if ($limit === null) {
return;
}
$purchasedQuantity = (int) PurchaseItem::query()
->where('source_catalog_item_id', $catalogItem->getKey())
->whereHas('purchase', function ($query) use ($userId, $excludedPurchaseId): void {
$query
->where('user_id', $userId)
->whereIn('status', [
Purchase::STATUS_CREATED,
Purchase::STATUS_PENDING_PAYMENT,
Purchase::STATUS_PAID,
])
->when(
$excludedPurchaseId !== null,
fn ($query) => $query->whereKeyNot($excludedPurchaseId),
);
})
->sum('cantidad');
if ($purchasedQuantity + $requestedQuantity > $limit) {
throw ValidationException::withMessages([
$field => __('api.purchase.max_units_per_user', ['max' => $limit]),
]);
}
$this->userPurchaseLimitService->assertCanPurchase(
$catalogItem,
$userId,
$requestedQuantity,
$excludedPurchaseId,
$field,
);
}
protected function resolveCheckoutCart(Tenant $tenant, int $userId, int $cartId): Cart

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Domains\Purchase\Services;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Models\PurchaseItem;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class UserPurchaseLimitService
{
public function assertCanPurchase(
CatalogItem $catalogItem,
int $userId,
int $requestedQuantity,
?int $excludedPurchaseId = null,
string $field = 'quantity',
): void {
DB::transaction(function () use (
$catalogItem,
$userId,
$requestedQuantity,
$excludedPurchaseId,
$field,
): void {
/** @var CatalogItem $catalogItem */
$catalogItem = CatalogItem::query()
->whereKey($catalogItem->getKey())
->lockForUpdate()
->firstOrFail();
$limit = $catalogItem->max_units_per_user;
if ($limit === null) {
return;
}
$purchasedQuantity = (int) PurchaseItem::query()
->where('source_catalog_item_id', $catalogItem->getKey())
->whereHas('purchase', function ($query) use ($userId, $excludedPurchaseId): void {
$query
->where('user_id', $userId)
->whereIn('status', [
Purchase::STATUS_CREATED,
Purchase::STATUS_PENDING_PAYMENT,
Purchase::STATUS_PAID,
])
->when(
$excludedPurchaseId !== null,
fn ($query) => $query->whereKeyNot($excludedPurchaseId),
);
})
->sum('cantidad');
if ($purchasedQuantity + $requestedQuantity > $limit) {
throw ValidationException::withMessages([
$field => __('api.purchase_limit.exceeded', ['max' => $limit]),
]);
}
});
}
}