mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: implement BatchGetActiveFreightTemplates and update cart item product handling
This commit is contained in:
parent
1585839c1e
commit
40ced00ed7
@ -120,6 +120,53 @@ WHERE id = $1 AND store_id = $2 AND deleted_at IS NULL
|
||||
return rec, err
|
||||
}
|
||||
|
||||
// BatchGetActiveFreightTemplates loads the distinct, enabled templates needed
|
||||
// by one storefront page in a single store-scoped query.
|
||||
func (r *ProductRepository) BatchGetActiveFreightTemplates(ctx context.Context, templateIDs []string, scope StoreScope) (map[string]*FreightTemplateRecord, error) {
|
||||
if err := requireWriteScope(scope); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make(map[string]*FreightTemplateRecord)
|
||||
if len(templateIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
placeholders := make([]string, 0, len(templateIDs))
|
||||
args := make([]any, 0, len(templateIDs)+1)
|
||||
seen := make(map[string]struct{}, len(templateIDs))
|
||||
for _, templateID := range templateIDs {
|
||||
if _, ok := seen[templateID]; ok {
|
||||
continue
|
||||
}
|
||||
id, err := parseID(templateID, "template_id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen[templateID] = struct{}{}
|
||||
args = append(args, id)
|
||||
placeholders = append(placeholders, fmt.Sprintf("$%d", len(args)))
|
||||
}
|
||||
args = append(args, scope.StoreID)
|
||||
query := fmt.Sprintf(`SELECT id::text, name, pricing_mode, default_fee, rules_json, status, version, created_at, updated_at, deleted_at
|
||||
FROM product.freight_templates
|
||||
WHERE id IN (%s) AND store_id = $%d AND status = 1 AND deleted_at IS NULL`, strings.Join(placeholders, ","), len(args))
|
||||
rows, err := r.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("batch get freight templates: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
rec, err := scanFreightTemplate(rows, scope)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan freight template: %w", err)
|
||||
}
|
||||
result[rec.ID] = rec
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate freight templates: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ListFreightTemplates pages templates.
|
||||
func (r *ProductRepository) ListFreightTemplates(ctx context.Context, filter FreightTemplateListFilter, scope StoreScope) ([]FreightTemplateRecord, int64, error) {
|
||||
if err := requireWriteScope(scope); err != nil {
|
||||
|
||||
17
internal/server/cart_freight_test.go
Normal file
17
internal/server/cart_freight_test.go
Normal file
@ -0,0 +1,17 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
||||
)
|
||||
|
||||
func TestCartProductIncludesFreightConfiguration(t *testing.T) {
|
||||
got := toProtoCartItemProduct("801", repository.CartItemProductRecord{
|
||||
ProductID: "1001", SKUID: "1101",
|
||||
Product: &repository.ProductRecord{FreightChargeType: 2, FreightTemplateID: "9001"},
|
||||
})
|
||||
if got.GetFreightChargeType() != 2 || got.GetFreightTemplateId() != "9001" {
|
||||
t.Fatalf("cart product freight = %#v", got)
|
||||
}
|
||||
}
|
||||
@ -342,10 +342,25 @@ func (s *ProductServer) BatchGetCartItemProduct(ctx context.Context, req *produc
|
||||
s.observer.Logger(ctx).Error("batch get cart item products incomplete", zap.Error(err))
|
||||
return nil, status.Error(codes.Internal, "batch get cart item products returned incomplete data")
|
||||
}
|
||||
templateIDs := make([]string, 0, len(records))
|
||||
for _, record := range records {
|
||||
if record.Product != nil && record.Product.FreightChargeType == 2 && record.Product.FreightTemplateID != "" {
|
||||
templateIDs = append(templateIDs, record.Product.FreightTemplateID)
|
||||
}
|
||||
}
|
||||
templates, err := s.repo.BatchGetActiveFreightTemplates(dbCtx, templateIDs, scope)
|
||||
if err != nil {
|
||||
s.observer.Logger(ctx).Error("batch get cart freight templates failed", zap.Error(err))
|
||||
return nil, status.Errorf(codes.Internal, "batch get cart freight templates: %v", err)
|
||||
}
|
||||
|
||||
items := make([]*productv1.CartItemProduct, 0, len(records))
|
||||
for index, record := range records {
|
||||
items = append(items, toProtoCartItemProduct(req.GetItems()[index].GetLineId(), record))
|
||||
item := toProtoCartItemProduct(req.GetItems()[index].GetLineId(), record)
|
||||
if record.Product != nil {
|
||||
item.FreightTemplate = toProtoFreightTemplate(templates[record.Product.FreightTemplateID])
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return &productv1.BatchGetCartItemProductResponse{Items: items}, nil
|
||||
}
|
||||
@ -3137,6 +3152,9 @@ func toProtoCartItemProduct(lineID string, record repository.CartItemProductReco
|
||||
result.DeliveryStartAt = timePtrToTimestamp(record.Product.DeliveryStartAt)
|
||||
result.DeliveryAfterPaidDays = record.Product.DeliveryAfterPaidDays
|
||||
result.PurchaseLimitQuantity = record.Product.PurchaseLimitQuantity
|
||||
result.FreightChargeType = record.Product.FreightChargeType
|
||||
result.FreightFixedAmount = record.Product.FreightFixedAmount
|
||||
result.FreightTemplateId = record.Product.FreightTemplateID
|
||||
if record.SKU == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user