mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
commonv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/common/v1"
|
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
|
)
|
|
|
|
type storeContextGetter interface {
|
|
GetStoreContext() *commonv1.StoreContext
|
|
}
|
|
|
|
func requireStoreScope(req storeContextGetter) (repository.StoreScope, error) {
|
|
if req == nil {
|
|
return repository.StoreScope{}, status.Error(codes.FailedPrecondition, "store_context is required")
|
|
}
|
|
sc := req.GetStoreContext()
|
|
if sc == nil || strings.TrimSpace(sc.GetStoreId()) == "" || strings.TrimSpace(sc.GetTenantId()) == "" {
|
|
return repository.StoreScope{}, status.Error(codes.FailedPrecondition, "store_context.store_id and tenant_id are required")
|
|
}
|
|
storeID, err := parsePositiveID(strings.TrimSpace(sc.GetStoreId()), "store_context.store_id")
|
|
if err != nil {
|
|
return repository.StoreScope{}, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
tenantID, err := parsePositiveID(strings.TrimSpace(sc.GetTenantId()), "store_context.tenant_id")
|
|
if err != nil {
|
|
return repository.StoreScope{}, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
return repository.StoreScope{TenantID: tenantID, StoreID: storeID}, nil
|
|
}
|
|
|
|
func parsePositiveID(raw, field string) (int64, error) {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil || id <= 0 {
|
|
return 0, fmt.Errorf("%s must be a positive snowflake id", field)
|
|
}
|
|
return id, nil
|
|
}
|