feat(category): update list filter category

This commit is contained in:
Efril
2026-08-06 21:21:41 +07:00
parent b9ac97178f
commit a7c2d6cbb3
5 changed files with 34 additions and 0 deletions
@@ -63,6 +63,21 @@ func (r *CategoryRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error
return r.db.WithContext(ctx).Delete(&entities.Category{}, "id = ?", id).Error
}
// applyCategoryTypeFilter narrows the query by position in the category tree.
// - "parent": top level categories only (no parent of their own)
// - "child": leaf categories — sub categories plus top level categories that
// have no sub categories, i.e. everything a product can be assigned to
func applyCategoryTypeFilter(query *gorm.DB, value interface{}) *gorm.DB {
switch value {
case "parent":
return query.Where("parent_id IS NULL")
case "child":
return query.Where("NOT EXISTS (SELECT 1 FROM categories AS sub WHERE sub.parent_id = categories.id)")
default:
return query
}
}
func (r *CategoryRepositoryImpl) List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.Category, int64, error) {
var categories []*entities.Category
var total int64
@@ -77,6 +92,8 @@ func (r *CategoryRepositoryImpl) List(ctx context.Context, filters map[string]in
case "outlet_id":
// Include outlet-specific categories AND global categories (outlet_id IS NULL)
query = query.Where("outlet_id = ? OR outlet_id IS NULL", value)
case "type":
query = applyCategoryTypeFilter(query, value)
default:
query = query.Where(key+" = ?", value)
}
@@ -101,6 +118,8 @@ func (r *CategoryRepositoryImpl) Count(ctx context.Context, filters map[string]i
query = query.Where("name ILIKE ? OR description ILIKE ?", searchValue, searchValue)
case "outlet_id":
query = query.Where("outlet_id = ? OR outlet_id IS NULL", value)
case "type":
query = applyCategoryTypeFilter(query, value)
default:
query = query.Where(key+" = ?", value)
}