diff --git a/app/shop/api/etc/shop-api.yaml b/app/shop/api/etc/shop-api.yaml index 6ecd4ff..fabc5b1 100644 --- a/app/shop/api/etc/shop-api.yaml +++ b/app/shop/api/etc/shop-api.yaml @@ -17,4 +17,7 @@ Prometheus: # ===== DEV CONFIG ==== ShopRpcConf: Endpoints: - - shop-rpc:8080 \ No newline at end of file + - shop-rpc:8080 +PlayerRpcConf: + Endpoints: + - player-rpc:8080 diff --git a/app/shop/api/internal/config/config.go b/app/shop/api/internal/config/config.go index b15f22d..cda177e 100644 --- a/app/shop/api/internal/config/config.go +++ b/app/shop/api/internal/config/config.go @@ -10,5 +10,6 @@ import ( type Config struct { rest.RestConf - ShopRpcConf zrpc.RpcClientConf + ShopRpcConf zrpc.RpcClientConf + PlayerRpcConf zrpc.RpcClientConf } diff --git a/app/shop/api/internal/logic/shop/acceptInvitationLogic.go b/app/shop/api/internal/logic/shop/acceptInvitationLogic.go index 35d9395..0ca869d 100644 --- a/app/shop/api/internal/logic/shop/acceptInvitationLogic.go +++ b/app/shop/api/internal/logic/shop/acceptInvitationLogic.go @@ -6,6 +6,7 @@ package shop import ( "context" "errors" + "juwan-backend/app/player/rpc/playerservice" "juwan-backend/app/shop/api/internal/svc" "juwan-backend/app/shop/api/internal/types" "juwan-backend/app/shop/rpc/pb" @@ -13,6 +14,8 @@ import ( "time" "github.com/zeromicro/go-zero/core/logx" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) type AcceptInvitationLogic struct { @@ -35,6 +38,18 @@ func (l *AcceptInvitationLogic) AcceptInvitation(req *types.AcceptInvitationReq) if err != nil { return nil, err } + playerResp, err := l.svcCtx.PlayerRpc.GetPlayerByUserId(l.ctx, &playerservice.SearchPlayersReq{UserId: &userId}) + if err != nil { + st, _ := status.FromError(err) + if st.Code() == codes.NotFound { + return nil, errors.New("bad request: invitation not found or not owned by user") + } + return nil, err + } + player := playerResp.GetPlayers() + if player == nil { + return nil, errors.New("bad request: invitation not found or not owned by user") + } invitation, err := l.svcCtx.ShopRpc.GetShopInvitationsById(l.ctx, &pb.GetShopInvitationsByIdReq{Id: req.Id}) if err != nil { return nil, err @@ -42,14 +57,14 @@ func (l *AcceptInvitationLogic) AcceptInvitation(req *types.AcceptInvitationReq) if invitation.ShopInvitations == nil { return nil, errors.New("invitation not found") } - if invitation.ShopInvitations.PlayerId != userId { + if invitation.ShopInvitations.PlayerId != player.Id { return nil, errors.New("bad request: invitation not found or not owned by user") } i, err := l.svcCtx.ShopRpc.UpdateShopInvitations(l.ctx, &pb.UpdateShopInvitationsReq{ Id: req.Id, ShopId: invitation.ShopInvitations.ShopId, - PlayerId: userId, + PlayerId: player.Id, Status: "accepted", InvitedBy: invitation.ShopInvitations.InvitedBy, CreatedAt: invitation.ShopInvitations.CreatedAt, @@ -60,7 +75,7 @@ func (l *AcceptInvitationLogic) AcceptInvitation(req *types.AcceptInvitationReq) } _, err = l.svcCtx.ShopRpc.AddShopPlayers(l.ctx, &pb.AddShopPlayersReq{ ShopId: i.ShopInvitations.ShopId, - PlayerId: userId, + PlayerId: player.Id, IsPrimary: false, JoinedAt: time.Now().Unix(), LeftAt: 0, diff --git a/app/shop/api/internal/logic/shop/rejectInvitationLogic.go b/app/shop/api/internal/logic/shop/rejectInvitationLogic.go index ea9fe1e..5ca1304 100644 --- a/app/shop/api/internal/logic/shop/rejectInvitationLogic.go +++ b/app/shop/api/internal/logic/shop/rejectInvitationLogic.go @@ -8,12 +8,15 @@ import ( "errors" "time" + "juwan-backend/app/player/rpc/playerservice" "juwan-backend/app/shop/api/internal/svc" "juwan-backend/app/shop/api/internal/types" "juwan-backend/app/shop/rpc/pb" "juwan-backend/common/utils/contextj" "github.com/zeromicro/go-zero/core/logx" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) type RejectInvitationLogic struct { @@ -36,6 +39,18 @@ func (l *RejectInvitationLogic) RejectInvitation(req *types.AcceptInvitationReq) if err != nil { return nil, err } + playerResp, err := l.svcCtx.PlayerRpc.GetPlayerByUserId(l.ctx, &playerservice.SearchPlayersReq{UserId: &userID}) + if err != nil { + st, _ := status.FromError(err) + if st.Code() == codes.NotFound { + return nil, contextj.ERRILLEGALUSER + } + return nil, err + } + player := playerResp.GetPlayers() + if player == nil { + return nil, contextj.ERRILLEGALUSER + } invitation, err := l.svcCtx.ShopRpc.GetShopInvitationsById(l.ctx, &pb.GetShopInvitationsByIdReq{Id: req.Id}) if err != nil { @@ -44,7 +59,7 @@ func (l *RejectInvitationLogic) RejectInvitation(req *types.AcceptInvitationReq) if invitation.ShopInvitations == nil { return nil, errors.New("invitation not found") } - if invitation.ShopInvitations.PlayerId != userID { + if invitation.ShopInvitations.PlayerId != player.Id { return nil, contextj.ERRILLEGALUSER } diff --git a/app/shop/api/internal/svc/serviceContext.go b/app/shop/api/internal/svc/serviceContext.go index c8a407e..fffc76a 100644 --- a/app/shop/api/internal/svc/serviceContext.go +++ b/app/shop/api/internal/svc/serviceContext.go @@ -4,6 +4,7 @@ package svc import ( + "juwan-backend/app/player/rpc/playerservice" "juwan-backend/app/shop/api/internal/config" "juwan-backend/app/shop/rpc/shopservice" "juwan-backend/common/middlewares" @@ -15,6 +16,7 @@ import ( type ServiceContext struct { Config config.Config ShopRpc shopservice.ShopService + PlayerRpc playerservice.PlayerService HeaderExtractorMiddleware rest.Middleware } @@ -22,6 +24,7 @@ func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ Config: c, ShopRpc: shopservice.NewShopService(zrpc.MustNewClient(c.ShopRpcConf)), + PlayerRpc: playerservice.NewPlayerService(zrpc.MustNewClient(c.PlayerRpcConf)), HeaderExtractorMiddleware: middlewares.NewHeaderExtractorMiddleware().Handle, } }