批次生成/作废、C 端兑码延长会员;admin-h5 /codes。 Loop continuous。Next:ECR-016 UserIntelligence。 Co-authored-by: Cursor <cursoragent@cursor.com>
353 lines
9.8 KiB
Go
353 lines
9.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"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/membership"
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/service/report"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
// ReportHandler exposes portrait reports and commerce mock.
|
|
type ReportHandler struct {
|
|
Svc *report.Service
|
|
Membership *membership.Service
|
|
}
|
|
|
|
func (h *ReportHandler) requireMembership(c *gin.Context) (*membership.Service, bool) {
|
|
if h.Membership == nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, "membership service unavailable")
|
|
return nil, false
|
|
}
|
|
return h.Membership, true
|
|
}
|
|
|
|
// Register mounts report/commerce routes.
|
|
func (h *ReportHandler) Register(rg *gin.RouterGroup) {
|
|
rg.POST("/reports/portrait", h.CreatePortrait)
|
|
rg.POST("/reports/star", h.CreateStar)
|
|
rg.POST("/reports/synastry", h.CreateSynastry)
|
|
rg.POST("/reports/rhythm", h.CreateRhythm)
|
|
rg.GET("/reports", h.List)
|
|
rg.GET("/reports/latest", h.GetLatest)
|
|
rg.GET("/reports/:id", h.Get)
|
|
rg.GET("/membership/me", h.GetMembership)
|
|
rg.POST("/membership/redeem", h.RedeemCode)
|
|
rg.POST("/orders", h.CreateOrder)
|
|
rg.POST("/orders/:id/pay-mock", h.PayMock)
|
|
}
|
|
|
|
// GetLatest handles GET /reports/latest?profile_id=&type=&peer_profile_id=.
|
|
func (h *ReportHandler) GetLatest(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
pid, err := uuid.Parse(c.Query("profile_id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "profile_id required")
|
|
return
|
|
}
|
|
typ := c.Query("type")
|
|
if typ == "" {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "type required")
|
|
return
|
|
}
|
|
var peer *uuid.UUID
|
|
if ps := c.Query("peer_profile_id"); ps != "" {
|
|
id, err := uuid.Parse(ps)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid peer_profile_id")
|
|
return
|
|
}
|
|
peer = &id
|
|
}
|
|
rep, err := h.Svc.GetLatest(c.Request.Context(), userID, pid, typ, peer)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusNotFound, 40402, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreatePortrait handles POST /reports/portrait.
|
|
func (h *ReportHandler) CreatePortrait(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
pid, err := uuid.Parse(req.ProfileID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.CreatePortrait(c.Request.Context(), userID, pid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreateStar handles POST /reports/star (星象性格).
|
|
func (h *ReportHandler) CreateStar(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
pid, err := uuid.Parse(req.ProfileID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.CreateStar(c.Request.Context(), userID, pid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreateSynastry handles POST /reports/synastry (五主盘 + 推运合盘).
|
|
func (h *ReportHandler) CreateSynastry(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileIDA string `json:"profile_id_a" binding:"required"`
|
|
ProfileIDB string `json:"profile_id_b" binding:"required"`
|
|
AsOf *string `json:"as_of"` // YYYY-MM-DD optional
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
aid, err := uuid.Parse(req.ProfileIDA)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id_a")
|
|
return
|
|
}
|
|
bid, err := uuid.Parse(req.ProfileIDB)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id_b")
|
|
return
|
|
}
|
|
if aid == bid {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "需要两个不同档案")
|
|
return
|
|
}
|
|
var asOfPtr *time.Time
|
|
if req.AsOf != nil && *req.AsOf != "" {
|
|
t, err := time.ParseInLocation("2006-01-02", *req.AsOf, time.FixedZone("CST", 8*3600))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid as_of")
|
|
return
|
|
}
|
|
asOfPtr = &t
|
|
}
|
|
rep, err := h.Svc.CreateSynastry(c.Request.Context(), userID, aid, bid, asOfPtr)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreateRhythm handles POST /reports/rhythm (身心节律) — wired when service ready.
|
|
func (h *ReportHandler) CreateRhythm(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
pid, err := uuid.Parse(req.ProfileID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.CreateRhythm(c.Request.Context(), userID, pid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// List handles GET /reports.
|
|
func (h *ReportHandler) List(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
items, err := h.Svc.List(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
// Get handles GET /reports/:id.
|
|
func (h *ReportHandler) Get(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
rid, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.Get(c.Request.Context(), userID, rid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusNotFound, 40401, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// GetMembership handles GET /membership/me.
|
|
func (h *ReportHandler) GetMembership(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
msvc, ok := h.requireMembership(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
me, err := msvc.Get(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, me)
|
|
}
|
|
|
|
// RedeemCode handles POST /membership/redeem.
|
|
func (h *ReportHandler) RedeemCode(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
msvc, ok := h.requireMembership(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var body struct {
|
|
Code string `json:"code"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil || body.Code == "" {
|
|
response.Fail(c, http.StatusBadRequest, 40000, "code required")
|
|
return
|
|
}
|
|
plan, err := msvc.Redeem(c.Request.Context(), userID, body.Code)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40000, err.Error())
|
|
return
|
|
}
|
|
me, err := msvc.Get(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"plan": plan, "membership": me})
|
|
}
|
|
|
|
// CreateOrder handles POST /orders.
|
|
func (h *ReportHandler) CreateOrder(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
msvc, ok := h.requireMembership(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req struct {
|
|
Kind string `json:"kind" binding:"required"`
|
|
Plan string `json:"plan"`
|
|
ReportID *string `json:"report_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
var rid *uuid.UUID
|
|
if req.ReportID != nil && *req.ReportID != "" {
|
|
id, err := uuid.Parse(*req.ReportID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid report_id")
|
|
return
|
|
}
|
|
rid = &id
|
|
}
|
|
oid, err := msvc.CreateOrder(c.Request.Context(), userID, membership.CreateOrderInput{
|
|
Kind: req.Kind, Plan: req.Plan, ReportID: rid,
|
|
})
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30003, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"order_id": oid.String()})
|
|
}
|
|
|
|
// PayMock handles POST /orders/:id/pay-mock.
|
|
func (h *ReportHandler) PayMock(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
msvc, ok := h.requireMembership(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
oid, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
|
|
return
|
|
}
|
|
if err := msvc.PayMock(c.Request.Context(), userID, oid); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30004, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"paid": true})
|
|
}
|