feat(catalog): omit hidden products before pagination

This commit is contained in:
2026-08-24 15:15:55 -03:00
parent 2323994f30
commit e27f7bb166
7 changed files with 112 additions and 46 deletions

View File

@@ -41,7 +41,7 @@ class CatalogControllerTest extends TestCase
$directItem = $this->createItem($tenant, 'Direct', $directInventory);
$row->featuredItems()->create(['catalog_item_id' => $directItem->id]);
$variantItem = $this->createItem($tenant, 'Variants');
$variantItem = $this->createItem($tenant, 'Variants', withoutInventory: true);
$firstInventory = Inventory::query()->create([
'real_stock' => 5,
'reserved_stock' => 1,
@@ -56,7 +56,7 @@ class CatalogControllerTest extends TestCase
]);
$variantItem->variants()->create(['inventory_id' => $firstInventory->id]);
$variantItem->variants()->create(['inventory_id' => $secondInventory->id]);
$unavailableVariant = $variantItem->variants()->create([
$variantItem->variants()->create([
'inventory_id' => $unavailableInventory->id,
]);
$cart->featuredItems()->create(['catalog_item_id' => $variantItem->id]);
@@ -72,24 +72,12 @@ class CatalogControllerTest extends TestCase
->assertJsonPath('0.items.0.nombre', 'Variants')
->assertJsonPath('0.items.0.descripcion', 'Variants description')
->assertJsonPath('0.items.0.precio', '100.00')
->assertJsonPath('0.items.0.availability.subject', 'product')
->assertJsonPath('0.items.0.availability.maximum_quantity', 7)
->assertJsonCount(3, '0.items.0.variants')
->assertJsonCount(2, '0.items.0.variants')
->assertJsonPath('0.items.0.variants.0.availability.maximum_quantity', 4)
->assertJsonPath('0.items.0.variants.0.availability.subject', 'variant')
->assertJsonPath('0.items.0.variants.1.availability.maximum_quantity', 3)
->assertJsonPath('0.items.0.variants.2.id', $unavailableVariant->id)
->assertJsonPath('0.items.0.variants.2.availability.maximum_quantity', 0)
->assertJsonPath(
'0.items.0.variants.2.availability.restrictions.0.message',
'Este producto no tiene stock disponible.',
)
->assertJsonPath(
'0.items.0.variants.2.availability.capabilities.select_variant',
false,
)
->assertJsonPath(
'0.items.0.variants.2.availability.capabilities.add_to_cart',
false,
)
->assertJsonPath('1.title', 'Row')
->assertJsonPath('1.items.data.0.availability.maximum_quantity', 8)
->assertJsonMissingPath('1.items.data.0.stock_tecnico')
@@ -106,7 +94,7 @@ class CatalogControllerTest extends TestCase
groupLayout: GroupLayout::Simple,
);
$user = User::factory()->create();
$item = $this->createItem($tenant, 'Limited variants');
$item = $this->createItem($tenant, 'Limited variants', withoutInventory: true);
$item->update(['max_units_per_user' => 3]);
$firstVariant = $item->variants()->create([
'inventory_id' => Inventory::query()->create(['real_stock' => 10])->id,
@@ -145,7 +133,7 @@ class CatalogControllerTest extends TestCase
->assertJsonMissingPath('0.items.0.variants.1.stock_tecnico');
}
public function test_it_includes_out_of_stock_items_with_an_unavailable_message(): void
public function test_it_hides_out_of_stock_items_before_building_the_group_response(): void
{
$tenant = $this->createTenant('catalog-available-variants');
$group = $this->createGroup(
@@ -155,7 +143,7 @@ class CatalogControllerTest extends TestCase
groupLayout: GroupLayout::SimpleVertical,
);
$unavailableItem = $this->createItem($tenant, 'Unavailable');
$unavailableItem = $this->createItem($tenant, 'Unavailable', withoutInventory: true);
$unavailableInventory = Inventory::query()->create([
'real_stock' => 4,
'reserved_stock' => 4,
@@ -163,7 +151,7 @@ class CatalogControllerTest extends TestCase
$unavailableItem->variants()->create(['inventory_id' => $unavailableInventory->id]);
$group->featuredItems()->create(['catalog_item_id' => $unavailableItem->id]);
$availableItem = $this->createItem($tenant, 'Available');
$availableItem = $this->createItem($tenant, 'Available', withoutInventory: true);
$availableInventory = Inventory::query()->create([
'real_stock' => 4,
'reserved_stock' => 3,
@@ -173,15 +161,9 @@ class CatalogControllerTest extends TestCase
$this->getJson("/api/tenants/{$tenant->codigo}/catalog")
->assertOk()
->assertJsonCount(2, '0.items')
->assertJsonPath('0.items.0.nombre', 'Unavailable')
->assertJsonPath('0.items.0.availability.maximum_quantity', 0)
->assertJsonPath(
'0.items.0.availability.restrictions.0.message',
'Este producto no tiene stock disponible.',
)
->assertJsonPath('0.items.1.nombre', 'Available')
->assertJsonCount(0, '0.items.1.availability.restrictions');
->assertJsonCount(1, '0.items')
->assertJsonPath('0.items.0.nombre', 'Available')
->assertJsonCount(0, '0.items.0.availability.restrictions');
}
public function test_column_with_image_uses_item_image_then_variant_image_then_null(): void
@@ -198,8 +180,8 @@ class CatalogControllerTest extends TestCase
'order' => 0,
]);
$variantItem = $this->createItem($tenant, 'Variant image');
$variantInventory = Inventory::query()->create();
$variantItem = $this->createItem($tenant, 'Variant image', withoutInventory: true);
$variantInventory = Inventory::query()->create(['real_stock' => 1]);
$variant = $variantItem->variants()->create(['inventory_id' => $variantInventory->id]);
$variantImage = $this->createAttachment('variant');
$variant->attachments()->attach($variantImage, ['orden' => 0]);
@@ -446,7 +428,12 @@ class CatalogControllerTest extends TestCase
Tenant $tenant,
string $name,
?Inventory $inventory = null,
bool $withoutInventory = false,
): CatalogItem {
$inventory ??= $withoutInventory
? null
: Inventory::query()->create(['real_stock' => 10]);
return CatalogItem::query()->create([
'tenant_code' => $tenant->codigo,
'inventory_id' => $inventory?->id,

View File

@@ -48,7 +48,18 @@ class CatalogItemDetailControllerTest extends TestCase
$this->assertStringContainsString($itemImage->path, $response->json('data.images.0'));
}
public function test_it_lists_unavailable_variants_and_selects_the_first_available_one(): void
public function test_it_does_not_return_an_out_of_stock_product_detail(): void
{
$tenant = $this->createTenant('detail-hidden');
$inventory = Inventory::query()->create(['real_stock' => 0]);
$item = $this->createItem($tenant, 'Hidden item', $inventory);
$this->getJson(
"/api/tenants/{$tenant->codigo}/catalog-items/{$item->id}",
)->assertNotFound();
}
public function test_it_hides_unavailable_variants_and_selects_the_first_available_one(): void
{
Storage::fake('s3');
$tenant = $this->createTenant('detail-default');
@@ -69,14 +80,9 @@ class CatalogItemDetailControllerTest extends TestCase
$response
->assertOk()
->assertJsonCount(2, 'data.variants')
->assertJsonPath('data.variants.0.id', $firstVariant->id)
->assertJsonPath('data.variants.0.availability.maximum_quantity', 0)
->assertJsonPath(
'data.variants.0.availability.restrictions.0.message',
'Este producto no tiene stock disponible.',
)
->assertJsonPath('data.variants.1.id', $secondVariant->id)
->assertJsonCount(1, 'data.variants')
->assertJsonPath('data.variants.0.id', $secondVariant->id)
->assertJsonPath('data.variants.0.availability.subject', 'variant')
->assertJsonPath('data.selected_variant.id', $secondVariant->id)
->assertJsonPath('data.selected_variant.availability.maximum_quantity', 6)
->assertJsonMissingPath('data.selected_variant.stock_tecnico')

View File

@@ -62,6 +62,8 @@ class CatalogSearchTest extends TestCase
$this->createCatalogItem($tenant, "Running {$number}");
}
$exactMatch = $this->createCatalogItem($tenant, 'Running');
$outOfStockMatch = $this->createCatalogItem($tenant, 'Running unavailable');
$outOfStockMatch->inventory->update(['real_stock' => 0]);
$this->createCatalogItem($tenant, 'Unrelated');
$this->createCatalogItem($otherTenant, 'Running foreign');
@@ -76,6 +78,7 @@ class CatalogSearchTest extends TestCase
->assertJsonPath('meta.total', 6)
->assertJsonCount(4, 'data')
->assertJsonPath('data.0.id', $exactMatch->id)
->assertJsonMissing(['nombre' => 'Running unavailable'])
->assertJsonMissing(['nombre' => 'Running foreign'])
->assertJsonMissing(['nombre' => 'Unrelated']);
}