feat: implement freight charge update functionality for products

This commit is contained in:
BXY 2026-10-27 11:09:45 +08:00
parent c48f722137
commit 6c4170f1e1
3 changed files with 243 additions and 0 deletions

View File

@ -119,6 +119,18 @@ type ProductRecord struct {
Scope StoreScope Scope StoreScope
} }
// ProductFreightChargeUpdate is the persisted freight projection returned by
// the dedicated freight-charge update command.
type ProductFreightChargeUpdate struct {
ProductID string
SPUNo string
FreightChargeType int32
FreightFixedAmount int64
FreightTemplateID string
Version int32
UpdatedAt time.Time
}
// ProductCountRecord contains aggregate counts for product.products. // ProductCountRecord contains aggregate counts for product.products.
type ProductCountRecord struct { type ProductCountRecord struct {
Total int64 Total int64
@ -1616,6 +1628,71 @@ WHERE id = $1
return nil return nil
} }
// UpdateProductFreightChargeBySPUNo atomically updates only the freight
// configuration of one non-deleted, store-scoped product. It intentionally
// increments the product version so a concurrent full-product edit detects
// that its read snapshot is stale.
func (r *ProductRepository) UpdateProductFreightChargeBySPUNo(ctx context.Context, spuNo string, freightChargeType int32, freightFixedAmount int64, freightTemplateID string, scope StoreScope) (*ProductFreightChargeUpdate, error) {
if err := requireWriteScope(scope); err != nil {
return nil, err
}
spuNo = strings.TrimSpace(spuNo)
if spuNo == "" {
return nil, errors.New("spu_no is required")
}
if freightChargeType < 1 || freightChargeType > 2 {
return nil, errors.New("freight_charge_type must be 1 or 2")
}
if freightFixedAmount < 0 {
return nil, errors.New("freight_fixed_amount must be greater than or equal to 0")
}
templateID := sql.NullInt64{}
if freightChargeType == 2 {
var err error
templateID, err = nullableID(freightTemplateID, "freight_template_id")
if err != nil {
return nil, err
}
if !templateID.Valid {
return nil, errors.New("freight_template_id is required when freight_charge_type is 2")
}
}
result := &ProductFreightChargeUpdate{}
var productID int64
var storedTemplateID sql.NullInt64
err := r.db.QueryRowContext(ctx, `
UPDATE product.products
SET freight_charge_type = $3,
freight_fixed_amount = $4,
freight_template_id = $5,
version = version + 1,
updated_at = NOW()
WHERE store_id = $1 AND spu_no = $2 AND deleted_at IS NULL
RETURNING id, spu_no, freight_charge_type, freight_fixed_amount, freight_template_id, version, updated_at
`, scope.StoreID, spuNo, int16(freightChargeType), freightFixedAmount, templateID).Scan(
&productID,
&result.SPUNo,
&result.FreightChargeType,
&result.FreightFixedAmount,
&storedTemplateID,
&result.Version,
&result.UpdatedAt,
)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrProductNotFound
}
if err != nil {
return nil, fmt.Errorf("update product freight charge by spu_no: %w", err)
}
result.ProductID = formatID(productID)
if storedTemplateID.Valid {
result.FreightTemplateID = formatID(storedTemplateID.Int64)
}
return result, nil
}
// UpdateProductWithSKUChange atomically persists a merged SPU update and an optional SKU operation. // UpdateProductWithSKUChange atomically persists a merged SPU update and an optional SKU operation.
func (r *ProductRepository) UpdateProductWithSKUChange(ctx context.Context, input ProductUpdateInput) (*SKUChangeResult, error) { func (r *ProductRepository) UpdateProductWithSKUChange(ctx context.Context, input ProductUpdateInput) (*SKUChangeResult, error) {
if err := requireWriteScope(input.Scope); err != nil { if err := requireWriteScope(input.Scope); err != nil {

View File

@ -0,0 +1,83 @@
package server
import (
"strings"
"testing"
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
)
func TestProductFreightChargeFromUpdateRequest(t *testing.T) {
fixedAmount := int64(1000)
templateID := "2114688315977371648"
tooLongSPU := strings.Repeat("a", 101)
tests := []struct {
name string
req *productv1.UpdateProductFreightChargeRequest
wantErr string
wantType int32
wantFee int64
wantID string
}{
{
name: "fixed freight",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: " SPU-001 ",
FreightChargeType: 1,
FreightFixedAmount: &fixedAmount,
},
wantType: 1,
wantFee: 1000,
},
{
name: "freight template",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: "SPU-001",
FreightChargeType: 2,
FreightTemplateId: &templateID,
},
wantType: 2,
wantID: templateID,
},
{
name: "fixed freight needs amount",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: "SPU-001", FreightChargeType: 1,
},
wantErr: "freight_fixed_amount is required",
},
{
name: "template mode rejects amount",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: "SPU-001", FreightChargeType: 2, FreightFixedAmount: &fixedAmount, FreightTemplateId: &templateID,
},
wantErr: "freight_fixed_amount must be omitted",
},
{
name: "spu cannot exceed limit",
req: &productv1.UpdateProductFreightChargeRequest{
SpuNo: tooLongSPU, FreightChargeType: 1, FreightFixedAmount: &fixedAmount,
},
wantErr: "spu_no must contain at most 100 characters",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spuNo, chargeType, fee, gotTemplateID, err := productFreightChargeFromUpdateRequest(tt.req)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("error = %v, want containing %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatalf("productFreightChargeFromUpdateRequest() error = %v", err)
}
if spuNo != "SPU-001" || chargeType != tt.wantType || fee != tt.wantFee || gotTemplateID != tt.wantID {
t.Fatalf("result = (%q, %d, %d, %q), want (%q, %d, %d, %q)", spuNo, chargeType, fee, gotTemplateID, "SPU-001", tt.wantType, tt.wantFee, tt.wantID)
}
})
}
}

