fix: some api bug

This commit is contained in:
wwweww
2026-03-31 22:12:06 +08:00
parent c5ff4f0216
commit e7970ac25f
219 changed files with 16195 additions and 2126 deletions
+52 -5
View File
@@ -4,7 +4,11 @@ import (
"context"
"encoding/json"
"errors"
"juwan-backend/app/shop/rpc/internal/models/schema"
"juwan-backend/app/snowflake/rpc/snowflake"
pb2 "juwan-backend/app/users/rpc/pb"
"slices"
"strings"
"juwan-backend/app/shop/rpc/internal/svc"
"juwan-backend/app/shop/rpc/pb"
@@ -27,8 +31,25 @@ func NewAddShopsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddShops
}
}
var allowedRoles = []string{"owner", "admin"}
var allowedCommissionTypes = []string{"fixed", "percentage"}
var allowedDispatchModes = []string{"manual", "auto"}
// -----------------------shops-----------------------
func (l *AddShopsLogic) AddShops(in *pb.AddShopsReq) (*pb.AddShopsResp, error) {
user, err := l.svcCtx.UsersRpc.GetUsersById(l.ctx, &pb2.GetUsersByIdReq{
Id: in.OwnerId,
})
if err != nil {
logx.Errorf("add shops err: %v", err)
return nil, errors.New("user not found")
}
if !slices.Contains(allowedRoles, user.Users.CurrentRole) {
logx.Errorf("add shops err: user %v has no permission to add shop", in.OwnerId)
return nil, errors.New("no permission to add shop")
}
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
if err != nil {
logx.Errorf("addPlayerServices err:%v", err)
@@ -44,28 +65,53 @@ func (l *AddShopsLogic) AddShops(in *pb.AddShopsReq) (*pb.AddShopsResp, error) {
rating, err := decimal.NewFromString(in.Rating)
if err != nil {
logx.Errorf("addPlayerServices new from string err:%v", err)
return nil, errors.New("invalid rating")
}
commissionValue, err := decimal.NewFromString(in.CommissionValue)
if err != nil {
logx.Errorf("addPlayerServices new from string err:%v", err)
return nil, errors.New("invalid commissionValue")
}
_, err = l.svcCtx.ShopModelRO.Shops.Create().
commissionType := strings.ToLower(strings.TrimSpace(in.CommissionType))
if commissionType == "" {
commissionType = "percentage"
}
if !slices.Contains(allowedCommissionTypes, commissionType) {
logx.Errorf("addPlayerServices contains err: invalid commissionType %v", in.CommissionType)
return nil, errors.New("invalid commissionType")
}
dispatchMode := strings.ToLower(strings.TrimSpace(in.DispatchMode))
if dispatchMode == "" {
dispatchMode = "manual"
}
if !slices.Contains(allowedDispatchModes, dispatchMode) {
logx.Errorf("addPlayerServices contains err: invalid dispatchMode %v", in.DispatchMode)
return nil, errors.New("invalid dispatchMode")
}
announcements := schema.TextArray{Elements: in.Announcements, Valid: true}
if announcements.Elements == nil {
announcements.Elements = []string{}
}
_, err = l.svcCtx.ShopModelRW.Shops.Create().
SetID(idResp.Id).
SetOwnerID(in.OwnerId).
SetOwnerID(idResp.Id).
SetName(in.Name).
SetBanner(in.Banner).
SetDescription(in.Description).
SetRating(rating).
SetTotalOrders(int(in.TotalOrders)).
SetPlayerCount(int(in.PlayerCount)).
SetNillableCommissionType(&in.CommissionType).
SetCommissionType(commissionType).
SetCommissionValue(commissionValue).
SetAllowMultiShop(in.AllowMultiShop).
SetAllowIndependentOrders(in.AllowIndependentOrders).
SetDispatchMode(in.DispatchMode).
SetAnnouncements(in.Announcements).
SetDispatchMode(dispatchMode).
SetAnnouncements(announcements).
SetTemplateConfig(templateConfig).
Save(l.ctx)
@@ -73,5 +119,6 @@ func (l *AddShopsLogic) AddShops(in *pb.AddShopsReq) (*pb.AddShopsResp, error) {
logx.Errorf("addPlayerServices err:%v", err)
return nil, errors.New("add player service failed")
}
logx.Debugf("shop created with id: %v", idResp.Id)
return &pb.AddShopsResp{}, nil
}
@@ -51,7 +51,7 @@ func (l *GetShopsByIdLogic) GetShopsById(in *pb.GetShopsByIdReq) (*pb.GetShopsBy
AllowMultiShop: shop.AllowMultiShop,
AllowIndependentOrders: shop.AllowIndependentOrders,
DispatchMode: shop.DispatchMode,
Announcements: shop.Announcements,
Announcements: shop.Announcements.Elements,
TemplateConfig: string(templateConfigBytes),
CreatedAt: shop.CreatedAt.Unix(),
UpdatedAt: shop.UpdatedAt.Unix(),
@@ -124,7 +124,7 @@ func (l *SearchShopsLogic) SearchShops(in *pb.SearchShopsReq) (*pb.SearchShopsRe
AllowMultiShop: item.AllowMultiShop,
AllowIndependentOrders: item.AllowIndependentOrders,
DispatchMode: item.DispatchMode,
Announcements: item.Announcements,
Announcements: item.Announcements.Elements,
TemplateConfig: string(templateConfigBytes),
CreatedAt: item.CreatedAt.Unix(),
UpdatedAt: item.UpdatedAt.Unix(),
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"juwan-backend/app/shop/rpc/internal/models/schema"
"juwan-backend/app/shop/rpc/internal/models/shops"
"time"
@@ -75,7 +76,7 @@ func (l *UpdateShopsLogic) UpdateShops(in *pb.UpdateShopsReq) (*pb.UpdateShopsRe
SetAllowIndependentOrders(in.AllowIndependentOrders)
if len(in.Announcements) > 0 {
updater = updater.SetAnnouncements(in.Announcements)
updater = updater.SetAnnouncements(schema.TextArray{Elements: in.Announcements, Valid: true})
}
if in.TemplateConfig != "" {