diff --git a/internal/repository/product_category_repository.go b/internal/repository/product_category_repository.go index dab6ffe..2670652 100644 --- a/internal/repository/product_category_repository.go +++ b/internal/repository/product_category_repository.go @@ -15,11 +15,13 @@ var ( ErrProductCategoryNotFound = errors.New("product category not found") ErrProductCategoryVersionConflict = errors.New("product category version conflict") ErrDuplicateProductCategory = errors.New("duplicate product category name") + ErrDuplicatePlatformCategory = errors.New("duplicate platform category id") ) type ProductCategoryRecord struct { ID, Name, ParentID, RuleID, Extra, CertStatusMsg string Level, Priority, Status, CategoryType, CertStatusCode, Version int32 + PlatformCategoryID, PlatformParentCategoryID int64 ChannelList []int32 HasChildren, HasPropRequired, CertStatusPass bool ProductCount int64 @@ -90,11 +92,15 @@ func createProductCategoryNode(ctx context.Context, tx *sql.Tx, input *ProductCa return nil, err } var parentLevel int32 - if err = tx.QueryRowContext(ctx, `SELECT level FROM product.product_category WHERE id=$1 AND deleted_at IS NULL FOR UPDATE`, id).Scan(&parentLevel); errors.Is(err, sql.ErrNoRows) { + var parentPlatformCategoryID int64 + if err = tx.QueryRowContext(ctx, `SELECT level,platform_category_id FROM product.product_category WHERE id=$1 AND deleted_at IS NULL FOR UPDATE`, id).Scan(&parentLevel, &parentPlatformCategoryID); errors.Is(err, sql.ErrNoRows) { return nil, ErrProductCategoryNotFound } else if err != nil { return nil, err } + if input.PlatformParentCategoryID != parentPlatformCategoryID { + return nil, errors.New("platform_parent_category_id must equal the local parent's platform_category_id") + } parent = sql.NullInt64{Int64: id, Valid: true} level = parentLevel + 1 } @@ -102,7 +108,7 @@ func createProductCategoryNode(ctx context.Context, tx *sql.Tx, input *ProductCa if err != nil { return nil, err } - row := tx.QueryRowContext(ctx, `INSERT INTO product.product_category (id,parent_id,level,name,priority,rule_id,status,category_type,channel_list,has_children,has_prop_required,extra,cert_status_code,cert_status_pass,cert_status_msg) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15) RETURNING id,parent_id,level,name,priority,rule_id,status,category_type,channel_list,has_children,has_prop_required,extra,cert_status_code,cert_status_pass,cert_status_msg,version,created_at,updated_at,deleted_at,0::bigint`, id, parent, level, input.Name, input.Priority, input.RuleID, input.Status, input.CategoryType, input.ChannelList, input.HasChildren, input.HasPropRequired, input.Extra, input.CertStatusCode, input.CertStatusPass, input.CertStatusMsg) + row := tx.QueryRowContext(ctx, `INSERT INTO product.product_category (id,parent_id,level,name,priority,rule_id,status,category_type,channel_list,has_children,has_prop_required,extra,cert_status_code,cert_status_pass,cert_status_msg,platform_category_id,platform_parent_category_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17) RETURNING id,parent_id,level,name,priority,rule_id,status,category_type,channel_list,has_children,has_prop_required,extra,cert_status_code,cert_status_pass,cert_status_msg,platform_category_id,platform_parent_category_id,version,created_at,updated_at,deleted_at,0::bigint`, id, parent, level, input.Name, input.Priority, input.RuleID, input.Status, input.CategoryType, input.ChannelList, input.HasChildren, input.HasPropRequired, input.Extra, input.CertStatusCode, input.CertStatusPass, input.CertStatusMsg, input.PlatformCategoryID, input.PlatformParentCategoryID) created, err := scanProductCategory(row) if err != nil { return nil, mapProductCategoryWriteError(err) @@ -201,7 +207,7 @@ func (r *ProductRepository) GetProductCategory(ctx context.Context, id string) ( return r.loadProductCategoryTree(ctx, id) } -const productCategoryColumns = `c.id,c.parent_id,c.level,c.name,c.priority,c.rule_id,c.status,c.category_type,c.channel_list,c.has_children,c.has_prop_required,c.extra,c.cert_status_code,c.cert_status_pass,c.cert_status_msg,c.version,c.created_at,c.updated_at,c.deleted_at,(SELECT COUNT(*) FROM product.product_category_bindings b WHERE b.product_category_id=c.id AND b.deleted_at IS NULL)` +const productCategoryColumns = `c.id,c.parent_id,c.level,c.name,c.priority,c.rule_id,c.status,c.category_type,c.channel_list,c.has_children,c.has_prop_required,c.extra,c.cert_status_code,c.cert_status_pass,c.cert_status_msg,c.platform_category_id,c.platform_parent_category_id,c.version,c.created_at,c.updated_at,c.deleted_at,(SELECT COUNT(*) FROM product.product_category_bindings b WHERE b.product_category_id=c.id AND b.deleted_at IS NULL)` func (r *ProductRepository) loadProductCategoryTree(ctx context.Context, id string) (*ProductCategoryRecord, error) { parsed, err := parseID(id, "product_category_id") @@ -515,7 +521,7 @@ func scanProductCategory(row productCategoryScanner) (*ProductCategoryRecord, er var deleted sql.NullTime var channelList postgresInt32Array value := &ProductCategoryRecord{} - if err := row.Scan(&id, &parent, &value.Level, &value.Name, &value.Priority, &value.RuleID, &value.Status, &value.CategoryType, &channelList, &value.HasChildren, &value.HasPropRequired, &value.Extra, &value.CertStatusCode, &value.CertStatusPass, &value.CertStatusMsg, &value.Version, &value.CreatedAt, &value.UpdatedAt, &deleted, &value.ProductCount); err != nil { + if err := row.Scan(&id, &parent, &value.Level, &value.Name, &value.Priority, &value.RuleID, &value.Status, &value.CategoryType, &channelList, &value.HasChildren, &value.HasPropRequired, &value.Extra, &value.CertStatusCode, &value.CertStatusPass, &value.CertStatusMsg, &value.PlatformCategoryID, &value.PlatformParentCategoryID, &value.Version, &value.CreatedAt, &value.UpdatedAt, &deleted, &value.ProductCount); err != nil { return nil, err } value.ChannelList = []int32(channelList) @@ -558,5 +564,8 @@ func mapProductCategoryWriteError(err error) error { if strings.Contains(err.Error(), "uq_product_category_active_parent_name") { return ErrDuplicateProductCategory } + if strings.Contains(err.Error(), "uq_product_category_active_platform_category") { + return ErrDuplicatePlatformCategory + } return err } diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index 672b010..cd126d0 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -431,6 +431,8 @@ CREATE TABLE IF NOT EXISTS product.product_category ( cert_status_code INTEGER NOT NULL, cert_status_pass BOOLEAN NOT NULL DEFAULT FALSE, cert_status_msg VARCHAR(500) NOT NULL DEFAULT '', + platform_category_id BIGINT NOT NULL CHECK (platform_category_id > 0), + platform_parent_category_id BIGINT NOT NULL CHECK (platform_parent_category_id >= 0), version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), @@ -441,6 +443,10 @@ CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_name ON product.product_category (COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL; CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_priority ON product.product_category (parent_id, priority, id) WHERE deleted_at IS NULL; +CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_active_platform_category + ON product.product_category (platform_category_id) WHERE deleted_at IS NULL; +CREATE INDEX IF NOT EXISTS idx_product_category_active_platform_parent_category + ON product.product_category (platform_parent_category_id) WHERE deleted_at IS NULL; CREATE TABLE IF NOT EXISTS product.product_category_bindings ( id BIGINT PRIMARY KEY, diff --git a/internal/server/product_category_platform_test.go b/internal/server/product_category_platform_test.go new file mode 100644 index 0000000..b94d62f --- /dev/null +++ b/internal/server/product_category_platform_test.go @@ -0,0 +1,56 @@ +package server + +import ( + "testing" + + productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1" +) + +func TestProductCategoryFromCreateKeepsPlatformIdentifiers(t *testing.T) { + root, err := (&ProductServer{}).productCategoryFromCreate(&productv1.CreateProductCategoryInput{ + Name: "root", + RuleId: "1004", + Status: 1, + PlatformCategoryId: 100, + PlatformParentCategoryId: 0, + HasChildren: true, + Children: []*productv1.CreateProductCategoryInput{{ + Name: "child", + RuleId: "10040001", + Status: 1, + PlatformCategoryId: 101, + PlatformParentCategoryId: 100, + }}, + }, true) + if err != nil { + t.Fatal(err) + } + if root.PlatformCategoryID != 100 || root.PlatformParentCategoryID != 0 || root.Children[0].PlatformCategoryID != 101 || root.Children[0].PlatformParentCategoryID != 100 { + t.Fatalf("platform identifiers were not preserved: %+v", root) + } + response := toProtoProductCategory(root) + if response.GetPlatformCategoryId() != 100 || response.GetPlatformParentCategoryId() != 0 || response.GetChildren()[0].GetPlatformCategoryId() != 101 || response.GetChildren()[0].GetPlatformParentCategoryId() != 100 { + t.Fatalf("response platform identifiers were not preserved: %+v", response) + } +} + +func TestProductCategoryFromCreateRejectsMismatchedPlatformParent(t *testing.T) { + _, err := (&ProductServer{}).productCategoryFromCreate(&productv1.CreateProductCategoryInput{ + Name: "root", + RuleId: "1004", + Status: 1, + PlatformCategoryId: 100, + PlatformParentCategoryId: 0, + HasChildren: true, + Children: []*productv1.CreateProductCategoryInput{{ + Name: "child", + RuleId: "10040001", + Status: 1, + PlatformCategoryId: 101, + PlatformParentCategoryId: 999, + }}, + }, true) + if err == nil { + t.Fatal("expected mismatched platform parent to be rejected") + } +} diff --git a/internal/server/product_category_server.go b/internal/server/product_category_server.go index d29cced..c45812c 100644 --- a/internal/server/product_category_server.go +++ b/internal/server/product_category_server.go @@ -162,13 +162,22 @@ func (s *ProductServer) productCategoryFromCreate(input *productv1.CreateProduct if len(input.GetCertStatusMsg()) > 500 { return nil, errors.New("cert_status_msg must contain at most 500 characters") } + if input.GetPlatformCategoryId() <= 0 { + return nil, errors.New("platform_category_id must be positive") + } + if input.GetPlatformParentCategoryId() < 0 { + return nil, errors.New("platform_parent_category_id must be non-negative") + } + if root && input.ParentId == nil && input.GetPlatformParentCategoryId() != 0 { + return nil, errors.New("root platform_parent_category_id must be 0") + } if !root && input.ParentId != nil { return nil, errors.New("children must not specify parent_id") } if len(input.GetChildren()) > 0 && !input.GetHasChildren() { return nil, errors.New("has_children must be true when children are supplied") } - value := &repository.ProductCategoryRecord{Name: name, RuleID: ruleID, Priority: input.GetPriority(), Status: input.GetStatus(), CategoryType: input.GetCategoryType(), ChannelList: input.GetChannelList(), HasChildren: input.GetHasChildren(), HasPropRequired: input.GetHasPropRequired(), Extra: input.GetExtra(), CertStatusCode: input.GetCertStatusCode(), CertStatusPass: input.GetCertStatusPass(), CertStatusMsg: input.GetCertStatusMsg()} + value := &repository.ProductCategoryRecord{Name: name, RuleID: ruleID, Priority: input.GetPriority(), Status: input.GetStatus(), CategoryType: input.GetCategoryType(), ChannelList: input.GetChannelList(), HasChildren: input.GetHasChildren(), HasPropRequired: input.GetHasPropRequired(), Extra: input.GetExtra(), CertStatusCode: input.GetCertStatusCode(), CertStatusPass: input.GetCertStatusPass(), CertStatusMsg: input.GetCertStatusMsg(), PlatformCategoryID: input.GetPlatformCategoryId(), PlatformParentCategoryID: input.GetPlatformParentCategoryId()} if input.ParentId != nil { value.ParentID = strings.TrimSpace(input.GetParentId()) } @@ -177,6 +186,9 @@ func (s *ProductServer) productCategoryFromCreate(input *productv1.CreateProduct if err != nil { return nil, err } + if item.PlatformParentCategoryID != value.PlatformCategoryID { + return nil, errors.New("child platform_parent_category_id must equal its parent's platform_category_id") + } value.Children = append(value.Children, item) } return value, nil @@ -232,7 +244,7 @@ func toProtoProductCategory(value *repository.ProductCategoryRecord) *productv1. if value == nil { return nil } - out := &productv1.ProductCategory{ProductCategoryId: value.ID, Name: value.Name, Level: value.Level, Priority: value.Priority, RuleId: value.RuleID, Status: value.Status, CategoryType: value.CategoryType, ChannelList: value.ChannelList, HasChildren: value.HasChildren, HasPropRequired: value.HasPropRequired, Extra: value.Extra, CertStatusCode: value.CertStatusCode, CertStatusPass: value.CertStatusPass, CertStatusMsg: value.CertStatusMsg, Version: value.Version, CreatedAt: value.CreatedAt.Format("2006-01-02T15:04:05Z07:00"), UpdatedAt: value.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"), ProductCount: value.ProductCount} + out := &productv1.ProductCategory{ProductCategoryId: value.ID, Name: value.Name, Level: value.Level, Priority: value.Priority, RuleId: value.RuleID, Status: value.Status, CategoryType: value.CategoryType, ChannelList: value.ChannelList, HasChildren: value.HasChildren, HasPropRequired: value.HasPropRequired, Extra: value.Extra, CertStatusCode: value.CertStatusCode, CertStatusPass: value.CertStatusPass, CertStatusMsg: value.CertStatusMsg, PlatformCategoryId: value.PlatformCategoryID, PlatformParentCategoryId: value.PlatformParentCategoryID, Version: value.Version, CreatedAt: value.CreatedAt.Format("2006-01-02T15:04:05Z07:00"), UpdatedAt: value.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"), ProductCount: value.ProductCount} if value.ParentID != "" { out.ParentId = &value.ParentID } @@ -254,6 +266,8 @@ func productCategoryGRPCError(err error) error { return status.Error(codes.Aborted, "product category version conflict") case errors.Is(err, repository.ErrDuplicateProductCategory): return status.Error(codes.AlreadyExists, "duplicate product category name") + case errors.Is(err, repository.ErrDuplicatePlatformCategory): + return status.Error(codes.AlreadyExists, "duplicate platform category id") default: return status.Error(codes.InvalidArgument, err.Error()) } diff --git a/scripts/sql/006_rebuild_standard_product_category_tree.sql b/scripts/sql/006_rebuild_standard_product_category_tree.sql index 68f2976..6e202be 100644 --- a/scripts/sql/006_rebuild_standard_product_category_tree.sql +++ b/scripts/sql/006_rebuild_standard_product_category_tree.sql @@ -26,6 +26,8 @@ CREATE TABLE product.product_category ( cert_status_code INTEGER NOT NULL, cert_status_pass BOOLEAN NOT NULL DEFAULT FALSE, cert_status_msg VARCHAR(500) NOT NULL DEFAULT '', + platform_category_id BIGINT NOT NULL CHECK (platform_category_id > 0), + platform_parent_category_id BIGINT NOT NULL CHECK (platform_parent_category_id >= 0), version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), @@ -45,6 +47,12 @@ CREATE INDEX idx_product_category_active_status_parent_priority CREATE INDEX idx_product_category_rule_id ON product.product_category (rule_id) WHERE deleted_at IS NULL; +CREATE UNIQUE INDEX uq_product_category_active_platform_category + ON product.product_category (platform_category_id) + WHERE deleted_at IS NULL; +CREATE INDEX idx_product_category_active_platform_parent_category + ON product.product_category (platform_parent_category_id) + WHERE deleted_at IS NULL; CREATE INDEX idx_product_category_channel_list ON product.product_category USING GIN (channel_list);