Files
digital-psychology/apps/api/internal/repository/star_config_write.go
T
jackyu66gitandCursor 77907f79ee feat(ECR-043): StarConfig 写面闭环并 Closed
加法权限 admin.explore.write、POST/PUT+审计、C端 GET /star/configs、
H5 无 active 空态/失败回退;migration 000052。ExploreConfig Loop STOP,禁自动 ECR-044。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 20:23:38 +08:00

140 lines
3.6 KiB
Go

package repository
import (
"context"
"encoding/json"
"errors"
"strings"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
// StarConfigWriteInput is create/update payload.
type StarConfigWriteInput struct {
Code string
Title string
Active bool
}
// ListActiveStarConfigs returns active configs for C-end.
func (r *AdminRepo) ListActiveStarConfigs(ctx context.Context) ([]StarConfigRow, error) {
rows, err := r.Pool.Query(ctx, `
SELECT id, code, title, active, system, updated_at
FROM star_configs
WHERE active = true
ORDER BY code ASC
LIMIT 100`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []StarConfigRow
for rows.Next() {
var row StarConfigRow
if err := rows.Scan(&row.ID, &row.Code, &row.Title, &row.Active, &row.System, &row.UpdatedAt); err != nil {
return nil, err
}
out = append(out, row)
}
return out, rows.Err()
}
// CreateStarConfigWithAudit inserts and audits.
func (r *AdminRepo) CreateStarConfigWithAudit(
ctx context.Context, adminID uuid.UUID, in StarConfigWriteInput, meta json.RawMessage,
) (*StarConfigRow, error) {
tx, err := r.Pool.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
var row StarConfigRow
err = tx.QueryRow(ctx, `
INSERT INTO star_configs(code, title, active, system)
VALUES ($1,$2,$3,false)
RETURNING id, code, title, active, system, updated_at`,
in.Code, in.Title, in.Active,
).Scan(&row.ID, &row.Code, &row.Title, &row.Active, &row.System, &row.UpdatedAt)
if err != nil {
return nil, mapStarConfigWriteErr(err)
}
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,'explore.star_config.create','star_config',$2,$3)`,
adminID, row.ID.String(), meta,
); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
return &row, nil
}
// UpdateStarConfigWithAudit updates and audits.
func (r *AdminRepo) UpdateStarConfigWithAudit(
ctx context.Context, adminID, id uuid.UUID, in StarConfigWriteInput, meta json.RawMessage,
) (*StarConfigRow, error) {
tx, err := r.Pool.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
var system bool
var oldCode string
err = tx.QueryRow(ctx, `SELECT system, code FROM star_configs WHERE id=$1`, id).Scan(&system, &oldCode)
if errors.Is(err, pgx.ErrNoRows) {
return nil, pgx.ErrNoRows
}
if err != nil {
return nil, err
}
code := in.Code
if system {
code = oldCode
}
var row StarConfigRow
err = tx.QueryRow(ctx, `
UPDATE star_configs
SET code=$2, title=$3, active=$4, updated_at=now()
WHERE id=$1
RETURNING id, code, title, active, system, updated_at`,
id, code, in.Title, in.Active,
).Scan(&row.ID, &row.Code, &row.Title, &row.Active, &row.System, &row.UpdatedAt)
if err != nil {
return nil, mapStarConfigWriteErr(err)
}
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,'explore.star_config.update','star_config',$2,$3)`,
adminID, id.String(), meta,
); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
return &row, nil
}
func mapStarConfigWriteErr(err error) error {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
return errString("star config code conflict")
}
return err
}
// StarConfigCodeConflict reports unique violation.
func StarConfigCodeConflict(err error) bool {
return err != nil && strings.Contains(err.Error(), "star config code conflict")
}