落地管理员 RBAC/封禁/推送任务 stub,logout 解绑 device 并统一各页 ensureAccount,同时收口 P2 生日生成与状态文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package admin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/repository"
|
|
homesvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/home"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidScaleStatus = errors.New("invalid scale status")
|
|
ErrScaleNotFound = errors.New("scale not found")
|
|
)
|
|
|
|
// ListHomeTools returns all grid tools.
|
|
func (s *Service) ListHomeTools(ctx context.Context) ([]repository.HomeTool, error) {
|
|
if s.Home == nil {
|
|
return nil, errors.New("home unavailable")
|
|
}
|
|
return s.Home.ListAdmin(ctx)
|
|
}
|
|
|
|
// ReplaceHomeTools replaces grid and audits in one transaction.
|
|
func (s *Service) ReplaceHomeTools(ctx context.Context, adminID uuid.UUID, items []homesvc.ReplaceInput) error {
|
|
if err := s.RequireSuper(ctx, adminID); err != nil {
|
|
return err
|
|
}
|
|
if s.Home == nil {
|
|
return errors.New("home unavailable")
|
|
}
|
|
meta, _ := json.Marshal(map[string]any{"count": len(items)})
|
|
return s.Home.ReplaceWithAudit(ctx, adminID, items, meta)
|
|
}
|
|
|
|
// ListScalesAdmin returns all scales.
|
|
func (s *Service) ListScalesAdmin(ctx context.Context) ([]repository.ScaleAdminItem, error) {
|
|
if s.Scales == nil {
|
|
return nil, errors.New("scales unavailable")
|
|
}
|
|
return s.Scales.ListAllAdmin(ctx)
|
|
}
|
|
|
|
// PatchScaleStatus updates published|draft and audits in one transaction.
|
|
func (s *Service) PatchScaleStatus(ctx context.Context, adminID, scaleID uuid.UUID, status string) error {
|
|
if err := s.RequireSuper(ctx, adminID); err != nil {
|
|
return err
|
|
}
|
|
if status != "published" && status != "draft" {
|
|
return ErrInvalidScaleStatus
|
|
}
|
|
if s.Scales == nil {
|
|
return errors.New("scales unavailable")
|
|
}
|
|
meta, _ := json.Marshal(map[string]any{"status": status})
|
|
if err := s.Scales.UpdateStatusWithAudit(ctx, scaleID, status, adminID, meta); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrScaleNotFound
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|