mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
fix(product): tighten SKU attributes parsing for product query
This commit is contained in:
parent
d0f0aea15f
commit
2fa8872603
@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||
)
|
||||
|
||||
@ -19,6 +21,9 @@ var ErrProductNotFound = errors.New("product not found")
|
||||
// ErrSKUNotFound is returned when a SKU does not exist.
|
||||
var ErrSKUNotFound = errors.New("sku not found")
|
||||
|
||||
// ErrDuplicateSPUNo is returned when a non-deleted product already uses the SPU number.
|
||||
var ErrDuplicateSPUNo = errors.New("spu_no already exists")
|
||||
|
||||
// ProductRecord is the persistence model for product.products (SPU).
|
||||
type ProductRecord struct {
|
||||
ID string
|
||||
@ -678,11 +683,22 @@ RETURNING id,
|
||||
|
||||
createdProduct, err := scanProduct(row)
|
||||
if err != nil {
|
||||
if isPostgresConstraintError(err, "23505", "uq_products_spu_no") {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDuplicateSPUNo, product.SPUNo)
|
||||
}
|
||||
return nil, fmt.Errorf("insert product: %w", err)
|
||||
}
|
||||
return createdProduct, nil
|
||||
}
|
||||
|
||||
func isPostgresConstraintError(err error, code, constraint string) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
if !errors.As(err, &pgErr) {
|
||||
return false
|
||||
}
|
||||
return pgErr.Code == code && pgErr.ConstraintName == constraint
|
||||
}
|
||||
|
||||
func insertSKU(ctx context.Context, tx *sql.Tx, productID string, sku SKURecord) (*SKURecord, error) {
|
||||
attrs, err := normalizeAttributesJSON(sku.AttributesJSON)
|
||||
if err != nil {
|
||||
@ -1198,15 +1214,12 @@ func productCrossBorderJSON(product ProductRecord) ([]byte, error) {
|
||||
}
|
||||
|
||||
func applySKUAttributes(rec *SKURecord) error {
|
||||
if rec == nil || strings.TrimSpace(rec.AttributesJSON) == "" {
|
||||
if rec == nil {
|
||||
return nil
|
||||
}
|
||||
if strings.TrimSpace(rec.AttributesJSON) == "{}" {
|
||||
return nil
|
||||
}
|
||||
var attrs []map[string]string
|
||||
if err := json.Unmarshal([]byte(rec.AttributesJSON), &attrs); err != nil {
|
||||
return fmt.Errorf("unmarshal sku attributes: %w", err)
|
||||
attrs, err := parseSKUAttributes(rec.AttributesJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, item := range attrs {
|
||||
if value := item["k1"]; value != "" {
|
||||
@ -1231,6 +1244,42 @@ func applySKUAttributes(rec *SKURecord) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseSKUAttributes(raw string) ([]map[string]string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || raw == "{}" || raw == "null" {
|
||||
return nil, nil
|
||||
}
|
||||
var attrs []map[string]string
|
||||
if err := json.Unmarshal([]byte(raw), &attrs); err == nil {
|
||||
return attrs, nil
|
||||
}
|
||||
|
||||
var attr map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &attr); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal sku attributes: %w", err)
|
||||
}
|
||||
if isEmptySKUAttributeObject(attr) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unmarshal sku attributes: expected array, got object")
|
||||
}
|
||||
|
||||
func isEmptySKUAttributeObject(attr map[string]any) bool {
|
||||
if len(attr) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, value := range attr {
|
||||
if value == nil {
|
||||
continue
|
||||
}
|
||||
if s, ok := value.(string); ok && strings.TrimSpace(s) == "" {
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func parseStringArrayJSON(raw string) ([]string, error) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return []string{}, nil
|
||||
|
||||
@ -88,6 +88,9 @@ func (s *ProductServer) CreateProduct(ctx context.Context, req *productv1.Create
|
||||
dbSpan.RecordError(err)
|
||||
dbSpan.SetStatus(otelcodes.Error, err.Error())
|
||||
s.observer.Logger(ctx).Error("create product failed", zap.String("spu_no", product.SPUNo), zap.Error(err))
|
||||
if errors.Is(err, repository.ErrDuplicateSPUNo) {
|
||||
return nil, status.Errorf(codes.AlreadyExists, "spu_no already exists: %s", product.SPUNo)
|
||||
}
|
||||
return nil, status.Errorf(codes.Internal, "create product: %v", err)
|
||||
}
|
||||
observability.IncProductsCreated()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user