mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add product category handling in product repository and server logic
This commit is contained in:
parent
155ab11d9f
commit
cb81c23830
@ -36,6 +36,10 @@ var ErrInvalidLeafCategory = errors.New("category must be an existing second-lev
|
|||||||
// the current store.
|
// the current store.
|
||||||
var ErrInvalidProductGroup = errors.New("group must be an existing product group")
|
var ErrInvalidProductGroup = errors.New("group must be an existing product group")
|
||||||
|
|
||||||
|
// ErrInvalidProductCategory is returned when a requested standard product
|
||||||
|
// category is missing, disabled, deleted, or not a terminal node.
|
||||||
|
var ErrInvalidProductCategory = errors.New("product category must be an enabled terminal product category")
|
||||||
|
|
||||||
// ErrSKUNotFound is returned when a SKU does not exist.
|
// ErrSKUNotFound is returned when a SKU does not exist.
|
||||||
var ErrSKUNotFound = errors.New("sku not found")
|
var ErrSKUNotFound = errors.New("sku not found")
|
||||||
|
|
||||||
@ -97,6 +101,8 @@ type ProductRecord struct {
|
|||||||
CategoryBindingIDs []string
|
CategoryBindingIDs []string
|
||||||
GroupIDs []string
|
GroupIDs []string
|
||||||
GroupBindingIDs []string
|
GroupBindingIDs []string
|
||||||
|
ProductCategoryIDs []string
|
||||||
|
ProductCategoryBindingIDs []string
|
||||||
Version int32
|
Version int32
|
||||||
DeletedAt *time.Time
|
DeletedAt *time.Time
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
@ -232,6 +238,10 @@ type ProductUpdateInput struct {
|
|||||||
// empty slice clears all product-group bindings.
|
// empty slice clears all product-group bindings.
|
||||||
GroupIDs *[]string
|
GroupIDs *[]string
|
||||||
GroupBindingIDs []string
|
GroupBindingIDs []string
|
||||||
|
// ProductCategoryIDs is nil when standard product-category bindings must
|
||||||
|
// remain unchanged. A non-nil empty slice clears all such bindings.
|
||||||
|
ProductCategoryIDs *[]string
|
||||||
|
ProductCategoryBindingIDs []string
|
||||||
Scope StoreScope
|
Scope StoreScope
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -650,6 +660,11 @@ func (r *ProductRepository) CreateProductWithSKUs(ctx context.Context, product P
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(product.ProductCategoryIDs) > 0 {
|
||||||
|
if err := replaceProductStandardCategoryBindings(ctx, tx, createdProduct.ID, product.ProductCategoryIDs, product.ProductCategoryBindingIDs, product.Scope); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := tx.Commit(); err != nil {
|
if err := tx.Commit(); err != nil {
|
||||||
return nil, nil, fmt.Errorf("commit create product: %w", err)
|
return nil, nil, fmt.Errorf("commit create product: %w", err)
|
||||||
@ -1355,6 +1370,11 @@ FOR UPDATE
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if input.ProductCategoryIDs != nil {
|
||||||
|
if err := replaceProductStandardCategoryBindings(ctx, tx, input.Product.ID, *input.ProductCategoryIDs, input.ProductCategoryBindingIDs, input.Scope); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if input.SKUChange != nil && input.SKUChange.Mode == SKUChangeModeReplaceAll && statusFromDB(currentStatus) == productv1.ProductStatus_PRODUCT_STATUS_ON_SALE {
|
if input.SKUChange != nil && input.SKUChange.Mode == SKUChangeModeReplaceAll && statusFromDB(currentStatus) == productv1.ProductStatus_PRODUCT_STATUS_ON_SALE {
|
||||||
return nil, ErrSKUReplacementRequiresNotOnSale
|
return nil, ErrSKUReplacementRequiresNotOnSale
|
||||||
@ -1601,6 +1621,158 @@ WHERE b.product_id = $1
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// replaceProductStandardCategoryBindings replaces the active global standard
|
||||||
|
// product-category bindings for one store product. It is intentionally
|
||||||
|
// transaction-scoped so product, SKU, and binding changes commit or roll back
|
||||||
|
// as one unit.
|
||||||
|
func replaceProductStandardCategoryBindings(ctx context.Context, tx *sql.Tx, productID string, categoryIDs []string, bindingIDs []string, scope StoreScope) error {
|
||||||
|
if err := requireWriteScope(scope); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(categoryIDs) != len(bindingIDs) {
|
||||||
|
return errors.New("product category ids and binding ids must have the same length")
|
||||||
|
}
|
||||||
|
parsedProductID, err := parseID(productID, "product_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
parsedCategoryIDs := make([]int64, len(categoryIDs))
|
||||||
|
seenCategoryIDs := make(map[int64]struct{}, len(categoryIDs))
|
||||||
|
for index, categoryID := range categoryIDs {
|
||||||
|
parsedCategoryIDs[index], err = parseID(categoryID, "product_category_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, duplicate := seenCategoryIDs[parsedCategoryIDs[index]]; duplicate {
|
||||||
|
return errors.New("product_category_ids must not contain duplicates")
|
||||||
|
}
|
||||||
|
seenCategoryIDs[parsedCategoryIDs[index]] = struct{}{}
|
||||||
|
if _, err := parseID(bindingIDs[index], "product category binding id"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(parsedCategoryIDs) > 0 {
|
||||||
|
placeholders := make([]string, len(parsedCategoryIDs))
|
||||||
|
args := make([]any, len(parsedCategoryIDs))
|
||||||
|
for index, categoryID := range parsedCategoryIDs {
|
||||||
|
placeholders[index] = fmt.Sprintf("$%d", index+1)
|
||||||
|
args[index] = categoryID
|
||||||
|
}
|
||||||
|
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
|
||||||
|
SELECT c.id
|
||||||
|
FROM product.product_category AS c
|
||||||
|
WHERE c.id IN (%s)
|
||||||
|
AND c.deleted_at IS NULL
|
||||||
|
AND c.status = 1
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM product.product_category AS child
|
||||||
|
WHERE child.parent_id = c.id AND child.deleted_at IS NULL
|
||||||
|
)
|
||||||
|
ORDER BY c.id
|
||||||
|
FOR UPDATE
|
||||||
|
`, strings.Join(placeholders, ", ")), args...)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("validate standard product categories: %w", err)
|
||||||
|
}
|
||||||
|
found := make(map[int64]struct{}, len(parsedCategoryIDs))
|
||||||
|
for rows.Next() {
|
||||||
|
var categoryID int64
|
||||||
|
if err := rows.Scan(&categoryID); err != nil {
|
||||||
|
rows.Close()
|
||||||
|
return fmt.Errorf("scan standard product category: %w", err)
|
||||||
|
}
|
||||||
|
found[categoryID] = struct{}{}
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
rows.Close()
|
||||||
|
return fmt.Errorf("iterate standard product categories: %w", err)
|
||||||
|
}
|
||||||
|
if err := rows.Close(); err != nil {
|
||||||
|
return fmt.Errorf("close standard product categories: %w", err)
|
||||||
|
}
|
||||||
|
if len(found) != len(parsedCategoryIDs) {
|
||||||
|
return ErrInvalidProductCategory
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteArgs := []any{parsedProductID, scope.TenantID, scope.StoreID}
|
||||||
|
deleteSQL := `
|
||||||
|
UPDATE product.product_category_bindings
|
||||||
|
SET deleted_at = NOW(), updated_at = NOW()
|
||||||
|
WHERE product_id = $1
|
||||||
|
AND tenant_id = $2
|
||||||
|
AND store_id = $3
|
||||||
|
AND deleted_at IS NULL`
|
||||||
|
if len(parsedCategoryIDs) > 0 {
|
||||||
|
placeholders := make([]string, len(parsedCategoryIDs))
|
||||||
|
for index, categoryID := range parsedCategoryIDs {
|
||||||
|
placeholders[index] = fmt.Sprintf("$%d", index+4)
|
||||||
|
deleteArgs = append(deleteArgs, categoryID)
|
||||||
|
}
|
||||||
|
deleteSQL += "\n AND product_category_id NOT IN (" + strings.Join(placeholders, ", ") + ")"
|
||||||
|
}
|
||||||
|
if _, err := tx.ExecContext(ctx, deleteSQL, deleteArgs...); err != nil {
|
||||||
|
return fmt.Errorf("soft delete standard product category bindings: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for index, categoryID := range parsedCategoryIDs {
|
||||||
|
var active bool
|
||||||
|
if err := tx.QueryRowContext(ctx, `
|
||||||
|
SELECT EXISTS(
|
||||||
|
SELECT 1
|
||||||
|
FROM product.product_category_bindings
|
||||||
|
WHERE product_id = $1
|
||||||
|
AND tenant_id = $2
|
||||||
|
AND store_id = $3
|
||||||
|
AND product_category_id = $4
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
)
|
||||||
|
`, parsedProductID, scope.TenantID, scope.StoreID, categoryID).Scan(&active); err != nil {
|
||||||
|
return fmt.Errorf("check active standard product category binding: %w", err)
|
||||||
|
}
|
||||||
|
if active {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result, err := tx.ExecContext(ctx, `
|
||||||
|
UPDATE product.product_category_bindings
|
||||||
|
SET deleted_at = NULL, updated_at = NOW()
|
||||||
|
WHERE id = (
|
||||||
|
SELECT id
|
||||||
|
FROM product.product_category_bindings
|
||||||
|
WHERE product_id = $1
|
||||||
|
AND tenant_id = $2
|
||||||
|
AND store_id = $3
|
||||||
|
AND product_category_id = $4
|
||||||
|
AND deleted_at IS NOT NULL
|
||||||
|
ORDER BY updated_at DESC, id DESC
|
||||||
|
LIMIT 1
|
||||||
|
)
|
||||||
|
`, parsedProductID, scope.TenantID, scope.StoreID, categoryID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("restore standard product category binding: %w", err)
|
||||||
|
}
|
||||||
|
affected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("standard product category binding rows affected: %w", err)
|
||||||
|
}
|
||||||
|
if affected == 0 {
|
||||||
|
bindingID, err := parseID(bindingIDs[index], "product category binding id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := tx.ExecContext(ctx, `
|
||||||
|
INSERT INTO product.product_category_bindings(id, tenant_id, store_id, product_id, product_category_id)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
`, bindingID, scope.TenantID, scope.StoreID, parsedProductID, categoryID); err != nil {
|
||||||
|
return fmt.Errorf("insert standard product category binding: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateProductStatus changes the product lifecycle state using optimistic locking.
|
// UpdateProductStatus changes the product lifecycle state using optimistic locking.
|
||||||
func (r *ProductRepository) UpdateProductStatus(ctx context.Context, productID string, next productv1.ProductStatus, expectedVersion int32, scope StoreScope) (*ProductStatusUpdate, error) {
|
func (r *ProductRepository) UpdateProductStatus(ctx context.Context, productID string, next productv1.ProductStatus, expectedVersion int32, scope StoreScope) (*ProductStatusUpdate, error) {
|
||||||
if err := requireWriteScope(scope); err != nil {
|
if err := requireWriteScope(scope); err != nil {
|
||||||
@ -2484,6 +2656,11 @@ func (r *ProductRepository) GetProduct(ctx context.Context, productID string, sc
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
product.GroupIDs = groupIDs
|
product.GroupIDs = groupIDs
|
||||||
|
productCategoryIDs, err := r.listProductCategoryIDs(ctx, productID, scope)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
product.ProductCategoryIDs = productCategoryIDs
|
||||||
|
|
||||||
return product, skus[productID], nil
|
return product, skus[productID], nil
|
||||||
}
|
}
|
||||||
@ -2654,6 +2831,43 @@ ORDER BY c.sort_num, c.id
|
|||||||
return groupIDs, nil
|
return groupIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *ProductRepository) listProductCategoryIDs(ctx context.Context, productID string, scope StoreScope) ([]string, error) {
|
||||||
|
if err := requireWriteScope(scope); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
parsedProductID, err := parseID(productID, "product_id")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT c.id
|
||||||
|
FROM product.product_category_bindings AS b
|
||||||
|
JOIN product.product_category AS c ON c.id = b.product_category_id
|
||||||
|
WHERE b.product_id = $1
|
||||||
|
AND b.tenant_id = $2
|
||||||
|
AND b.store_id = $3
|
||||||
|
AND b.deleted_at IS NULL
|
||||||
|
AND c.deleted_at IS NULL
|
||||||
|
ORDER BY c.level, c.priority, c.id
|
||||||
|
`, parsedProductID, scope.TenantID, scope.StoreID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list product category ids: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
categoryIDs := make([]string, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
var categoryID int64
|
||||||
|
if err := rows.Scan(&categoryID); err != nil {
|
||||||
|
return nil, fmt.Errorf("scan product category id: %w", err)
|
||||||
|
}
|
||||||
|
categoryIDs = append(categoryIDs, formatID(categoryID))
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("iterate product category ids: %w", err)
|
||||||
|
}
|
||||||
|
return categoryIDs, nil
|
||||||
|
}
|
||||||
|
|
||||||
// BatchGetCartItemProducts loads current catalog facts for cart lines in request order.
|
// BatchGetCartItemProducts loads current catalog facts for cart lines in request order.
|
||||||
// It includes archived SKUs, but treats soft-deleted SPUs and SKUs as unavailable.
|
// It includes archived SKUs, but treats soft-deleted SPUs and SKUs as unavailable.
|
||||||
func (r *ProductRepository) BatchGetCartItemProducts(ctx context.Context, lookups []CartItemProductLookup, scope StoreScope) ([]CartItemProductRecord, error) {
|
func (r *ProductRepository) BatchGetCartItemProducts(ctx context.Context, lookups []CartItemProductLookup, scope StoreScope) ([]CartItemProductRecord, error) {
|
||||||
|
|||||||
46
internal/server/product_category_ids_test.go
Normal file
46
internal/server/product_category_ids_test.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||||
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProductCategoryIDsFlowThroughCreateUpdateAndResponse(t *testing.T) {
|
||||||
|
request := &productv1.CreateProductRequest{
|
||||||
|
SpuNo: "SPU-category-ids",
|
||||||
|
Title: "Product",
|
||||||
|
Status: productv1.ProductStatus_PRODUCT_STATUS_INVENTORY,
|
||||||
|
ProductCategoryIds: []string{"9001", "9002"},
|
||||||
|
}
|
||||||
|
product, _, err := productFromCreateRequest(request)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("productFromCreateRequest() error = %v", err)
|
||||||
|
}
|
||||||
|
if want := []string{"9001", "9002"}; !reflect.DeepEqual(product.ProductCategoryIDs, want) {
|
||||||
|
t.Fatalf("create product category ids = %v, want %v", product.ProductCategoryIDs, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
merged, err := mergeProductUpdate(&product, &productv1.ProductUpdate{
|
||||||
|
ProductCategoryIds: []string{"9002", "9003"},
|
||||||
|
}, []string{"product_category_ids"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mergeProductUpdate() error = %v", err)
|
||||||
|
}
|
||||||
|
if want := []string{"9002", "9003"}; !reflect.DeepEqual(merged.ProductCategoryIDs, want) {
|
||||||
|
t.Fatalf("updated product category ids = %v, want %v", merged.ProductCategoryIDs, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := toProtoProduct(&repository.ProductRecord{ProductCategoryIDs: merged.ProductCategoryIDs}, nil)
|
||||||
|
if want := []string{"9002", "9003"}; !reflect.DeepEqual(response.GetProductCategoryIds(), want) {
|
||||||
|
t.Fatalf("response product category ids = %v, want %v", response.GetProductCategoryIds(), want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeProductCategoryIDsRejectsDuplicate(t *testing.T) {
|
||||||
|
if _, err := normalizeProductCategoryIDs([]string{"9001", "9001"}); err == nil {
|
||||||
|
t.Fatal("normalizeProductCategoryIDs() accepted duplicate ids")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -129,6 +129,9 @@ func (s *ProductServer) CreateProduct(ctx context.Context, req *productv1.Create
|
|||||||
for range product.GroupIDs {
|
for range product.GroupIDs {
|
||||||
product.GroupBindingIDs = append(product.GroupBindingIDs, s.idgen.NextString())
|
product.GroupBindingIDs = append(product.GroupBindingIDs, s.idgen.NextString())
|
||||||
}
|
}
|
||||||
|
for range product.ProductCategoryIDs {
|
||||||
|
product.ProductCategoryBindingIDs = append(product.ProductCategoryBindingIDs, s.idgen.NextString())
|
||||||
|
}
|
||||||
|
|
||||||
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.create", trace.WithAttributes(
|
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.create", trace.WithAttributes(
|
||||||
attribute.String("product.spu_no", product.SPUNo),
|
attribute.String("product.spu_no", product.SPUNo),
|
||||||
@ -150,6 +153,9 @@ func (s *ProductServer) CreateProduct(ctx context.Context, req *productv1.Create
|
|||||||
if errors.Is(err, repository.ErrInvalidProductGroup) {
|
if errors.Is(err, repository.ErrInvalidProductGroup) {
|
||||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
}
|
}
|
||||||
|
if errors.Is(err, repository.ErrInvalidProductCategory) {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
}
|
||||||
return nil, status.Errorf(codes.Internal, "create product: %v", err)
|
return nil, status.Errorf(codes.Internal, "create product: %v", err)
|
||||||
}
|
}
|
||||||
observability.IncProductsCreated()
|
observability.IncProductsCreated()
|
||||||
@ -403,6 +409,8 @@ func (s *ProductServer) UpdateProduct(ctx context.Context, req *productv1.Update
|
|||||||
var categoryBindingIDs []string
|
var categoryBindingIDs []string
|
||||||
var groupIDs *[]string
|
var groupIDs *[]string
|
||||||
var groupBindingIDs []string
|
var groupBindingIDs []string
|
||||||
|
var productCategoryIDs *[]string
|
||||||
|
var productCategoryBindingIDs []string
|
||||||
if containsUpdatePath(req.GetUpdateMask().GetPaths(), "leaf_catalog_category_ids") {
|
if containsUpdatePath(req.GetUpdateMask().GetPaths(), "leaf_catalog_category_ids") {
|
||||||
leafCategoryIDs = &merged.LeafCategoryIDs
|
leafCategoryIDs = &merged.LeafCategoryIDs
|
||||||
if s.idgen == nil {
|
if s.idgen == nil {
|
||||||
@ -421,6 +429,15 @@ func (s *ProductServer) UpdateProduct(ctx context.Context, req *productv1.Update
|
|||||||
groupBindingIDs = append(groupBindingIDs, s.idgen.NextString())
|
groupBindingIDs = append(groupBindingIDs, s.idgen.NextString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if containsUpdatePath(req.GetUpdateMask().GetPaths(), "product_category_ids") {
|
||||||
|
productCategoryIDs = &merged.ProductCategoryIDs
|
||||||
|
if s.idgen == nil {
|
||||||
|
return nil, status.Error(codes.Internal, "id generator is not configured")
|
||||||
|
}
|
||||||
|
for range merged.ProductCategoryIDs {
|
||||||
|
productCategoryBindingIDs = append(productCategoryBindingIDs, s.idgen.NextString())
|
||||||
|
}
|
||||||
|
}
|
||||||
result, err := s.repo.UpdateProductWithSKUChange(dbCtx, repository.ProductUpdateInput{
|
result, err := s.repo.UpdateProductWithSKUChange(dbCtx, repository.ProductUpdateInput{
|
||||||
Product: merged,
|
Product: merged,
|
||||||
ExpectedVersion: req.GetExpectedVersion(),
|
ExpectedVersion: req.GetExpectedVersion(),
|
||||||
@ -429,6 +446,8 @@ func (s *ProductServer) UpdateProduct(ctx context.Context, req *productv1.Update
|
|||||||
CategoryBindingIDs: categoryBindingIDs,
|
CategoryBindingIDs: categoryBindingIDs,
|
||||||
GroupIDs: groupIDs,
|
GroupIDs: groupIDs,
|
||||||
GroupBindingIDs: groupBindingIDs,
|
GroupBindingIDs: groupBindingIDs,
|
||||||
|
ProductCategoryIDs: productCategoryIDs,
|
||||||
|
ProductCategoryBindingIDs: productCategoryBindingIDs,
|
||||||
Scope: scope,
|
Scope: scope,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -447,6 +466,8 @@ func (s *ProductServer) UpdateProduct(ctx context.Context, req *productv1.Update
|
|||||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
case errors.Is(err, repository.ErrInvalidProductGroup):
|
case errors.Is(err, repository.ErrInvalidProductGroup):
|
||||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
case errors.Is(err, repository.ErrInvalidProductCategory):
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
default:
|
default:
|
||||||
s.observer.Logger(ctx).Error("update product failed", zap.String("product_id", req.GetProductId()), zap.Error(err))
|
s.observer.Logger(ctx).Error("update product failed", zap.String("product_id", req.GetProductId()), zap.Error(err))
|
||||||
return nil, status.Errorf(codes.Internal, "update product: %v", err)
|
return nil, status.Errorf(codes.Internal, "update product: %v", err)
|
||||||
@ -1997,6 +2018,11 @@ func productFromCreateRequest(req *productv1.CreateProductRequest) (repository.P
|
|||||||
return repository.ProductRecord{}, nil, err
|
return repository.ProductRecord{}, nil, err
|
||||||
}
|
}
|
||||||
record.GroupIDs = groupIDs
|
record.GroupIDs = groupIDs
|
||||||
|
productCategoryIDs, err := normalizeProductCategoryIDs(req.GetProductCategoryIds())
|
||||||
|
if err != nil {
|
||||||
|
return repository.ProductRecord{}, nil, err
|
||||||
|
}
|
||||||
|
record.ProductCategoryIDs = productCategoryIDs
|
||||||
if record.FreightFixedAmount < 0 {
|
if record.FreightFixedAmount < 0 {
|
||||||
return repository.ProductRecord{}, nil, errors.New("freight_fixed_amount must be greater than or equal to 0")
|
return repository.ProductRecord{}, nil, errors.New("freight_fixed_amount must be greater than or equal to 0")
|
||||||
}
|
}
|
||||||
@ -2094,6 +2120,12 @@ func mergeProductUpdate(current *repository.ProductRecord, update *productv1.Pro
|
|||||||
return repository.ProductRecord{}, err
|
return repository.ProductRecord{}, err
|
||||||
}
|
}
|
||||||
merged.GroupIDs = groupIDs
|
merged.GroupIDs = groupIDs
|
||||||
|
case "product_category_ids":
|
||||||
|
productCategoryIDs, err := normalizeProductCategoryIDs(update.GetProductCategoryIds())
|
||||||
|
if err != nil {
|
||||||
|
return repository.ProductRecord{}, err
|
||||||
|
}
|
||||||
|
merged.ProductCategoryIDs = productCategoryIDs
|
||||||
default:
|
default:
|
||||||
return repository.ProductRecord{}, fmt.Errorf("unsupported update field %q", path)
|
return repository.ProductRecord{}, fmt.Errorf("unsupported update field %q", path)
|
||||||
}
|
}
|
||||||
@ -2450,6 +2482,29 @@ func normalizeGroupIDs(values []string) ([]string, error) {
|
|||||||
return groupIDs, nil
|
return groupIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalizeProductCategoryIDs(values []string) ([]string, error) {
|
||||||
|
if len(values) > 100 {
|
||||||
|
return nil, errors.New("product_category_ids must contain at most 100 items")
|
||||||
|
}
|
||||||
|
categoryIDs := make([]string, 0, len(values))
|
||||||
|
seen := make(map[string]struct{}, len(values))
|
||||||
|
for _, value := range values {
|
||||||
|
categoryID := strings.TrimSpace(value)
|
||||||
|
if categoryID == "" {
|
||||||
|
return nil, errors.New("product_category_ids must not contain an empty id")
|
||||||
|
}
|
||||||
|
if _, err := normalizeCategoryID(categoryID, "product_category_ids"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, duplicate := seen[categoryID]; duplicate {
|
||||||
|
return nil, errors.New("product_category_ids must not contain duplicates")
|
||||||
|
}
|
||||||
|
seen[categoryID] = struct{}{}
|
||||||
|
categoryIDs = append(categoryIDs, categoryID)
|
||||||
|
}
|
||||||
|
return categoryIDs, nil
|
||||||
|
}
|
||||||
|
|
||||||
func normalizeGroupProductIDs(values []string) ([]string, error) {
|
func normalizeGroupProductIDs(values []string) ([]string, error) {
|
||||||
productIDs := normalizeStringList(values)
|
productIDs := normalizeStringList(values)
|
||||||
if len(productIDs) == 0 {
|
if len(productIDs) == 0 {
|
||||||
@ -2579,6 +2634,7 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
|
|||||||
DeletedAt: timePtrToMillisString(product.DeletedAt),
|
DeletedAt: timePtrToMillisString(product.DeletedAt),
|
||||||
LeafCatalogCategoryIds: append([]string(nil), product.LeafCategoryIDs...),
|
LeafCatalogCategoryIds: append([]string(nil), product.LeafCategoryIDs...),
|
||||||
GroupIds: append([]string(nil), product.GroupIDs...),
|
GroupIds: append([]string(nil), product.GroupIDs...),
|
||||||
|
ProductCategoryIds: append([]string(nil), product.ProductCategoryIDs...),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user