mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: implement CopyProductToStore functionality and add corresponding tests
This commit is contained in:
parent
c258915f5b
commit
a67703d55c
@ -501,6 +501,138 @@ func (s *ProductServer) TransferProductStore(ctx context.Context, req *productv1
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CopyProductToStore creates a new target-store product instead of moving the
|
||||||
|
// source record. Source product, SKU, inventory, and material records remain
|
||||||
|
// untouched. Material IDs must already have been copied by ids-svc-storage.
|
||||||
|
func (s *ProductServer) CopyProductToStore(ctx context.Context, req *productv1.CopyProductToStoreRequest) (*productv1.CopyProductToStoreResponse, error) {
|
||||||
|
scope, err := requireStoreScope(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := validatePositiveID(req.GetSourceProductId(), "source_product_id"); err != nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
}
|
||||||
|
targetStoreID, err := parsePositiveID(strings.TrimSpace(req.GetTargetStoreId()), "target_store_id")
|
||||||
|
if err != nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
}
|
||||||
|
if targetStoreID == scope.StoreID {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, "target store is the current store")
|
||||||
|
}
|
||||||
|
if s.idgen == nil {
|
||||||
|
return nil, status.Error(codes.Internal, "id generator is not configured")
|
||||||
|
}
|
||||||
|
|
||||||
|
source, sourceSKUs, err := s.repo.GetProduct(ctx, req.GetSourceProductId(), scope)
|
||||||
|
if errors.Is(err, repository.ErrProductNotFound) {
|
||||||
|
return nil, status.Error(codes.NotFound, "source product not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, status.Errorf(codes.Internal, "load source product: %v", err)
|
||||||
|
}
|
||||||
|
mappings, err := productCopyMaterialMap(source, sourceSKUs, req.GetMaterialIdMappings())
|
||||||
|
if err != nil {
|
||||||
|
return nil, status.Error(codes.FailedPrecondition, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
targetScope := repository.StoreScope{TenantID: scope.TenantID, StoreID: targetStoreID}
|
||||||
|
target := *source
|
||||||
|
target.ID = s.idgen.NextString()
|
||||||
|
target.Scope = targetScope
|
||||||
|
target.Status = productv1.ProductStatus_PRODUCT_STATUS_INVENTORY
|
||||||
|
target.BrandID = ""
|
||||||
|
target.SupplierID = ""
|
||||||
|
target.ImageIDs = remapProductCopyImageIDs(source.ImageIDs, mappings)
|
||||||
|
target.LeafCategoryIDs = nil
|
||||||
|
target.CategoryBindingIDs = nil
|
||||||
|
target.GroupIDs = nil
|
||||||
|
target.GroupBindingIDs = nil
|
||||||
|
target.Version = 0
|
||||||
|
target.DeletedAt = nil
|
||||||
|
target.CreatedAt = time.Time{}
|
||||||
|
target.UpdatedAt = time.Time{}
|
||||||
|
|
||||||
|
targetSKUs := make([]repository.SKURecord, 0, len(sourceSKUs))
|
||||||
|
for _, sourceSKU := range sourceSKUs {
|
||||||
|
targetSKU := sourceSKU
|
||||||
|
targetSKU.ID = s.idgen.NextString()
|
||||||
|
targetSKU.ProductID = target.ID
|
||||||
|
targetSKU.ImageIDs = remapProductCopyImageIDs(sourceSKU.ImageIDs, mappings)
|
||||||
|
targetSKU.StockQuantity = 0
|
||||||
|
targetSKU.LockedQuantity = 0
|
||||||
|
targetSKU.SoldQuantity = 0
|
||||||
|
targetSKU.Version = 0
|
||||||
|
targetSKU.DeletedAt = nil
|
||||||
|
targetSKU.IsArchived = false
|
||||||
|
targetSKU.ArchivedAt = nil
|
||||||
|
targetSKU.CreatedAt = time.Time{}
|
||||||
|
targetSKU.UpdatedAt = time.Time{}
|
||||||
|
targetSKU.Inventory = &repository.InventoryRecord{ID: s.idgen.NextString()}
|
||||||
|
targetSKUs = append(targetSKUs, targetSKU)
|
||||||
|
}
|
||||||
|
|
||||||
|
created, _, err := s.repo.CreateProductWithSKUs(ctx, target, targetSKUs)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, repository.ErrDuplicateSPUNo) || errors.Is(err, repository.ErrDuplicateSKUNo) {
|
||||||
|
return nil, status.Error(codes.AlreadyExists, err.Error())
|
||||||
|
}
|
||||||
|
return nil, status.Errorf(codes.Internal, "copy product to target store: %v", err)
|
||||||
|
}
|
||||||
|
return &productv1.CopyProductToStoreResponse{
|
||||||
|
SourceProductId: req.GetSourceProductId(),
|
||||||
|
TargetProductId: created.ID,
|
||||||
|
TenantId: strconv.FormatInt(scope.TenantID, 10),
|
||||||
|
TargetStoreId: strconv.FormatInt(targetStoreID, 10),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func productCopyMaterialMap(product *repository.ProductRecord, skus []repository.SKURecord, mappings []*productv1.MaterialIDMapping) (map[string]string, error) {
|
||||||
|
required := make(map[string]struct{})
|
||||||
|
if product != nil {
|
||||||
|
for _, imageID := range product.ImageIDs {
|
||||||
|
required[imageID] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, sku := range skus {
|
||||||
|
for _, imageID := range sku.ImageIDs {
|
||||||
|
required[imageID] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resolved := make(map[string]string, len(mappings))
|
||||||
|
for _, mapping := range mappings {
|
||||||
|
if mapping == nil || strings.TrimSpace(mapping.GetSourceMaterialId()) == "" || strings.TrimSpace(mapping.GetTargetMaterialId()) == "" {
|
||||||
|
return nil, fmt.Errorf("material_id_mappings must contain source and target IDs")
|
||||||
|
}
|
||||||
|
if _, err := parsePositiveID(mapping.GetSourceMaterialId(), "source_material_id"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err := parsePositiveID(mapping.GetTargetMaterialId(), "target_material_id"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, exists := resolved[mapping.GetSourceMaterialId()]; exists {
|
||||||
|
return nil, fmt.Errorf("material_id_mappings contains duplicate source_material_id")
|
||||||
|
}
|
||||||
|
resolved[mapping.GetSourceMaterialId()] = mapping.GetTargetMaterialId()
|
||||||
|
}
|
||||||
|
if len(required) != len(resolved) {
|
||||||
|
return nil, fmt.Errorf("material_id_mappings must exactly cover source product images")
|
||||||
|
}
|
||||||
|
for sourceID := range required {
|
||||||
|
if _, ok := resolved[sourceID]; !ok {
|
||||||
|
return nil, fmt.Errorf("material_id_mappings is missing source material %s", sourceID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resolved, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func remapProductCopyImageIDs(imageIDs []string, mappings map[string]string) []string {
|
||||||
|
result := make([]string, 0, len(imageIDs))
|
||||||
|
for _, imageID := range imageIDs {
|
||||||
|
result = append(result, mappings[imageID])
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// ListProducts returns a paginated list of all non-deleted products.
|
// ListProducts returns a paginated list of all non-deleted products.
|
||||||
func (s *ProductServer) ListProducts(ctx context.Context, req *productv1.ListProductsRequest) (*productv1.ListProductsResponse, error) {
|
func (s *ProductServer) ListProducts(ctx context.Context, req *productv1.ListProductsRequest) (*productv1.ListProductsResponse, error) {
|
||||||
scope, err := requireStoreScope(req)
|
scope, err := requireStoreScope(req)
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
commonv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/common/v1"
|
commonv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/common/v1"
|
||||||
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||||
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRequireStoreScopeMissing(t *testing.T) {
|
func TestRequireStoreScopeMissing(t *testing.T) {
|
||||||
@ -35,3 +36,26 @@ func TestGetProductBySkuRequiresID(t *testing.T) {
|
|||||||
t.Fatalf("err = %v, want InvalidArgument", err)
|
t.Fatalf("err = %v, want InvalidArgument", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProductCopyMaterialMapRequiresExactCoverage(t *testing.T) {
|
||||||
|
product := &repository.ProductRecord{ImageIDs: []string{"100"}}
|
||||||
|
skus := []repository.SKURecord{{ImageIDs: []string{"200", "100"}}}
|
||||||
|
|
||||||
|
mappings, err := productCopyMaterialMap(product, skus, []*productv1.MaterialIDMapping{
|
||||||
|
{SourceMaterialId: "100", TargetMaterialId: "300"},
|
||||||
|
{SourceMaterialId: "200", TargetMaterialId: "400"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("productCopyMaterialMap() error = %v", err)
|
||||||
|
}
|
||||||
|
if mappings["100"] != "300" || mappings["200"] != "400" {
|
||||||
|
t.Fatalf("mappings = %#v", mappings)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = productCopyMaterialMap(product, skus, []*productv1.MaterialIDMapping{
|
||||||
|
{SourceMaterialId: "100", TargetMaterialId: "300"},
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("productCopyMaterialMap() error = nil, want missing mapping error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user