落地埋点 ingest/数据看板、首页宫格 CMS 与测评上下架;含账号引导、问答流式与免责声明去重,以及 review P1 审计同事务修复。 Co-authored-by: Cursor <cursoragent@cursor.com>
182 lines
4.9 KiB
Go
182 lines
4.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// ScaleListItem is a published 探索测试.
|
|
type ScaleListItem struct {
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
// ScaleQuestion is one item in a scale.
|
|
type ScaleQuestion struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Sort int `json:"sort"`
|
|
Body json.RawMessage `json:"body"`
|
|
}
|
|
|
|
// ScaleDetail is scale + questions.
|
|
type ScaleDetail struct {
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
Questions []ScaleQuestion `json:"questions"`
|
|
}
|
|
|
|
// ScaleResultRow stored result.
|
|
type ScaleResultRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
ScaleSlug string `json:"scale_slug"`
|
|
Result json.RawMessage `json:"result"`
|
|
}
|
|
|
|
// ScaleRepo loads scales and results.
|
|
type ScaleRepo struct {
|
|
Pool *pgxpool.Pool
|
|
}
|
|
|
|
// ListPublished returns published scales.
|
|
func (r *ScaleRepo) ListPublished(ctx context.Context) ([]ScaleListItem, error) {
|
|
rows, err := r.Pool.Query(ctx, `
|
|
SELECT slug, title, description FROM scales
|
|
WHERE status='published' AND deleted_at IS NULL ORDER BY created_at`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []ScaleListItem
|
|
for rows.Next() {
|
|
var it ScaleListItem
|
|
if err := rows.Scan(&it.Slug, &it.Title, &it.Description); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, it)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// GetBySlug loads a published scale with questions.
|
|
func (r *ScaleRepo) GetBySlug(ctx context.Context, slug string) (*ScaleDetail, error) {
|
|
d := &ScaleDetail{Slug: slug}
|
|
var scaleID uuid.UUID
|
|
err := r.Pool.QueryRow(ctx, `
|
|
SELECT id, title, description FROM scales
|
|
WHERE slug=$1 AND status='published' AND deleted_at IS NULL`, slug,
|
|
).Scan(&scaleID, &d.Title, &d.Description)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := r.Pool.Query(ctx, `
|
|
SELECT id, sort, body FROM scale_questions
|
|
WHERE scale_id=$1 AND deleted_at IS NULL ORDER BY sort`, scaleID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var q ScaleQuestion
|
|
if err := rows.Scan(&q.ID, &q.Sort, &q.Body); err != nil {
|
|
return nil, err
|
|
}
|
|
d.Questions = append(d.Questions, q)
|
|
}
|
|
return d, rows.Err()
|
|
}
|
|
|
|
// ScaleAdminItem is a scale row for ops.
|
|
type ScaleAdminItem struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// ListAllAdmin returns all non-deleted scales.
|
|
func (r *ScaleRepo) ListAllAdmin(ctx context.Context) ([]ScaleAdminItem, error) {
|
|
rows, err := r.Pool.Query(ctx, `
|
|
SELECT id, slug, title, description, status FROM scales
|
|
WHERE deleted_at IS NULL ORDER BY created_at`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []ScaleAdminItem
|
|
for rows.Next() {
|
|
var it ScaleAdminItem
|
|
if err := rows.Scan(&it.ID, &it.Slug, &it.Title, &it.Description, &it.Status); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, it)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// UpdateStatus sets published|draft.
|
|
func (r *ScaleRepo) UpdateStatus(ctx context.Context, id uuid.UUID, status string) error {
|
|
return r.UpdateStatusWithAudit(ctx, id, status, uuid.Nil, nil)
|
|
}
|
|
|
|
// UpdateStatusWithAudit updates status and optionally writes audit in one tx.
|
|
func (r *ScaleRepo) UpdateStatusWithAudit(
|
|
ctx context.Context,
|
|
id uuid.UUID,
|
|
status string,
|
|
adminID uuid.UUID,
|
|
meta json.RawMessage,
|
|
) error {
|
|
tx, err := r.Pool.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
tag, err := tx.Exec(ctx, `
|
|
UPDATE scales SET status=$2, updated_at=now()
|
|
WHERE id=$1 AND deleted_at IS NULL`, id, status)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return pgx.ErrNoRows
|
|
}
|
|
if adminID != uuid.Nil {
|
|
if meta == nil {
|
|
meta = json.RawMessage(`{}`)
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO admin_audit_logs(admin_id, action, target_type, target_id, meta)
|
|
VALUES ($1,'scale.status','scale',$2,$3)`, adminID, id.String(), meta); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
// SaveResult stores scoring output.
|
|
func (r *ScaleRepo) SaveResult(ctx context.Context, userID, scaleID, profileID uuid.UUID, answers, result json.RawMessage) (uuid.UUID, error) {
|
|
var id uuid.UUID
|
|
err := r.Pool.QueryRow(ctx, `
|
|
INSERT INTO scale_results(user_id, scale_id, profile_id, answers, result)
|
|
VALUES ($1,$2,$3,$4,$5) RETURNING id`,
|
|
userID, scaleID, profileID, answers, result,
|
|
).Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
// ScaleIDBySlug resolves id for a published scale.
|
|
func (r *ScaleRepo) ScaleIDBySlug(ctx context.Context, slug string) (uuid.UUID, error) {
|
|
var id uuid.UUID
|
|
err := r.Pool.QueryRow(ctx, `
|
|
SELECT id FROM scales
|
|
WHERE slug=$1 AND status='published' AND deleted_at IS NULL`, slug).Scan(&id)
|
|
return id, err
|
|
}
|