feat(category): include products from all descendant categories in paginated response

This commit is contained in:
2026-09-24 16:28:15 -03:00
parent 41c0902b83
commit cf3f0ceb1b
2 changed files with 63 additions and 6 deletions

View File

@@ -72,6 +72,29 @@ class CategoryDetailTest extends TestCase
->assertJsonCount(0, 'data');
}
public function test_it_includes_products_from_all_descendant_categories(): void
{
$tenant = $this->createTenant('category-descendants');
$parent = $this->createCategory($tenant, 'Indumentaria');
$child = $this->createCategory($tenant, 'Remeras', $parent);
$grandchild = $this->createCategory($tenant, 'Manga corta', $child);
$sibling = $this->createCategory($tenant, 'Pantalones');
$parentItem = $this->createCatalogItem($tenant, $parent, 'Producto padre');
$childItem = $this->createCatalogItem($tenant, $child, 'Producto hijo');
$grandchildItem = $this->createCatalogItem($tenant, $grandchild, 'Producto nieto');
$this->createCatalogItem($tenant, $sibling, 'Producto ajeno');
$this->getJson("/api/tenants/{$tenant->codigo}/categories/{$parent->id}")
->assertOk()
->assertJsonPath('meta.total', 3)
->assertJsonCount(3, 'data')
->assertJsonPath('data.0.id', $childItem->id)
->assertJsonPath('data.1.id', $grandchildItem->id)
->assertJsonPath('data.2.id', $parentItem->id)
->assertJsonMissing(['nombre' => 'Producto ajeno']);
}
public function test_it_rejects_categories_from_another_tenant(): void
{
$tenant = $this->createTenant('category-owner');
@@ -107,10 +130,14 @@ class CategoryDetailTest extends TestCase
]);
}
private function createCategory(Tenant $tenant, string $name): Category
{
private function createCategory(
Tenant $tenant,
string $name,
?Category $parent = null,
): Category {
return Category::query()->create([
'tenant_code' => $tenant->codigo,
'categoria_id' => $parent?->id,
'nombre' => $name,
]);
}