feat(kol): expose kol_profile on /api/auth/me and add seed script

This commit is contained in:
2026-04-17 09:02:33 +08:00
parent 70725193eb
commit 40519545c6
5 changed files with 92 additions and 14 deletions
+65 -13
View File
@@ -13,13 +13,24 @@ import (
)
type AuthService struct {
repo repository.AuthRepository
jwt *auth.Manager
sessions *auth.SessionStore
repo repository.AuthRepository
kolProfiles repository.KolProfileRepository
jwt *auth.Manager
sessions *auth.SessionStore
}
func NewAuthService(repo repository.AuthRepository, jwt *auth.Manager, sessions *auth.SessionStore) *AuthService {
return &AuthService{repo: repo, jwt: jwt, sessions: sessions}
func NewAuthService(
repo repository.AuthRepository,
kolProfiles repository.KolProfileRepository,
jwt *auth.Manager,
sessions *auth.SessionStore,
) *AuthService {
return &AuthService{
repo: repo,
kolProfiles: kolProfiles,
jwt: jwt,
sessions: sessions,
}
}
type LoginRequest struct {
@@ -35,13 +46,21 @@ type LoginResponse struct {
}
type UserInfo struct {
ID int64 `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
AvatarURL *string `json:"avatar_url"`
TenantID int64 `json:"tenant_id"`
TenantRole string `json:"tenant_role"`
Permissions []string `json:"permissions"`
ID int64 `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
AvatarURL *string `json:"avatar_url"`
TenantID int64 `json:"tenant_id"`
TenantRole string `json:"tenant_role"`
Permissions []string `json:"permissions"`
KolProfile *KolProfileBrief `json:"kol_profile"`
}
type KolProfileBrief struct {
ID int64 `json:"id"`
DisplayName string `json:"display_name"`
Status string `json:"status"`
MarketEnabled bool `json:"market_enabled"`
}
func (s *AuthService) Login(ctx context.Context, req LoginRequest) (*LoginResponse, error) {
@@ -69,6 +88,11 @@ func (s *AuthService) Login(ctx context.Context, req LoginRequest) (*LoginRespon
return nil, fmt.Errorf("save refresh: %w", err)
}
kolProfile, err := s.loadKolProfileBrief(ctx, membership.TenantID, user.ID)
if err != nil {
return nil, err
}
return &LoginResponse{
AccessToken: pair.AccessToken,
RefreshToken: pair.RefreshToken,
@@ -81,6 +105,7 @@ func (s *AuthService) Login(ctx context.Context, req LoginRequest) (*LoginRespon
TenantID: membership.TenantID,
TenantRole: membership.TenantRole,
Permissions: auth.PermissionsForRole(membership.TenantRole),
KolProfile: kolProfile,
},
}, nil
}
@@ -137,14 +162,41 @@ func (s *AuthService) Me(ctx context.Context, actor auth.Actor) (*UserInfo, erro
return nil, response.ErrNotFound(40401, "user_not_found", "user not found")
}
kolProfile, err := s.loadKolProfileBrief(ctx, actor.TenantID, actor.UserID)
if err != nil {
return nil, err
}
return &UserInfo{
ID: actor.UserID,
ID: user.ID,
Email: user.Email,
Name: user.Name,
AvatarURL: user.AvatarURL,
TenantID: actor.TenantID,
TenantRole: actor.Role,
Permissions: auth.PermissionsForRole(actor.Role),
KolProfile: kolProfile,
}, nil
}
func (s *AuthService) loadKolProfileBrief(ctx context.Context, tenantID, userID int64) (*KolProfileBrief, error) {
if s.kolProfiles == nil {
return nil, nil
}
profile, err := s.kolProfiles.GetByUser(ctx, tenantID, userID)
if err != nil {
return nil, fmt.Errorf("get kol profile: %w", err)
}
if profile == nil || profile.Status != "active" {
return nil, nil
}
return &KolProfileBrief{
ID: profile.ID,
DisplayName: profile.DisplayName,
Status: profile.Status,
MarketEnabled: profile.MarketEnabled,
}, nil
}