feat(catalog): add is_enabled attribute to categories and filter items accordingly

This commit is contained in:
2026-08-24 10:49:14 -03:00
parent 36eb41a18a
commit 24e000b423
5 changed files with 100 additions and 0 deletions

View File

@@ -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<string, string>
*/
@@ -29,6 +34,7 @@ class Category extends Model
{
return [
'categoria_id' => 'integer',
'is_enabled' => 'boolean',
];
}

View File

@@ -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',

View File

@@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('categorias', function (Blueprint $table): void {
$table->boolean('is_enabled')->default(true)->after('nombre');
});
}
public function down(): void
{
Schema::table('categorias', function (Blueprint $table): void {
$table->dropColumn('is_enabled');
});
}
};

View File

@@ -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,

View File

@@ -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'));