Files
digital-psychology/apps/api/internal/service/relation/service.go
T
jackyu66gitandCursor 14b836f53b feat: add RelationInsight API and wire H5 relation/profile pages
Second P1 growth engine: compare two profiles, gate full tips behind
deep-access mock payment, and list personal archives on /profile.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-02 16:28:10 +08:00

61 lines
1.7 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")
}
out := releng.Build(pa.BirthDate, pb.BirthDate, pa.DisplayName, pb.DisplayName)
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
}