diff --git a/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go b/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go index f4c7d045..e05f3be1 100644 --- a/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go +++ b/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository.go @@ -7,7 +7,6 @@ import ( "gitlab.com/mbugroup/lti-api.git/internal/common/repository" entity "gitlab.com/mbugroup/lti-api.git/internal/entities" - "gitlab.com/mbugroup/lti-api.git/internal/utils" "gorm.io/gorm" ) @@ -166,42 +165,16 @@ func (r *ProductWarehouseRepositoryImpl) ApplyFlagsFilter(db *gorm.DB, flags []s return db } - fallbackCategoryCodes := utils.LegacyProductCategoryCodesForFlags(flags) - - db = db. - Joins("JOIN products p_flag ON p_flag.id = product_warehouses.product_id"). - Joins("LEFT JOIN product_categories pc_flag ON pc_flag.id = p_flag.product_category_id") - - actualFlagFilter := ` - EXISTS ( - SELECT 1 - FROM flags f_flag - WHERE f_flag.flagable_id = p_flag.id - AND f_flag.flagable_type = ? - AND f_flag.name IN ? - ) - ` - - if len(fallbackCategoryCodes) == 0 { - return db.Where(actualFlagFilter, entity.FlagableTypeProduct, flags).Distinct() - } - return db. - Where( - `(`+actualFlagFilter+`) OR ( - NOT EXISTS ( - SELECT 1 - FROM flags f_any - WHERE f_any.flagable_id = p_flag.id - AND f_any.flagable_type = ? - ) - AND pc_flag.code IN ? - )`, - entity.FlagableTypeProduct, - flags, - entity.FlagableTypeProduct, - fallbackCategoryCodes, - ). + Where(` + EXISTS ( + SELECT 1 + FROM flags f_flag + WHERE f_flag.flagable_id = product_warehouses.product_id + AND f_flag.flagable_type = ? + AND f_flag.name IN ? + ) + `, entity.FlagableTypeProduct, flags). Distinct() } diff --git a/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository_test.go b/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository_test.go index 3f810e53..a5399858 100644 --- a/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository_test.go +++ b/internal/modules/inventory/product-warehouses/repositories/product_warehouse.repository_test.go @@ -117,7 +117,7 @@ func insertProductWarehouseTestFixtures(t *testing.T, db *gorm.DB) { } } -func TestApplyFlagsFilterIncludesLegacyCategoryFallback(t *testing.T) { +func TestApplyFlagsFilterOnlyIncludesFlaggedProducts(t *testing.T) { db := setupProductWarehouseFlagFilterTestDB(t) repo := NewProductWarehouseRepository(db) ctx := context.Background() @@ -131,12 +131,14 @@ func TestApplyFlagsFilterIncludesLegacyCategoryFallback(t *testing.T) { t.Fatalf("unexpected error: %v", err) } - if len(ids) != 2 || ids[0] != 1 || ids[1] != 2 { - t.Fatalf("expected flagged and legacy RAW rows to match, got %v", ids) + // Only PW 1 (product 10, flagged PAKAN) should match. + // PW 2 (product 20, no flags, RAW category) must not appear — legacy fallback removed. + if len(ids) != 1 || ids[0] != 1 { + t.Fatalf("expected only flagged row to match, got %v", ids) } } -func TestApplyFlagsFilterDoesNotFallbackWhenProductAlreadyHasDifferentFlags(t *testing.T) { +func TestApplyFlagsFilterExcludesWrongFlaggedProducts(t *testing.T) { db := setupProductWarehouseFlagFilterTestDB(t) repo := NewProductWarehouseRepository(db) ctx := context.Background() @@ -150,8 +152,9 @@ func TestApplyFlagsFilterDoesNotFallbackWhenProductAlreadyHasDifferentFlags(t *t t.Fatalf("unexpected error: %v", err) } + // PW 3 belongs to an OVK-flagged product — must not appear when filtering for PAKAN. if len(ids) != 0 { - t.Fatalf("expected OVK-flagged product not to match PAKAN fallback, got %v", ids) + t.Fatalf("expected OVK-flagged product not to match PAKAN filter, got %v", ids) } }