mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
- Introduced new tables for suppliers and brands in the product schema with necessary fields and constraints. - Implemented CRUD operations for suppliers and brands in the catalog master server. - Added validation for supplier and brand references in product creation and update processes. - Updated SQL scripts to recreate product tables with new suppliers and brands structure. - Enhanced permissions for the new tables in the database.
502 lines
19 KiB
Go
502 lines
19 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
|
)
|
|
|
|
const maxCatalogMasterNameLength = 100
|
|
const maxCatalogMasterCodeLength = 64
|
|
|
|
func (s *ProductServer) CreateSupplier(ctx context.Context, req *productv1.CreateSupplierRequest) (*productv1.CreateSupplierResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
record, err := supplierFromCreate(req)
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if s.idgen == nil {
|
|
return nil, status.Error(codes.Internal, "id generator is not configured")
|
|
}
|
|
record.ID = s.idgen.NextString()
|
|
record.Scope = scope
|
|
created, err := s.repo.CreateSupplier(ctx, record)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.CreateSupplierResponse{Supplier: toProtoSupplier(created)}, nil
|
|
}
|
|
func (s *ProductServer) GetSupplier(ctx context.Context, req *productv1.GetSupplierRequest) (*productv1.GetSupplierResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetSupplierId(), "supplier_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
value, err := s.repo.GetSupplier(ctx, req.GetSupplierId(), scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.GetSupplierResponse{Supplier: toProtoSupplier(value)}, nil
|
|
}
|
|
func (s *ProductServer) ListSuppliers(ctx context.Context, req *productv1.ListSuppliersRequest) (*productv1.ListSuppliersResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
page, size, err := catalogPagination(req.GetPage(), req.GetPageSize())
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
var statusValue *int32
|
|
if req.Status != nil {
|
|
if err := validateCatalogStatus(*req.Status); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
statusValue = req.Status
|
|
}
|
|
values, total, err := s.repo.ListSuppliers(ctx, repository.SupplierListFilter{Query: strings.TrimSpace(req.GetQuery()), Status: statusValue, Page: page, PageSize: size}, scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
out := make([]*productv1.Supplier, 0, len(values))
|
|
for i := range values {
|
|
out = append(out, toProtoSupplier(&values[i]))
|
|
}
|
|
return &productv1.ListSuppliersResponse{Suppliers: out, Page: page, PageSize: size, Total: total, TotalPages: catalogTotalPages(total, size)}, nil
|
|
}
|
|
func (s *ProductServer) UpdateSupplier(ctx context.Context, req *productv1.UpdateSupplierRequest) (*productv1.UpdateSupplierResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetSupplierId(), "supplier_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
patch, err := supplierPatch(req)
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
value, err := s.repo.UpdateSupplier(ctx, req.GetSupplierId(), patch, scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.UpdateSupplierResponse{Supplier: toProtoSupplier(value)}, nil
|
|
}
|
|
func (s *ProductServer) UpdateSupplierStatus(ctx context.Context, req *productv1.UpdateSupplierStatusRequest) (*productv1.UpdateSupplierStatusResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetSupplierId(), "supplier_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if err := validateCatalogStatus(req.GetStatus()); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if req.GetExpectedVersion() < 0 {
|
|
return nil, status.Error(codes.InvalidArgument, "expected_version must be non-negative")
|
|
}
|
|
value, err := s.repo.UpdateSupplierStatus(ctx, req.GetSupplierId(), req.GetStatus(), req.GetExpectedVersion(), scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.UpdateSupplierStatusResponse{Supplier: toProtoSupplier(value)}, nil
|
|
}
|
|
func (s *ProductServer) DeleteSupplier(ctx context.Context, req *productv1.DeleteSupplierRequest) (*productv1.DeleteSupplierResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetSupplierId(), "supplier_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if req.GetExpectedVersion() < 0 {
|
|
return nil, status.Error(codes.InvalidArgument, "expected_version must be non-negative")
|
|
}
|
|
deleted, err := s.repo.DeleteSupplier(ctx, req.GetSupplierId(), req.GetExpectedVersion(), scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.DeleteSupplierResponse{SupplierId: req.GetSupplierId(), DeletedAt: timeToMillisString(*deleted)}, nil
|
|
}
|
|
|
|
func (s *ProductServer) CreateBrand(ctx context.Context, req *productv1.CreateBrandRequest) (*productv1.CreateBrandResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
record, err := brandFromCreate(req)
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if s.idgen == nil {
|
|
return nil, status.Error(codes.Internal, "id generator is not configured")
|
|
}
|
|
record.ID = s.idgen.NextString()
|
|
record.Scope = scope
|
|
created, err := s.repo.CreateBrand(ctx, record)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.CreateBrandResponse{Brand: toProtoBrand(created)}, nil
|
|
}
|
|
func (s *ProductServer) GetBrand(ctx context.Context, req *productv1.GetBrandRequest) (*productv1.GetBrandResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetBrandId(), "brand_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
value, err := s.repo.GetBrand(ctx, req.GetBrandId(), scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.GetBrandResponse{Brand: toProtoBrand(value)}, nil
|
|
}
|
|
func (s *ProductServer) ListBrands(ctx context.Context, req *productv1.ListBrandsRequest) (*productv1.ListBrandsResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
page, size, err := catalogPagination(req.GetPage(), req.GetPageSize())
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if req.GetSupplierId() != "" {
|
|
if err := validatePositiveID(req.GetSupplierId(), "supplier_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
}
|
|
var statusValue *int32
|
|
if req.Status != nil {
|
|
if err := validateCatalogStatus(*req.Status); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
statusValue = req.Status
|
|
}
|
|
values, total, err := s.repo.ListBrands(ctx, repository.BrandListFilter{Query: strings.TrimSpace(req.GetQuery()), SupplierID: strings.TrimSpace(req.GetSupplierId()), Status: statusValue, Page: page, PageSize: size}, scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
out := make([]*productv1.Brand, 0, len(values))
|
|
for i := range values {
|
|
out = append(out, toProtoBrand(&values[i]))
|
|
}
|
|
return &productv1.ListBrandsResponse{Brands: out, Page: page, PageSize: size, Total: total, TotalPages: catalogTotalPages(total, size)}, nil
|
|
}
|
|
func (s *ProductServer) UpdateBrand(ctx context.Context, req *productv1.UpdateBrandRequest) (*productv1.UpdateBrandResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetBrandId(), "brand_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
patch, err := brandPatch(req)
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
value, err := s.repo.UpdateBrand(ctx, req.GetBrandId(), patch, scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.UpdateBrandResponse{Brand: toProtoBrand(value)}, nil
|
|
}
|
|
func (s *ProductServer) UpdateBrandStatus(ctx context.Context, req *productv1.UpdateBrandStatusRequest) (*productv1.UpdateBrandStatusResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetBrandId(), "brand_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if err := validateCatalogStatus(req.GetStatus()); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if req.GetExpectedVersion() < 0 {
|
|
return nil, status.Error(codes.InvalidArgument, "expected_version must be non-negative")
|
|
}
|
|
value, err := s.repo.UpdateBrandStatus(ctx, req.GetBrandId(), req.GetStatus(), req.GetExpectedVersion(), scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.UpdateBrandStatusResponse{Brand: toProtoBrand(value)}, nil
|
|
}
|
|
func (s *ProductServer) DeleteBrand(ctx context.Context, req *productv1.DeleteBrandRequest) (*productv1.DeleteBrandResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
scope, err := requireStoreScope(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validatePositiveID(req.GetBrandId(), "brand_id"); err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
if req.GetExpectedVersion() < 0 {
|
|
return nil, status.Error(codes.InvalidArgument, "expected_version must be non-negative")
|
|
}
|
|
deleted, err := s.repo.DeleteBrand(ctx, req.GetBrandId(), req.GetExpectedVersion(), scope)
|
|
if err != nil {
|
|
return nil, catalogGRPCError(err)
|
|
}
|
|
return &productv1.DeleteBrandResponse{BrandId: req.GetBrandId(), DeletedAt: timeToMillisString(*deleted)}, nil
|
|
}
|
|
|
|
func supplierFromCreate(req *productv1.CreateSupplierRequest) (repository.SupplierRecord, error) {
|
|
statusValue := int32(1)
|
|
if req.Status != nil {
|
|
statusValue = *req.Status
|
|
}
|
|
if err := validateCatalogStatus(statusValue); err != nil {
|
|
return repository.SupplierRecord{}, err
|
|
}
|
|
attachments, err := attachmentsFromProto(req.GetAttachments())
|
|
if err != nil {
|
|
return repository.SupplierRecord{}, err
|
|
}
|
|
return repository.SupplierRecord{Name: catalogName(req.GetName(), "name"), Alias: strings.TrimSpace(req.GetAlias()), SupplierCode: catalogCode(req.GetSupplierCode(), "supplier_code"), Attachments: attachments, ContactName: strings.TrimSpace(req.GetContactName()), ContactPhone: strings.TrimSpace(req.GetContactPhone()), Remark: strings.TrimSpace(req.GetRemark()), Status: statusValue}, validateCatalogMasterFields(catalogName(req.GetName(), "name"), catalogCode(req.GetSupplierCode(), "supplier_code"))
|
|
}
|
|
func brandFromCreate(req *productv1.CreateBrandRequest) (repository.BrandRecord, error) {
|
|
statusValue := int32(1)
|
|
if req.Status != nil {
|
|
statusValue = *req.Status
|
|
}
|
|
if err := validateCatalogStatus(statusValue); err != nil {
|
|
return repository.BrandRecord{}, err
|
|
}
|
|
if err := validatePositiveID(req.GetSupplierId(), "supplier_id"); err != nil {
|
|
return repository.BrandRecord{}, err
|
|
}
|
|
name, code := catalogName(req.GetName(), "name"), catalogCode(req.GetBrandCode(), "brand_code")
|
|
if err := validateCatalogMasterFields(name, code); err != nil {
|
|
return repository.BrandRecord{}, err
|
|
}
|
|
return repository.BrandRecord{Name: name, Alias: strings.TrimSpace(req.GetAlias()), BrandCode: code, SupplierID: req.GetSupplierId(), Status: statusValue}, nil
|
|
}
|
|
func supplierPatch(req *productv1.UpdateSupplierRequest) (repository.SupplierPatch, error) {
|
|
if req.GetSupplier() == nil {
|
|
return repository.SupplierPatch{}, errors.New("supplier is required")
|
|
}
|
|
if req.GetExpectedVersion() < 0 {
|
|
return repository.SupplierPatch{}, errors.New("expected_version must be non-negative")
|
|
}
|
|
out := repository.SupplierPatch{ExpectedVersion: req.GetExpectedVersion()}
|
|
for _, field := range req.GetUpdateMask().GetPaths() {
|
|
switch field {
|
|
case "name":
|
|
v := catalogName(req.GetSupplier().GetName(), "name")
|
|
if err := validateCatalogName(v); err != nil {
|
|
return out, err
|
|
}
|
|
out.Name = &v
|
|
case "alias":
|
|
v := strings.TrimSpace(req.GetSupplier().GetAlias())
|
|
out.Alias = &v
|
|
case "supplier_code":
|
|
v := catalogCode(req.GetSupplier().GetSupplierCode(), "supplier_code")
|
|
if err := validateCatalogCode(v); err != nil {
|
|
return out, err
|
|
}
|
|
out.SupplierCode = &v
|
|
case "attachments":
|
|
v, err := attachmentsFromProto(req.GetSupplier().GetAttachments())
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out.Attachments = &v
|
|
case "contact_name":
|
|
v := strings.TrimSpace(req.GetSupplier().GetContactName())
|
|
out.ContactName = &v
|
|
case "contact_phone":
|
|
v := strings.TrimSpace(req.GetSupplier().GetContactPhone())
|
|
out.ContactPhone = &v
|
|
case "remark":
|
|
v := strings.TrimSpace(req.GetSupplier().GetRemark())
|
|
out.Remark = &v
|
|
default:
|
|
return out, fmt.Errorf("unsupported update_mask path: %s", field)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
func brandPatch(req *productv1.UpdateBrandRequest) (repository.BrandPatch, error) {
|
|
if req.GetBrand() == nil {
|
|
return repository.BrandPatch{}, errors.New("brand is required")
|
|
}
|
|
if req.GetExpectedVersion() < 0 {
|
|
return repository.BrandPatch{}, errors.New("expected_version must be non-negative")
|
|
}
|
|
out := repository.BrandPatch{ExpectedVersion: req.GetExpectedVersion()}
|
|
for _, field := range req.GetUpdateMask().GetPaths() {
|
|
switch field {
|
|
case "name":
|
|
v := catalogName(req.GetBrand().GetName(), "name")
|
|
if err := validateCatalogName(v); err != nil {
|
|
return out, err
|
|
}
|
|
out.Name = &v
|
|
case "alias":
|
|
v := strings.TrimSpace(req.GetBrand().GetAlias())
|
|
out.Alias = &v
|
|
case "brand_code":
|
|
v := catalogCode(req.GetBrand().GetBrandCode(), "brand_code")
|
|
if err := validateCatalogCode(v); err != nil {
|
|
return out, err
|
|
}
|
|
out.BrandCode = &v
|
|
case "supplier_id":
|
|
v := strings.TrimSpace(req.GetBrand().GetSupplierId())
|
|
if err := validatePositiveID(v, "supplier_id"); err != nil {
|
|
return out, err
|
|
}
|
|
out.SupplierID = &v
|
|
default:
|
|
return out, fmt.Errorf("unsupported update_mask path: %s", field)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
func attachmentsFromProto(values []*productv1.SupplierAttachment) ([]repository.SupplierAttachmentRecord, error) {
|
|
out := make([]repository.SupplierAttachmentRecord, 0, len(values))
|
|
for _, item := range values {
|
|
if item == nil || strings.TrimSpace(item.GetFileId()) == "" {
|
|
return nil, errors.New("attachments[].file_id is required")
|
|
}
|
|
out = append(out, repository.SupplierAttachmentRecord{FileID: strings.TrimSpace(item.GetFileId()), FileName: strings.TrimSpace(item.GetFileName()), FileType: strings.TrimSpace(item.GetFileType())})
|
|
}
|
|
return out, nil
|
|
}
|
|
func catalogName(value, _ string) string { return strings.TrimSpace(value) }
|
|
func catalogCode(value, _ string) string { return strings.TrimSpace(value) }
|
|
func validateCatalogMasterFields(name, code string) error {
|
|
if err := validateCatalogName(name); err != nil {
|
|
return err
|
|
}
|
|
return validateCatalogCode(code)
|
|
}
|
|
func validateCatalogName(value string) error {
|
|
if value == "" {
|
|
return errors.New("name is required")
|
|
}
|
|
if len([]rune(value)) > maxCatalogMasterNameLength {
|
|
return fmt.Errorf("name must be at most %d characters", maxCatalogMasterNameLength)
|
|
}
|
|
return nil
|
|
}
|
|
func validateCatalogCode(value string) error {
|
|
if value == "" {
|
|
return errors.New("code is required")
|
|
}
|
|
if len([]rune(value)) > maxCatalogMasterCodeLength {
|
|
return fmt.Errorf("code must be at most %d characters", maxCatalogMasterCodeLength)
|
|
}
|
|
return nil
|
|
}
|
|
func validateCatalogStatus(value int32) error {
|
|
if value != 0 && value != 1 {
|
|
return errors.New("status must be 0 or 1")
|
|
}
|
|
return nil
|
|
}
|
|
func catalogPagination(page, size int32) (int32, int32, error) {
|
|
if page == 0 {
|
|
page = 1
|
|
}
|
|
if size == 0 {
|
|
size = defaultPageSize
|
|
}
|
|
if page < 1 {
|
|
return 0, 0, errors.New("page must be positive")
|
|
}
|
|
if size < 1 || size > maxPageSize {
|
|
return 0, 0, fmt.Errorf("page_size must be between 1 and %d", maxPageSize)
|
|
}
|
|
return page, size, nil
|
|
}
|
|
func catalogTotalPages(total int64, size int32) int32 {
|
|
if total == 0 {
|
|
return 0
|
|
}
|
|
return int32((total + int64(size) - 1) / int64(size))
|
|
}
|
|
func toProtoSupplier(value *repository.SupplierRecord) *productv1.Supplier {
|
|
if value == nil {
|
|
return nil
|
|
}
|
|
out := &productv1.Supplier{SupplierId: value.ID, Name: value.Name, Alias: value.Alias, SupplierCode: value.SupplierCode, ContactName: value.ContactName, ContactPhone: value.ContactPhone, Remark: value.Remark, Status: value.Status, TenantId: fmt.Sprint(value.Scope.TenantID), StoreId: fmt.Sprint(value.Scope.StoreID), CreatedAt: timeToMillisString(value.CreatedAt), UpdatedAt: timeToMillisString(value.UpdatedAt), Version: value.Version}
|
|
for _, item := range value.Attachments {
|
|
out.Attachments = append(out.Attachments, &productv1.SupplierAttachment{FileId: item.FileID, FileName: item.FileName, FileType: item.FileType})
|
|
}
|
|
out.DeletedAt = timePtrToMillisString(value.DeletedAt)
|
|
return out
|
|
}
|
|
func toProtoBrand(value *repository.BrandRecord) *productv1.Brand {
|
|
if value == nil {
|
|
return nil
|
|
}
|
|
return &productv1.Brand{BrandId: value.ID, Name: value.Name, Alias: value.Alias, BrandCode: value.BrandCode, SupplierId: value.SupplierID, Status: value.Status, TenantId: fmt.Sprint(value.Scope.TenantID), StoreId: fmt.Sprint(value.Scope.StoreID), CreatedAt: timeToMillisString(value.CreatedAt), UpdatedAt: timeToMillisString(value.UpdatedAt), DeletedAt: timePtrToMillisString(value.DeletedAt), Version: value.Version}
|
|
}
|
|
func catalogGRPCError(err error) error {
|
|
switch {
|
|
case errors.Is(err, repository.ErrSupplierNotFound), errors.Is(err, repository.ErrBrandNotFound):
|
|
return status.Error(codes.NotFound, err.Error())
|
|
case errors.Is(err, repository.ErrSupplierVersionConflict), errors.Is(err, repository.ErrBrandVersionConflict):
|
|
return status.Error(codes.Aborted, err.Error())
|
|
case errors.Is(err, repository.ErrDuplicateSupplierCode), errors.Is(err, repository.ErrDuplicateBrandCode):
|
|
return status.Error(codes.AlreadyExists, err.Error())
|
|
case errors.Is(err, repository.ErrSupplierHasBrands), errors.Is(err, repository.ErrSupplierHasActiveBrands), errors.Is(err, repository.ErrBrandInUse), errors.Is(err, repository.ErrSupplierInactive):
|
|
return status.Error(codes.FailedPrecondition, err.Error())
|
|
case errors.Is(err, repository.ErrStoreScopeRequired):
|
|
return status.Error(codes.InvalidArgument, err.Error())
|
|
default:
|
|
return status.Error(codes.Internal, "catalog master operation failed")
|
|
}
|
|
}
|