Files
digital-psychology/apps/api/internal/service/relation/service.go
T
jackyu66gitandCursor bd22d9dddd feat: P1 合盘/星座/问答与测测完整设计包及模拟器取证工具
落地 synastry/star/ask API 与 H5 页面,补齐 cece-frontend-re complete-design 证据文档,并加入 Android 模拟器截图抓取脚本。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-03 11:37:53 +08:00

65 lines
1.9 KiB
Go

package relation
import (
"context"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/yuxingu/digital-psychology/apps/api/internal/model"
releng "github.com/yuxingu/digital-psychology/apps/api/internal/relation"
"github.com/yuxingu/digital-psychology/apps/api/internal/repository"
)
// Service creates relation understanding reports.
type Service struct {
Profiles *repository.ProfileRepo
Reports *repository.ReportRepo
Relations *repository.RelationRepo
}
// CreateInsightResult is API payload.
type CreateInsightResult struct {
Insight *model.RelationInsight `json:"insight"`
Report *model.GrowthReport `json:"report"`
}
// Create builds 关系理解 for two owned profiles.
func (s *Service) Create(ctx context.Context, userID, aID, bID uuid.UUID) (*CreateInsightResult, error) {
if aID == bID {
return nil, errors.New("need two different profiles")
}
pa, err := s.Profiles.GetForUser(ctx, userID, aID)
if err != nil {
return nil, errors.New("profile_a not found")
}
pb, err := s.Profiles.GetForUser(ctx, userID, bID)
if err != nil {
return nil, errors.New("profile_b not found")
}
relType := ""
if pb.RelationType != nil {
relType = *pb.RelationType
}
out := releng.BuildFull(pa.BirthDate, pb.BirthDate, pa.BirthTime, pb.BirthTime, pa.BirthPlace, pb.BirthPlace, pa.DisplayName, pb.DisplayName, relType)
sum, _ := json.Marshal(out.Summary)
det, _ := json.Marshal(out.Detail)
rep, err := s.Reports.Create(ctx, userID, aID, "relation", sum, det)
if err != nil {
return nil, err
}
ins, err := s.Relations.Create(ctx, userID, aID, bID, sum, rep.ID)
if err != nil {
return nil, err
}
// entitlement trim on report
deep, _ := s.Reports.HasDeepAccess(ctx, userID, rep.ID)
vip, _ := s.Reports.HasActiveMembership(ctx, userID)
rep.HasDeep = deep || vip
if !rep.HasDeep {
rep.Detail = nil
}
return &CreateInsightResult{Insight: ins, Report: rep}, nil
}