mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
fix: hide legace unflagged products to be consistent with the validation
This commit is contained in:
+9
-36
@@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -166,42 +165,16 @@ func (r *ProductWarehouseRepositoryImpl) ApplyFlagsFilter(db *gorm.DB, flags []s
|
|||||||
return db
|
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.
|
return db.
|
||||||
Where(
|
Where(`
|
||||||
`(`+actualFlagFilter+`) OR (
|
EXISTS (
|
||||||
NOT EXISTS (
|
SELECT 1
|
||||||
SELECT 1
|
FROM flags f_flag
|
||||||
FROM flags f_any
|
WHERE f_flag.flagable_id = product_warehouses.product_id
|
||||||
WHERE f_any.flagable_id = p_flag.id
|
AND f_flag.flagable_type = ?
|
||||||
AND f_any.flagable_type = ?
|
AND f_flag.name IN ?
|
||||||
)
|
)
|
||||||
AND pc_flag.code IN ?
|
`, entity.FlagableTypeProduct, flags).
|
||||||
)`,
|
|
||||||
entity.FlagableTypeProduct,
|
|
||||||
flags,
|
|
||||||
entity.FlagableTypeProduct,
|
|
||||||
fallbackCategoryCodes,
|
|
||||||
).
|
|
||||||
Distinct()
|
Distinct()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-5
@@ -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)
|
db := setupProductWarehouseFlagFilterTestDB(t)
|
||||||
repo := NewProductWarehouseRepository(db)
|
repo := NewProductWarehouseRepository(db)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -131,12 +131,14 @@ func TestApplyFlagsFilterIncludesLegacyCategoryFallback(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ids) != 2 || ids[0] != 1 || ids[1] != 2 {
|
// Only PW 1 (product 10, flagged PAKAN) should match.
|
||||||
t.Fatalf("expected flagged and legacy RAW rows to match, got %v", ids)
|
// 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)
|
db := setupProductWarehouseFlagFilterTestDB(t)
|
||||||
repo := NewProductWarehouseRepository(db)
|
repo := NewProductWarehouseRepository(db)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -150,8 +152,9 @@ func TestApplyFlagsFilterDoesNotFallbackWhenProductAlreadyHasDifferentFlags(t *t
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user