feat: enhance product group handling by validating second-level groups and updating queries

This commit is contained in:
BXY 2026-10-28 10:08:35 +08:00
parent 0b4726efce
commit 9e1dbdf583
3 changed files with 21 additions and 12 deletions

View File

@ -356,7 +356,7 @@ func (r *ProductRepository) ReplaceProductsGroup(ctx context.Context, productIDs
err = tx.QueryRowContext(ctx, `
SELECT id
FROM product.catalog_category
WHERE id = $1 AND node_type = 2 AND store_id = $2
WHERE id = $1 AND node_type = 2 AND level = 2 AND store_id = $2
`, parsedGroupID, scope.StoreID).Scan(&parsedGroupID)
if errors.Is(err, sql.ErrNoRows) {
return 0, fmt.Errorf("%w: %s", ErrCategoryNodeNotFound, groupID)

View File

@ -1395,10 +1395,10 @@ func pointerStringSlice(value *[]string) []string {
return *value
}
// AddProductsToGroup binds every product to a product group in one transaction.
// AddProductsToGroup binds every product to a second-level product group in one transaction.
// Existing bindings are retained and duplicate bindings are ignored.
func (r *ProductRepository) AddProductsToGroup(ctx context.Context, groupID string, bindingIDs, productIDs []string, scope StoreScope) error {
return r.addProductsToCategoryNode(ctx, groupID, bindingIDs, productIDs, 2, 0, "group", scope)
return r.addProductsToCategoryNode(ctx, groupID, bindingIDs, productIDs, 2, 2, "group", scope)
}
// AddProductsToCategory binds every product to a terminal catalog category at
@ -1552,7 +1552,7 @@ func (r *ProductRepository) RemoveProductsFromGroup(ctx context.Context, groupID
err = tx.QueryRowContext(ctx, `
SELECT id
FROM product.catalog_category
WHERE id = $1 AND node_type = 2 AND store_id = $2
WHERE id = $1 AND node_type = 2 AND level = 2 AND store_id = $2
`, parsedGroupID, scope.StoreID).Scan(&parsedGroupID)
if errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("%w: %s", ErrCategoryNodeNotFound, groupID)
@ -1976,6 +1976,7 @@ func replaceProductGroupBindings(ctx context.Context, tx *sql.Tx, productID stri
SELECT id
FROM product.catalog_category
WHERE node_type = 2
AND level = 2
AND store_id = $%d
AND id IN (%s)
FOR KEY SHARE
@ -3424,7 +3425,7 @@ ORDER BY pi.product_id ASC, pi.price ASC, pi.id ASC
return result, nil
}
// ValidateProductGroup verifies that a group belongs to the current store.
// ValidateProductGroup verifies that a second-level group belongs to the current store.
func (r *ProductRepository) ValidateProductGroup(ctx context.Context, groupID string, scope StoreScope) error {
if err := requireWriteScope(scope); err != nil {
return err
@ -3437,7 +3438,7 @@ func (r *ProductRepository) ValidateProductGroup(ctx context.Context, groupID st
err = r.db.QueryRowContext(ctx, `
SELECT id
FROM product.catalog_category
WHERE id = $1 AND node_type = 2 AND store_id = $2
WHERE id = $1 AND node_type = 2 AND level = 2 AND store_id = $2
`, parsedGroupID, scope.StoreID).Scan(&found)
if errors.Is(err, sql.ErrNoRows) {
return ErrInvalidProductGroup
@ -4004,14 +4005,14 @@ SELECT COUNT(*)
FROM product.products p
JOIN product.product_catalog_bindings b ON b.product_id = p.id
JOIN product.catalog_category c ON c.id = b.category_id
WHERE ($6 = 2 OR b.category_id IN (
WHERE (($6 = 2 AND b.category_id = $1) OR ($6 = 1 AND b.category_id IN (
WITH RECURSIVE descendants(id) AS (
SELECT id FROM product.catalog_category WHERE id = $1 AND store_id = $5 AND node_type = 1
UNION ALL
SELECT child.id FROM product.catalog_category child JOIN descendants parent ON child.parent_id = parent.id
WHERE child.store_id = $5 AND child.node_type = 1
) SELECT id FROM descendants
))
)))
AND p.deleted_at IS NULL
AND p.store_id = $5
AND c.store_id = $5
@ -4037,14 +4038,14 @@ SELECT p.id
FROM product.products p
JOIN product.product_catalog_bindings b ON b.product_id = p.id
JOIN product.catalog_category c ON c.id = b.category_id
WHERE ($6 = 2 OR b.category_id IN (
WHERE (($6 = 2 AND b.category_id = $1) OR ($6 = 1 AND b.category_id IN (
WITH RECURSIVE descendants(id) AS (
SELECT id FROM product.catalog_category WHERE id = $1 AND store_id = $5 AND node_type = 1
UNION ALL
SELECT child.id FROM product.catalog_category child JOIN descendants parent ON child.parent_id = parent.id
WHERE child.store_id = $5 AND child.node_type = 1
) SELECT id FROM descendants
))
)))
AND p.deleted_at IS NULL
AND p.store_id = $5
AND c.store_id = $5
@ -4107,14 +4108,14 @@ LEFT JOIN LATERAL (
AND pi.is_archived = FALSE
AND ($2 = FALSE OR pi.status = 1)
) sku_summary ON TRUE
WHERE ($8 = 2 OR b.category_id IN (
WHERE (($8 = 2 AND b.category_id = $1) OR ($8 = 1 AND b.category_id IN (
WITH RECURSIVE descendants(id) AS (
SELECT id FROM product.catalog_category WHERE id = $1 AND store_id = $7 AND node_type = 1
UNION ALL
SELECT child.id FROM product.catalog_category child JOIN descendants parent ON child.parent_id = parent.id
WHERE child.store_id = $7 AND child.node_type = 1
) SELECT id FROM descendants
))
)))
AND p.deleted_at IS NULL
AND p.store_id = $7
AND c.store_id = $7

View File

@ -1022,6 +1022,14 @@ func (s *ProductServer) ListCategoryProducts(ctx context.Context, req *productv1
if targetNodeType != 1 && targetNodeType != 2 {
return nil, status.Error(codes.InvalidArgument, "target_node_type must be category (1) or group (2)")
}
if targetNodeType == 2 {
if err := s.repo.ValidateProductGroup(ctx, req.GetCategoryId(), scope); err != nil {
if errors.Is(err, repository.ErrInvalidProductGroup) {
return nil, status.Error(codes.NotFound, "second-level product group not found")
}
return nil, status.Errorf(codes.Internal, "validate product group: %v", err)
}
}
page, pageSize := normalizedPage(req.GetPage(), req.GetPageSize())