droboticflow/internal/api/account_hierarchy.go

113 lines
3.8 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"net/http"
)
func (s *server) validateAccountSupplier(ctx context.Context, org, id string, role domain.UserRole) (*validationIssue, error) {
if role == domain.UserRoleAdmin {
return nil, nil
}
return s.validateUserSupplier(ctx, org, id)
}
func (s *server) ensureRoleChangeIdle(w http.ResponseWriter, r *http.Request, user domain.UserRecord) bool {
pending, err := s.store.AccountHasPendingWork(r.Context(), user.OrganizationID, user.ID, user.Role)
if err != nil {
renderError(w, http.StatusInternalServerError, err)
return false
}
if pending {
renderJSON(w, http.StatusConflict, map[string]string{"code": "account_has_pending_tasks", "error": "该账号有未完成任务,请先提交或释放任务后再修改角色"})
return false
}
return true
}
type accountHierarchyIssue struct {
UserID string `json:"userId"`
Phone string `json:"phone"`
Role domain.UserRole `json:"role"`
SupplierID string `json:"supplierId"`
ProjectID string `json:"projectId,omitempty"`
Reason string `json:"reason"`
StoredRoles []domain.AnnotationAssignmentRole `json:"storedRoles,omitempty"`
PendingTasks int `json:"pendingTasks"`
}
// Read-only inventory: no automatic backfill or changes to task history.
func (s *server) handleAccountHierarchyAudit(w http.ResponseWriter, r *http.Request) {
principal, _ := currentPrincipal(r)
if !isOrganizationAdmin(principal) {
renderJSON(w, 403, map[string]string{"error": "仅管理员可检查历史账号数据"})
return
}
org := s.requestOrganizationID(r)
users, err := s.store.ListUsersByOrganization(r.Context(), org)
if err != nil {
renderError(w, 500, err)
return
}
members, err := s.store.ListProjectMembers(r.Context(), org, "")
if err != nil {
renderError(w, 500, err)
return
}
items, err := s.store.ListHumanItems(r.Context(), org, domain.HumanItemQuery{})
if err != nil {
renderError(w, 500, err)
return
}
suppliers, err := s.store.ListSuppliers(r.Context(), org)
if err != nil {
renderError(w, 500, err)
return
}
valid := map[string]bool{}
for _, v := range suppliers {
valid[v.ID] = v.Status == "active"
}
pending := map[string]int{}
for _, v := range items {
if v.AssignedUserID != "" && humanItemPending(v.Status) {
pending[v.AssignedUserID]++
}
}
directory := map[string]domain.UserRecord{}
issues := []accountHierarchyIssue{}
add := func(u domain.UserRecord, project, reason string, roles []domain.AnnotationAssignmentRole) {
issues = append(issues, accountHierarchyIssue{u.ID, u.Phone, u.Role, u.SupplierID, project, reason, roles, pending[u.ID]})
}
for _, u := range users {
directory[u.ID] = u
if u.IsSuperAdmin {
continue
}
if u.Role == domain.UserRoleAdmin && u.SupplierID != "" {
add(u, "", "管理员仍绑定供应商", nil)
}
if u.Role != domain.UserRoleAdmin && !valid[u.SupplierID] {
add(u, "", "账号未绑定有效供应商", nil)
}
if len(u.DefaultProjectRoles) > 0 {
add(u, "", "存在旧默认项目角色配置", u.DefaultProjectRoles)
}
}
for _, m := range members {
u, ok := directory[m.UserID]
if !ok {
add(domain.UserRecord{ID: m.UserID}, m.ProjectID, "成员账号缺失", m.Roles)
continue
}
expected := domain.AccountProductionRoles(u)
if len(expected) != 1 || len(m.Roles) != 1 || (len(expected) == 1 && m.Roles[0] != expected[0]) {
add(u, m.ProjectID, "历史项目角色与账号角色不一致", m.Roles)
}
if m.SupplierID != u.SupplierID {
add(u, m.ProjectID, "成员供应商与账号归属不一致", m.Roles)
}
}
renderJSON(w, 200, map[string]any{"items": issues})
}