feat: refactor cart item handling to use mapped buyable types and improve method signatures

This commit is contained in:
2026-07-14 16:43:00 -03:00
parent 480ee70393
commit c05206cc51
7 changed files with 50 additions and 31 deletions

View File

@@ -116,7 +116,7 @@ class Cart extends Model
});
}
public function updateItem(string $buyableType, int $buyableId, int $quantity): CartItem
public function updateItem(int $cartItemId, int $quantity): CartItem
{
if ($quantity <= 0) {
throw ValidationException::withMessages([
@@ -124,17 +124,14 @@ class Cart extends Model
]);
}
return DB::transaction(function () use ($buyableType, $buyableId, $quantity): CartItem {
$canonicalType = $this->resolveBuyableClass($buyableType);
return DB::transaction(function () use ($cartItemId, $quantity): CartItem {
/** @var CartItem $item */
$item = $this->items()
->where('buyable_type', $canonicalType)
->where('buyable_id', $buyableId)
->where('id', $cartItemId)
->lockForUpdate()
->firstOrFail();
$buyable = $this->resolveScopedBuyable($buyableType, $buyableId, true);
$buyable = $this->resolveScopedBuyable($item->buyable_type, $item->buyable_id, true);
$delta = $quantity - $item->cantidad;
$availableQuantity = $buyable->availableQuantity();
@@ -160,19 +157,16 @@ class Cart extends Model
});
}
public function removeItem(string $buyableType, int $buyableId): void
public function removeItem(int $cartItemId): void
{
DB::transaction(function () use ($buyableType, $buyableId): void {
$canonicalType = $this->resolveBuyableClass($buyableType);
DB::transaction(function () use ($cartItemId): void {
/** @var CartItem $item */
$item = $this->items()
->where('buyable_type', $canonicalType)
->where('buyable_id', $buyableId)
->where('id', $cartItemId)
->lockForUpdate()
->firstOrFail();
$buyable = $this->resolveScopedBuyable($buyableType, $buyableId, true);
$buyable = $this->resolveScopedBuyable($item->buyable_type, $item->buyable_id, true);
$buyable->decrementReservedStock($item->cantidad);
$item->delete();
});