Files
digital-psychology/apps/api/internal/repository/entitlement_repo.go
T
jackyu66gitandCursor c81f57a7d1 feat(ECR-018): Entitlement 用户权益只读并 Closed
聚合 GET /admin/users/:id/entitlements(Membership∪DeepAccess∪问答额度)与 admin-h5 权益 Tab;无 migration / 无真支付。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 19:26:15 +08:00

53 lines
1.5 KiB
Go

package repository
import (
"context"
"time"
"github.com/google/uuid"
)
// DeepAccessBrief is one deep_access row for ops Entitlement.
type DeepAccessBrief struct {
ID uuid.UUID `json:"id"`
ReportID uuid.UUID `json:"report_id"`
ReportType string `json:"report_type,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// ListDeepAccessForUser returns recent deep accesses with report type.
func (r *AdminRepo) ListDeepAccessForUser(ctx context.Context, userID uuid.UUID, limit int) ([]DeepAccessBrief, error) {
if limit <= 0 || limit > 50 {
limit = 20
}
rows, err := r.Pool.Query(ctx, `
SELECT d.id, d.report_id, coalesce(g.type, ''), d.created_at
FROM deep_accesses d
LEFT JOIN growth_reports g ON g.id = d.report_id AND g.deleted_at IS NULL
WHERE d.user_id=$1 AND d.deleted_at IS NULL
ORDER BY d.created_at DESC
LIMIT $2`, userID, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var out []DeepAccessBrief
for rows.Next() {
var b DeepAccessBrief
if err := rows.Scan(&b.ID, &b.ReportID, &b.ReportType, &b.CreatedAt); err != nil {
return nil, err
}
out = append(out, b)
}
return out, rows.Err()
}
// CountDeepAccessForUser counts non-deleted deep accesses.
func (r *AdminRepo) CountDeepAccessForUser(ctx context.Context, userID uuid.UUID) (int, error) {
var n int
err := r.Pool.QueryRow(ctx, `
SELECT count(*)::int FROM deep_accesses
WHERE user_id=$1 AND deleted_at IS NULL`, userID).Scan(&n)
return n, err
}