View File

@ -534,6 +534,51 @@ func (s *ProductServer) UpdateProduct(ctx context.Context, req *productv1.Update
}, nil }, nil
} }
// UpdateProductFreightCharge updates only one product's freight configuration,
// locating it by the store-scoped SPU business number.
func (s *ProductServer) UpdateProductFreightCharge(ctx context.Context, req *productv1.UpdateProductFreightChargeRequest) (*productv1.UpdateProductFreightChargeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "request is required")
}
scope, err := requireStoreScope(req)
if err != nil {
return nil, err
}
spuNo, freightChargeType, freightFixedAmount, freightTemplateID, err := productFreightChargeFromUpdateRequest(req)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
if freightChargeType == 2 {
if err := validatePositiveID(freightTemplateID, "freight_template_id"); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
if err := s.repo.FreightTemplateExists(ctx, freightTemplateID, scope); err != nil {
if errors.Is(err, repository.ErrFreightTemplateNotFound) {
return nil, status.Error(codes.InvalidArgument, "freight_template_id not found or disabled")
}
return nil, status.Errorf(codes.Internal, "validate freight template: %v", err)
}
}
updated, err := s.repo.UpdateProductFreightChargeBySPUNo(ctx, spuNo, freightChargeType, freightFixedAmount, freightTemplateID, scope)
if errors.Is(err, repository.ErrProductNotFound) {
return nil, status.Error(codes.NotFound, "product not found")
}
if err != nil {
s.observer.Logger(ctx).Error("update product freight charge failed", zap.String("spu_no", spuNo), zap.Error(err))
return nil, status.Errorf(codes.Internal, "update product freight charge: %v", err)
}
return &productv1.UpdateProductFreightChargeResponse{
ProductId: updated.ProductID,
SpuNo: updated.SPUNo,
FreightChargeType: updated.FreightChargeType,
FreightFixedAmount: updated.FreightFixedAmount,
FreightTemplateId: updated.FreightTemplateID,
Version: updated.Version,
UpdatedAt: updated.UpdatedAt.UTC().Format(time.RFC3339Nano),
}, nil
}
// UpdateProductStatus changes the product lifecycle status. // UpdateProductStatus changes the product lifecycle status.
func (s *ProductServer) UpdateProductStatus(ctx context.Context, req *productv1.UpdateProductStatusRequest) (*productv1.UpdateProductStatusResponse, error) { func (s *ProductServer) UpdateProductStatus(ctx context.Context, req *productv1.UpdateProductStatusRequest) (*productv1.UpdateProductStatusResponse, error) {
scope, err := requireStoreScope(req) scope, err := requireStoreScope(req)
@ -2362,6 +2407,44 @@ func containsUpdatePath(paths []string, target string) bool {
return false return false
} }
func productFreightChargeFromUpdateRequest(req *productv1.UpdateProductFreightChargeRequest) (spuNo string, freightChargeType int32, freightFixedAmount int64, freightTemplateID string, err error) {
spuNo = strings.TrimSpace(req.GetSpuNo())
if spuNo == "" {
return "", 0, 0, "", errors.New("spu_no is required")
}
if utf8.RuneCountInString(spuNo) > 100 {
return "", 0, 0, "", errors.New("spu_no must contain at most 100 characters")
}
freightChargeType = req.GetFreightChargeType()
switch freightChargeType {
case 1:
if req.FreightFixedAmount == nil {
return "", 0, 0, "", errors.New("freight_fixed_amount is required when freight_charge_type is 1")
}
if *req.FreightFixedAmount < 0 {
return "", 0, 0, "", errors.New("freight_fixed_amount must be greater than or equal to 0")
}
if req.FreightTemplateId != nil {
return "", 0, 0, "", errors.New("freight_template_id must be omitted when freight_charge_type is 1")
}
return spuNo, freightChargeType, *req.FreightFixedAmount, "", nil
case 2:
if req.FreightFixedAmount != nil {
return "", 0, 0, "", errors.New("freight_fixed_amount must be omitted when freight_charge_type is 2")
}
if req.FreightTemplateId == nil {
return "", 0, 0, "", errors.New("freight_template_id is required when freight_charge_type is 2")
}
freightTemplateID = strings.TrimSpace(*req.FreightTemplateId)
if freightTemplateID == "" {
return "", 0, 0, "", errors.New("freight_template_id is required when freight_charge_type is 2")
}
return spuNo, freightChargeType, 0, freightTemplateID, nil
default:
return "", 0, 0, "", errors.New("freight_charge_type must be 1 or 2")
}
}
func validateProductForUpdate(product *repository.ProductRecord) error { func validateProductForUpdate(product *repository.ProductRecord) error {
if product == nil { if product == nil {
return errors.New("product is required") return errors.New("product is required")