query-database/api/internal/handlers/onboarding.go
2026-03-25 15:46:20 +08:00

75 lines
1.9 KiB
Go

package handlers
import (
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"query-database/api/internal/auth"
)
type onboardingReq struct {
Name string `json:"name"`
ModuleKey string `json:"moduleKey"`
ExperienceLevel string `json:"experienceLevel"`
DBMode string `json:"dbMode"`
MockDBKey *string `json:"mockDbKey"`
}
func (h *Handlers) CompleteOnboarding(c *gin.Context) {
userID := auth.UserID(c)
var req onboardingReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "参数错误"})
return
}
name := strings.TrimSpace(req.Name)
if name == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "请填写姓名"})
return
}
if req.ModuleKey != "shop" && req.ModuleKey != "hr" {
c.JSON(http.StatusBadRequest, gin.H{"message": "模块不合法"})
return
}
if req.ExperienceLevel != "beginner" && req.ExperienceLevel != "normal" && req.ExperienceLevel != "advanced" {
c.JSON(http.StatusBadRequest, gin.H{"message": "经验不合法"})
return
}
if req.DBMode != "mock" && req.DBMode != "import" {
c.JSON(http.StatusBadRequest, gin.H{"message": "数据库模式不合法"})
return
}
_, err := h.sqlite.Exec(
`UPDATE users SET name = ?, module_key = ?, experience_level = ?, onboarding_completed = 1 WHERE id = ?`,
name,
req.ModuleKey,
req.ExperienceLevel,
userID,
)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "服务异常"})
return
}
if req.DBMode == "mock" {
key := req.ModuleKey
if req.MockDBKey != nil {
key = *req.MockDBKey
}
if key != "shop" && key != "hr" {
c.JSON(http.StatusBadRequest, gin.H{"message": "模拟库不合法"})
return
}
if err := h.activateMockForUser(userID, key, time.Now().UTC().Format(time.RFC3339)); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "设置数据库失败"})
return
}
}
c.Status(http.StatusNoContent)
}