Merge branch 'feat/toggle-negative-usgae' into 'development'

fix: hide legace unflagged products to be consistent with the validation

See merge request mbugroup/lti-api!493
This commit is contained in:
Adnan Zahir
2026-04-29 12:18:56 +07:00
2 changed files with 17 additions and 41 deletions
@@ -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()
}
@@ -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)
}
}