mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: implement ListProductSKUs to return current SKU stock projections for a product
This commit is contained in:
parent
9c31d0e869
commit
bb82460117
@ -137,6 +137,13 @@ type SKURecord struct {
|
|||||||
Inventory *InventoryRecord
|
Inventory *InventoryRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProductSKUStockRecord is the compact current-SKU projection used by SKU list queries.
|
||||||
|
type ProductSKUStockRecord struct {
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
StockQuantity int32
|
||||||
|
}
|
||||||
|
|
||||||
// InventoryRecord is the persistence model for product.product_item_inventory.
|
// InventoryRecord is the persistence model for product.product_item_inventory.
|
||||||
type InventoryRecord struct {
|
type InventoryRecord struct {
|
||||||
ID string
|
ID string
|
||||||
@ -2269,6 +2276,69 @@ func (r *ProductRepository) GetProduct(ctx context.Context, productID string, sc
|
|||||||
return product, skus[productID], nil
|
return product, skus[productID], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListProductSKUStocks loads every current SKU and its inventory pool quantity for one product.
|
||||||
|
// Archived and soft-deleted SKU rows are historical records and are deliberately excluded.
|
||||||
|
func (r *ProductRepository) ListProductSKUStocks(ctx context.Context, productID string, scope StoreScope) ([]ProductSKUStockRecord, error) {
|
||||||
|
if err := requireWriteScope(scope); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
parsedProductID, err := parseID(productID, "product_id")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var exists bool
|
||||||
|
err = r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT EXISTS(
|
||||||
|
SELECT 1
|
||||||
|
FROM product.products
|
||||||
|
WHERE id = $1
|
||||||
|
AND store_id = $2
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
)
|
||||||
|
`, parsedProductID, scope.StoreID).Scan(&exists)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("check product for sku list: %w", err)
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return nil, ErrProductNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT pi.id,
|
||||||
|
pi.name,
|
||||||
|
COALESCE(inv.stock_quantity, 0)
|
||||||
|
FROM product.product_items AS pi
|
||||||
|
LEFT JOIN product.product_item_inventory AS inv ON inv.sku_id = pi.id
|
||||||
|
WHERE pi.product_id = $1
|
||||||
|
AND pi.store_id = $2
|
||||||
|
AND pi.deleted_at IS NULL
|
||||||
|
AND pi.is_archived = FALSE
|
||||||
|
ORDER BY pi.created_at ASC, pi.id ASC
|
||||||
|
`, parsedProductID, scope.StoreID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("query product sku stocks: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
skus := make([]ProductSKUStockRecord, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
var (
|
||||||
|
sku ProductSKUStockRecord
|
||||||
|
id int64
|
||||||
|
)
|
||||||
|
if err := rows.Scan(&id, &sku.Name, &sku.StockQuantity); err != nil {
|
||||||
|
return nil, fmt.Errorf("scan product sku stock: %w", err)
|
||||||
|
}
|
||||||
|
sku.ID = formatID(id)
|
||||||
|
skus = append(skus, sku)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("iterate product sku stocks: %w", err)
|
||||||
|
}
|
||||||
|
return skus, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetProductBySKU loads the SPU that owns skuID (product_items.id is the SKU PK).
|
// GetProductBySKU loads the SPU that owns skuID (product_items.id is the SKU PK).
|
||||||
func (r *ProductRepository) GetProductBySKU(ctx context.Context, skuID string, scope StoreScope) (*ProductRecord, []SKURecord, error) {
|
func (r *ProductRepository) GetProductBySKU(ctx context.Context, skuID string, scope StoreScope) (*ProductRecord, []SKURecord, error) {
|
||||||
if err := requireWriteScope(scope); err != nil {
|
if err := requireWriteScope(scope); err != nil {
|
||||||
|
|||||||
34
internal/server/list_product_skus_test.go
Normal file
34
internal/server/list_product_skus_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
|
commonv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/common/v1"
|
||||||
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestListProductSKUsRequiresStoreContext(t *testing.T) {
|
||||||
|
s := NewProductServer(nil, nil, nil, nil)
|
||||||
|
_, err := s.ListProductSKUs(context.Background(), &productv1.ListProductSKUsRequest{})
|
||||||
|
if status.Code(err) != codes.FailedPrecondition {
|
||||||
|
t.Fatalf("err = %v, want FailedPrecondition", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListProductSKUsRequiresPositiveProductID(t *testing.T) {
|
||||||
|
s := NewProductServer(nil, nil, nil, nil)
|
||||||
|
_, err := s.ListProductSKUs(context.Background(), &productv1.ListProductSKUsRequest{
|
||||||
|
ProductId: "invalid",
|
||||||
|
StoreContext: &commonv1.StoreContext{
|
||||||
|
TenantId: "10",
|
||||||
|
StoreId: "20",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if status.Code(err) != codes.InvalidArgument {
|
||||||
|
t.Fatalf("err = %v, want InvalidArgument", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -175,6 +175,45 @@ func (s *ProductServer) GetProduct(ctx context.Context, req *productv1.GetProduc
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListProductSKUs returns the compact current-SKU stock projection for one SPU.
|
||||||
|
func (s *ProductServer) ListProductSKUs(ctx context.Context, req *productv1.ListProductSKUsRequest) (*productv1.ListProductSKUsResponse, error) {
|
||||||
|
scope, err := requireStoreScope(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := validatePositiveID(req.GetProductId(), "product_id"); err != nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.sku.list", trace.WithAttributes(attribute.String("product.id", req.GetProductId())))
|
||||||
|
defer dbSpan.End()
|
||||||
|
|
||||||
|
skus, err := s.repo.ListProductSKUStocks(dbCtx, req.GetProductId(), scope)
|
||||||
|
if errors.Is(err, repository.ErrProductNotFound) {
|
||||||
|
dbSpan.SetStatus(otelcodes.Error, "product not found")
|
||||||
|
return nil, status.Error(codes.NotFound, "product not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
dbSpan.RecordError(err)
|
||||||
|
dbSpan.SetStatus(otelcodes.Error, err.Error())
|
||||||
|
s.observer.Logger(ctx).Error("list product skus failed", zap.String("product_id", req.GetProductId()), zap.Error(err))
|
||||||
|
return nil, status.Errorf(codes.Internal, "list product skus: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]*productv1.ProductSKUStock, 0, len(skus))
|
||||||
|
for _, sku := range skus {
|
||||||
|
result = append(result, &productv1.ProductSKUStock{
|
||||||
|
Id: sku.ID,
|
||||||
|
Name: sku.Name,
|
||||||
|
StockQuantity: sku.StockQuantity,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return &productv1.ListProductSKUsResponse{
|
||||||
|
Skus: result,
|
||||||
|
Total: int32(len(result)),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetProductBySku returns the SPU that owns the SKU.
|
// GetProductBySku returns the SPU that owns the SKU.
|
||||||
func (s *ProductServer) GetProductBySku(ctx context.Context, req *productv1.GetProductBySkuRequest) (*productv1.GetProductResponse, error) {
|
func (s *ProductServer) GetProductBySku(ctx context.Context, req *productv1.GetProductBySkuRequest) (*productv1.GetProductResponse, error) {
|
||||||
scope, err := requireStoreScope(req)
|
scope, err := requireStoreScope(req)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user