package repository import ( "context" "apskel-pos-be/internal/entities" "github.com/google/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) type CategoryRepositoryImpl struct { db *gorm.DB } func NewCategoryRepositoryImpl(db *gorm.DB) *CategoryRepositoryImpl { return &CategoryRepositoryImpl{ db: db, } } func (r *CategoryRepositoryImpl) Create(ctx context.Context, category *entities.Category) error { return r.db.WithContext(ctx).Create(category).Error } func (r *CategoryRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.Category, error) { var category entities.Category err := r.db.WithContext(ctx).Preload("Parent").First(&category, "id = ?", id).Error if err != nil { return nil, err } return &category, nil } func (r *CategoryRepositoryImpl) GetWithProducts(ctx context.Context, id uuid.UUID) (*entities.Category, error) { var category entities.Category err := r.db.WithContext(ctx).Preload("Products").First(&category, "id = ?", id).Error if err != nil { return nil, err } return &category, nil } func (r *CategoryRepositoryImpl) GetByOrganization(ctx context.Context, organizationID uuid.UUID) ([]*entities.Category, error) { var categories []*entities.Category err := r.db.WithContext(ctx).Where("organization_id = ?", organizationID).Find(&categories).Error return categories, err } // ListParentCategories returns the top-level categories of an organization. These are // the buckets the parent category reports roll up to via COALESCE(parent_id, id), so // the list is deliberately every top-level category, not only those with children — // otherwise a team could show up in a report but not be selectable on a purchase. // Categories with no outlet of their own are shared, so they are always included. func (r *CategoryRepositoryImpl) ListParentCategories(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID) ([]*entities.Category, error) { var categories []*entities.Category query := r.db.WithContext(ctx). Where("organization_id = ?", organizationID). Where("parent_id IS NULL") if outletID != nil { query = query.Where("outlet_id = ? OR outlet_id IS NULL", *outletID) } err := query.Order("\"order\" ASC, name ASC").Find(&categories).Error return categories, err } func (r *CategoryRepositoryImpl) GetByBusinessType(ctx context.Context, businessType string) ([]*entities.Category, error) { var categories []*entities.Category err := r.db.WithContext(ctx).Where("business_type = ?", businessType).Find(&categories).Error return categories, err } func (r *CategoryRepositoryImpl) Update(ctx context.Context, category *entities.Category) error { // Omit associations so a preloaded Parent is not upserted back over parent_id return r.db.WithContext(ctx).Omit(clause.Associations).Save(category).Error } 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 query := r.db.WithContext(ctx).Model(&entities.Category{}) for key, value := range filters { switch key { case "search": searchValue := "%" + value.(string) + "%" query = query.Where("name ILIKE ? OR description ILIKE ?", searchValue, searchValue) 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) } } if err := query.Count(&total).Error; err != nil { return nil, 0, err } err := query.Preload("Parent").Order("\"order\" ASC").Limit(limit).Offset(offset).Find(&categories).Error return categories, total, err } func (r *CategoryRepositoryImpl) Count(ctx context.Context, filters map[string]interface{}) (int64, error) { var count int64 query := r.db.WithContext(ctx).Model(&entities.Category{}) for key, value := range filters { switch key { case "search": searchValue := "%" + value.(string) + "%" 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) } } err := query.Count(&count).Error return count, err } func (r *CategoryRepositoryImpl) GetByName(ctx context.Context, organizationID uuid.UUID, name string) (*entities.Category, error) { var category entities.Category err := r.db.WithContext(ctx).Where("organization_id = ? AND name = ?", organizationID, name).First(&category).Error if err != nil { return nil, err } return &category, nil } func (r *CategoryRepositoryImpl) ExistsByName(ctx context.Context, organizationID uuid.UUID, name string, excludeID *uuid.UUID) (bool, error) { query := r.db.WithContext(ctx).Model(&entities.Category{}).Where("organization_id = ? AND name = ?", organizationID, name) if excludeID != nil { query = query.Where("id != ?", *excludeID) } var count int64 err := query.Count(&count).Error return count > 0, err }