From 24e000b423958644d4c532d8d8c2d707f6db01fc Mon Sep 17 00:00:00 2001 From: ncoronel Date: Mon, 24 Aug 2026 10:49:14 -0300 Subject: [PATCH] feat(catalog): add is_enabled attribute to categories and filter items accordingly --- app/Domains/Catalog/Models/Category.php | 6 +++ .../Catalog/Services/FeaturedGroupService.php | 9 ++++ ...000_add_is_enabled_to_categorias_table.php | 22 +++++++++ .../Feature/Catalog/CatalogControllerTest.php | 47 +++++++++++++++++++ tests/Feature/Catalog/CatalogSchemaTest.php | 16 +++++++ 5 files changed, 100 insertions(+) create mode 100644 database/migrations/2026_08_24_000000_add_is_enabled_to_categorias_table.php diff --git a/app/Domains/Catalog/Models/Category.php b/app/Domains/Catalog/Models/Category.php index ee683ba..c0ed114 100644 --- a/app/Domains/Catalog/Models/Category.php +++ b/app/Domains/Catalog/Models/Category.php @@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; 'tenant_code', 'categoria_id', 'nombre', + 'is_enabled', ])] class Category extends Model { @@ -22,6 +23,10 @@ class Category extends Model protected $table = 'categorias'; + protected $attributes = [ + 'is_enabled' => true, + ]; + /** * @return array */ @@ -29,6 +34,7 @@ class Category extends Model { return [ 'categoria_id' => 'integer', + 'is_enabled' => 'boolean', ]; } diff --git a/app/Domains/Catalog/Services/FeaturedGroupService.php b/app/Domains/Catalog/Services/FeaturedGroupService.php index f01b679..0302a1f 100644 --- a/app/Domains/Catalog/Services/FeaturedGroupService.php +++ b/app/Domains/Catalog/Services/FeaturedGroupService.php @@ -49,6 +49,15 @@ class FeaturedGroupService { $query = CatalogItem::query() ->where('catalog_items.tenant_code', $featuredGroup->tenant_code) + ->where(function (Builder $query): void { + $query + ->whereDoesntHave('category') + ->orWhereHas( + 'category', + fn (Builder $categoryQuery): Builder => $categoryQuery + ->where('is_enabled', true), + ); + }) ->with([ 'inventory', 'attachments', diff --git a/database/migrations/2026_08_24_000000_add_is_enabled_to_categorias_table.php b/database/migrations/2026_08_24_000000_add_is_enabled_to_categorias_table.php new file mode 100644 index 0000000..e61c5b0 --- /dev/null +++ b/database/migrations/2026_08_24_000000_add_is_enabled_to_categorias_table.php @@ -0,0 +1,22 @@ +boolean('is_enabled')->default(true)->after('nombre'); + }); + } + + public function down(): void + { + Schema::table('categorias', function (Blueprint $table): void { + $table->dropColumn('is_enabled'); + }); + } +}; diff --git a/tests/Feature/Catalog/CatalogControllerTest.php b/tests/Feature/Catalog/CatalogControllerTest.php index 63f1051..cdc98d2 100644 --- a/tests/Feature/Catalog/CatalogControllerTest.php +++ b/tests/Feature/Catalog/CatalogControllerTest.php @@ -367,6 +367,53 @@ class CatalogControllerTest extends TestCase ->assertJsonPath('1.items.meta.total', 2); } + public function test_index_excludes_items_from_disabled_categories(): void + { + $tenant = $this->createTenant('catalog-disabled-category'); + $group = $this->createGroup( + $tenant, + ProductLayout::Row, + 'All products', + groupLayout: GroupLayout::Simple, + ); + $enabledCategory = Category::query()->create([ + 'tenant_code' => $tenant->codigo, + 'nombre' => 'Enabled', + ]); + $disabledCategory = Category::query()->create([ + 'tenant_code' => $tenant->codigo, + 'nombre' => 'Disabled', + 'is_enabled' => false, + ]); + + $enabledItem = $this->createItem($tenant, 'Enabled item'); + $enabledItem->category()->associate($enabledCategory)->save(); + $disabledItem = $this->createItem($tenant, 'Disabled item'); + $disabledItem->category()->associate($disabledCategory)->save(); + $uncategorizedItem = $this->createItem($tenant, 'Uncategorized item'); + + foreach ([$enabledItem, $disabledItem, $uncategorizedItem] as $order => $item) { + $group->featuredItems()->create([ + 'catalog_item_id' => $item->id, + 'order' => $order, + ]); + } + + $this->getJson("/api/tenants/{$tenant->codigo}/catalog") + ->assertOk() + ->assertJsonCount(2, '0.items') + ->assertJsonPath('0.items.0.nombre', 'Enabled item') + ->assertJsonPath('0.items.1.nombre', 'Uncategorized item') + ->assertJsonMissing(['nombre' => 'Disabled item']); + + $this->getJson( + "/api/tenants/{$tenant->codigo}/catalog/featured-groups/{$group->id}/items" + ) + ->assertOk() + ->assertJsonCount(2) + ->assertJsonMissing(['nombre' => 'Disabled item']); + } + private function createGroup( Tenant $tenant, ProductLayout $layout, diff --git a/tests/Feature/Catalog/CatalogSchemaTest.php b/tests/Feature/Catalog/CatalogSchemaTest.php index 705bfc7..43147b4 100644 --- a/tests/Feature/Catalog/CatalogSchemaTest.php +++ b/tests/Feature/Catalog/CatalogSchemaTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\Catalog; use App\Domains\Attachable\Enums\AttachmentType; use App\Domains\Attachable\Models\Attachment; use App\Domains\Catalog\Models\CatalogItem; +use App\Domains\Catalog\Models\Category; use App\Domains\Catalog\Models\Inventory; use App\Domains\Tenant\Models\Tenant; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -15,6 +16,21 @@ class CatalogSchemaTest extends TestCase { use RefreshDatabase; + public function test_categories_are_enabled_by_default(): void + { + $this->assertTrue(Schema::hasColumn('categorias', 'is_enabled')); + + $category = Category::query()->create([ + 'nombre' => 'Enabled category', + ]); + + $this->assertTrue($category->is_enabled); + $this->assertDatabaseHas('categorias', [ + 'id' => $category->id, + 'is_enabled' => true, + ]); + } + public function test_legacy_product_tables_are_replaced_by_catalog_tables(): void { $this->assertFalse(Schema::hasTable('productos'));