feat(cart): update CartItemResource to use visibleVariants method with included variant ID; enhance variant filtering logic
This commit is contained in:
@@ -38,7 +38,7 @@ class CartItemResource extends JsonResource
|
||||
'product' => $selectedItem === null ? null : [
|
||||
'nombre' => $selectedItem->getName(),
|
||||
'imagen' => $imageUrl,
|
||||
'variants' => $this->catalogItem->variants
|
||||
'variants' => $this->catalogItem->visibleVariants($this->variant_id)
|
||||
->map(fn (Variant $variant): array => [
|
||||
'id' => $variant->id,
|
||||
'precio' => $this->formatMoney($variant->getPrice()),
|
||||
|
||||
@@ -172,10 +172,11 @@ class CatalogItem extends Model
|
||||
}
|
||||
|
||||
/** @return Collection<int, Variant> */
|
||||
public function visibleVariants(): Collection
|
||||
public function visibleVariants(?int $includedVariantId = null): Collection
|
||||
{
|
||||
return $this->variants
|
||||
->filter(fn (Variant $variant): bool => $this->inventory_policy === InventoryPolicy::Unlimited
|
||||
->filter(fn (Variant $variant): bool => ($includedVariantId !== null && $variant->id === $includedVariantId)
|
||||
|| $this->inventory_policy === InventoryPolicy::Unlimited
|
||||
|| ($variant->inventory?->availableStock() ?? 0) > 0)
|
||||
->values();
|
||||
}
|
||||
|
||||
@@ -260,11 +260,16 @@ class CartControllerTest extends TestCase
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
[$item, $firstVariant] = $this->createVariantItem($tenant, 10, '15.00');
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 8]);
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 2]);
|
||||
$secondVariant = $item->variants()->create([
|
||||
'inventory_id' => $secondInventory->id,
|
||||
'precio' => '20.00',
|
||||
]);
|
||||
$unavailableInventory = Inventory::query()->create(['real_stock' => 0]);
|
||||
$unavailableVariant = $item->variants()->create([
|
||||
'inventory_id' => $unavailableInventory->id,
|
||||
'precio' => '25.00',
|
||||
]);
|
||||
$createResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $firstVariant->id,
|
||||
@@ -285,7 +290,11 @@ class CartControllerTest extends TestCase
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items.0.variant_id', $secondVariant->id)
|
||||
->assertJsonPath('data.items.0.precio_unitario', '20.00')
|
||||
->assertJsonCount(2, 'data.items.0.product.variants');
|
||||
->assertJsonCount(2, 'data.items.0.product.variants')
|
||||
->assertJsonPath('data.items.0.product.variants.0.id', $firstVariant->id)
|
||||
->assertJsonPath('data.items.0.product.variants.1.id', $secondVariant->id)
|
||||
->assertJsonPath('data.items.0.product.variants.1.stock_tecnico', 0)
|
||||
->assertJsonMissing(['id' => $unavailableVariant->id, 'stock_tecnico' => 0]);
|
||||
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $firstVariant->inventory_id,
|
||||
|
||||
@@ -290,11 +290,17 @@ class CatalogModelsTest extends TestCase
|
||||
{
|
||||
$unavailable = (new Variant)->setRelation('inventory', $this->trackedInventory(3, 3));
|
||||
$available = (new Variant)->setRelation('inventory', $this->trackedInventory(5, 2));
|
||||
$unavailable->id = 10;
|
||||
$available->id = 20;
|
||||
$item = new CatalogItem;
|
||||
$item->inventory_policy = InventoryPolicy::Tracked;
|
||||
$item->setRelation('variants', new EloquentCollection([$unavailable, $available]));
|
||||
|
||||
$this->assertSame([$available], $item->visibleVariants()->all());
|
||||
$this->assertSame(
|
||||
[$unavailable, $available],
|
||||
$item->visibleVariants($unavailable->id)->all(),
|
||||
);
|
||||
|
||||
$item->inventory_policy = InventoryPolicy::Unlimited;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user