From e8084e9d2c5c255054682e5322fbbcffbd97e153 Mon Sep 17 00:00:00 2001 From: Tension Date: Mon, 29 Dec 2025 13:50:50 +0800 Subject: [PATCH] feat(subscribe): rename stop user subscribe handler to toggle and update logic for status change --- apis/admin/user.api | 6 ++--- ...go => toggleUserSubscribeStatusHandler.go} | 8 +++---- internal/handler/routes.go | 2 +- ...c.go => toggleUserSubscribeStatusLogic.go} | 22 ++++++++++++++----- internal/types/types.go | 8 +++---- 5 files changed, 28 insertions(+), 18 deletions(-) rename internal/handler/admin/user/{stopUserSubscribeHandler.go => toggleUserSubscribeStatusHandler.go} (66%) rename internal/logic/admin/user/{stopUserSubscribeLogic.go => toggleUserSubscribeStatusLogic.go} (67%) diff --git a/apis/admin/user.api b/apis/admin/user.api index 1f01a6f..923f197 100644 --- a/apis/admin/user.api +++ b/apis/admin/user.api @@ -184,7 +184,7 @@ type ( GetUserSubscribeByIdRequest { Id int64 `form:"id" validate:"required"` } - StopUserSubscribeRequest { + ToggleUserSubscribeStatusRequest { UserSubscribeId int64 `json:"user_subscribe_id"` } ResetUserSubscribeTrafficRequest { @@ -304,8 +304,8 @@ service ppanel { post /subscribe/reset/token (ResetUserSubscribeTokenRequest) @doc "Stop user subscribe" - @handler StopUserSubscribe - post /subscribe/stop (StopUserSubscribeRequest) + @handler ToggleUserSubscribeStatus + post /subscribe/toggle (ToggleUserSubscribeStatusRequest) @doc "Reset user subscribe traffic" @handler ResetUserSubscribeTraffic diff --git a/internal/handler/admin/user/stopUserSubscribeHandler.go b/internal/handler/admin/user/toggleUserSubscribeStatusHandler.go similarity index 66% rename from internal/handler/admin/user/stopUserSubscribeHandler.go rename to internal/handler/admin/user/toggleUserSubscribeStatusHandler.go index d61f04a..5883e99 100644 --- a/internal/handler/admin/user/stopUserSubscribeHandler.go +++ b/internal/handler/admin/user/toggleUserSubscribeStatusHandler.go @@ -9,9 +9,9 @@ import ( ) // Stop user subscribe -func StopUserSubscribeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { +func ToggleUserSubscribeStatusHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return func(c *gin.Context) { - var req types.StopUserSubscribeRequest + var req types.ToggleUserSubscribeStatusRequest _ = c.ShouldBind(&req) validateErr := svcCtx.Validate(&req) if validateErr != nil { @@ -19,8 +19,8 @@ func StopUserSubscribeHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) { return } - l := user.NewStopUserSubscribeLogic(c.Request.Context(), svcCtx) - err := l.StopUserSubscribe(&req) + l := user.NewToggleUserSubscribeStatusLogic(c.Request.Context(), svcCtx) + err := l.ToggleUserSubscribeStatus(&req) result.HttpResult(c, nil, err) } } diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 2d41010..6a942a7 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -583,7 +583,7 @@ func RegisterHandlers(router *gin.Engine, serverCtx *svc.ServiceContext) { adminUserGroupRouter.POST("/subscribe/reset/traffic", adminUser.ResetUserSubscribeTrafficHandler(serverCtx)) // Stop user subscribe - adminUserGroupRouter.POST("/subscribe/stop", adminUser.StopUserSubscribeHandler(serverCtx)) + adminUserGroupRouter.POST("/subscribe/toggle", adminUser.ToggleUserSubscribeStatusHandler(serverCtx)) // Get user subcribe traffic logs adminUserGroupRouter.GET("/subscribe/traffic_logs", adminUser.GetUserSubscribeTrafficLogsHandler(serverCtx)) diff --git a/internal/logic/admin/user/stopUserSubscribeLogic.go b/internal/logic/admin/user/toggleUserSubscribeStatusLogic.go similarity index 67% rename from internal/logic/admin/user/stopUserSubscribeLogic.go rename to internal/logic/admin/user/toggleUserSubscribeStatusLogic.go index 658e4cf..5f06e98 100644 --- a/internal/logic/admin/user/stopUserSubscribeLogic.go +++ b/internal/logic/admin/user/toggleUserSubscribeStatusLogic.go @@ -10,28 +10,38 @@ import ( "github.com/pkg/errors" ) -type StopUserSubscribeLogic struct { +type ToggleUserSubscribeStatusLogic struct { logger.Logger ctx context.Context svcCtx *svc.ServiceContext } -// NewStopUserSubscribeLogic Stop user subscribe -func NewStopUserSubscribeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StopUserSubscribeLogic { - return &StopUserSubscribeLogic{ +// NewToggleUserSubscribeStatusLogic Stop user subscribe +func NewToggleUserSubscribeStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ToggleUserSubscribeStatusLogic { + return &ToggleUserSubscribeStatusLogic{ Logger: logger.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } -func (l *StopUserSubscribeLogic) StopUserSubscribe(req *types.StopUserSubscribeRequest) error { +func (l *ToggleUserSubscribeStatusLogic) ToggleUserSubscribeStatus(req *types.ToggleUserSubscribeStatusRequest) error { userSub, err := l.svcCtx.UserModel.FindOneSubscribe(l.ctx, req.UserSubscribeId) if err != nil { l.Errorw("FindOneSubscribe error", logger.Field("error", err.Error()), logger.Field("userSubscribeId", req.UserSubscribeId)) return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), " FindOneSubscribe error: %v", err.Error()) } - userSub.Status = 5 // set status to stopped + + switch userSub.Status { + case 2: // active + userSub.Status = 5 // set status to stopped + case 5: // stopped + userSub.Status = 2 // set status to active + default: + l.Errorw("invalid user subscribe status", logger.Field("userSubscribeId", req.UserSubscribeId), logger.Field("status", userSub.Status)) + return errors.Wrapf(xerr.NewErrCodeMsg(xerr.ERROR, "invalid subscribe status"), "invalid user subscribe status: %d", userSub.Status) + } + err = l.svcCtx.UserModel.UpdateSubscribe(l.ctx, userSub) if err != nil { l.Errorw("UpdateSubscribe error", logger.Field("error", err.Error()), logger.Field("userSubscribeId", req.UserSubscribeId)) diff --git a/internal/types/types.go b/internal/types/types.go index 6717c18..97042bd 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -2044,10 +2044,6 @@ type StopBatchSendEmailTaskRequest struct { Id int64 `json:"id"` } -type StopUserSubscribeRequest struct { - UserSubscribeId int64 `json:"user_subscribe_id"` -} - type StripePayment struct { Method string `json:"method"` ClientSecret string `json:"client_secret"` @@ -2239,6 +2235,10 @@ type ToggleNodeStatusRequest struct { Enable *bool `json:"enable"` } +type ToggleUserSubscribeStatusRequest struct { + UserSubscribeId int64 `json:"user_subscribe_id"` +} + type TosConfig struct { TosContent string `json:"tos_content"` }