feat: refactor cart item handling to use mapped buyable types and improve method signatures
This commit is contained in:
@@ -112,7 +112,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([
|
||||
@@ -120,15 +120,14 @@ class Cart extends Model
|
||||
]);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($buyableType, $buyableId, $quantity): CartItem {
|
||||
return DB::transaction(function () use ($cartItemId, $quantity): CartItem {
|
||||
/** @var CartItem $item */
|
||||
$item = $this->items()
|
||||
->where('buyable_type', $buyableType)
|
||||
->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;
|
||||
|
||||
if ($delta > 0 && $buyable->stock_tecnico < $delta) {
|
||||
@@ -153,17 +152,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 {
|
||||
DB::transaction(function () use ($cartItemId): void {
|
||||
/** @var CartItem $item */
|
||||
$item = $this->items()
|
||||
->where('buyable_type', $buyableType)
|
||||
->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();
|
||||
});
|
||||
@@ -171,11 +169,11 @@ class Cart extends Model
|
||||
|
||||
protected function resolveScopedBuyable(string $buyableType, int $buyableId, bool $lockForUpdate = false)
|
||||
{
|
||||
if ($buyableType === 'variant') {
|
||||
if ($buyableType === \App\Domains\Catalog\Models\ProductVariant::class) {
|
||||
$query = \App\Domains\Catalog\Models\ProductVariant::query()
|
||||
->whereKey($buyableId)
|
||||
->whereHas('product', fn ($query) => $query->where('tenant_codigo', $this->tenant_codigo));
|
||||
} elseif ($buyableType === 'bundle') {
|
||||
} elseif ($buyableType === \App\Domains\Bundle\Models\Bundle::class) {
|
||||
$query = \App\Domains\Bundle\Models\Bundle::query()
|
||||
->whereKey($buyableId)
|
||||
->where('tenant_codigo', $this->tenant_codigo);
|
||||
|
||||
Reference in New Issue
Block a user