UserStatus 迁移、DeviceAuth 拒绝非 active、admin-h5 CTA; Reviewer Closed。Human 授权 LOOP_AUTHORIZATION(免逐闸确认)。 Co-authored-by: Cursor <cursoragent@cursor.com>
79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/service/admin"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
func (h *AdminHandler) registerLifecycle(authed *gin.RouterGroup) {
|
|
authed.POST("/users/:id/status", middleware.RequireAdminPermission(h.Svc, admin.PermUsersStatusWrite), h.PostUserStatus)
|
|
authed.GET("/users/:id/status-transitions", middleware.RequireAdminPermission(h.Svc, admin.PermUsersRead), h.ListUserStatusTransitions)
|
|
}
|
|
|
|
func (h *AdminHandler) PostUserStatus(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40102, "admin session invalid")
|
|
return
|
|
}
|
|
userID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40000, "invalid id")
|
|
return
|
|
}
|
|
var body struct {
|
|
Status string `json:"status"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil || body.Status == "" {
|
|
response.Fail(c, http.StatusBadRequest, 40000, "status required")
|
|
return
|
|
}
|
|
err = h.Svc.TransitionUserStatus(c.Request.Context(), adminID, userID, body.Status, body.Reason)
|
|
if errors.Is(err, admin.ErrUserNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40400, "user not found")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrReasonRequired) || errors.Is(err, admin.ErrInvalidStatusEdge) {
|
|
response.Fail(c, http.StatusBadRequest, 40000, err.Error())
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
detail, err := h.Svc.GetUser(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, detail)
|
|
}
|
|
|
|
func (h *AdminHandler) ListUserStatusTransitions(c *gin.Context) {
|
|
userID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40000, "invalid id")
|
|
return
|
|
}
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50"))
|
|
items, err := h.Svc.ListStatusTransitions(c.Request.Context(), userID, limit)
|
|
if errors.Is(err, admin.ErrUserNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40400, "user not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|