feat: add administration domain core
This commit is contained in:
1 parent
065dcd955a
commit
cea2751dc5
9 files changed
+1135
No files matched your search
@@ -0,0 +1,82 @@
|
||||
package administration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Service) ListOrganizations(ctx context.Context, actor Actor) ([]OrganizationProjection, error) {
|
||||
include := actor.Role == RoleSuperAdmin
|
||||
if actor.Role != RoleSuperAdmin && actor.Role != RoleOrganizationAdmin {
|
||||
return nil, problem(ErrorForbidden, "需要管理员权限。")
|
||||
}
|
||||
organizations, err := s.store.ListOrganizations(ctx, include)
|
||||
if err != nil {
|
||||
return nil, infrastructure("list organizations", err)
|
||||
}
|
||||
out := make([]OrganizationProjection, 0, len(organizations))
|
||||
for _, org := range organizations {
|
||||
if actor.Role == RoleSuperAdmin || org.ID == actor.OrganizationID {
|
||||
out = append(out, ProjectOrganization(org))
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (s *Service) CreateOrganization(ctx context.Context, actor Actor, name string) (Organization, error) {
|
||||
if err := requireSuper(actor); err != nil {
|
||||
return Organization{}, err
|
||||
}
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" {
|
||||
return Organization{}, problem(ErrorValidation, "组织名称不能为空。")
|
||||
}
|
||||
now := s.now()
|
||||
id := s.id("org")
|
||||
org := Organization{ID: id, Name: name, Status: StatusActive, ArchiveOwnerID: "archive:" + id, CreatedAt: now, UpdatedAt: now}
|
||||
created, err := s.store.CreateOrganization(ctx, org)
|
||||
return created, infrastructure("create organization", err)
|
||||
}
|
||||
func (s *Service) UpdateOrganization(ctx context.Context, actor Actor, id string, patch UpdateOrganizationInput) (Organization, error) {
|
||||
if err := requireSuper(actor); err != nil {
|
||||
return Organization{}, err
|
||||
}
|
||||
current, found, err := s.store.GetOrganization(ctx, id)
|
||||
if err != nil {
|
||||
return Organization{}, infrastructure("get organization", err)
|
||||
}
|
||||
if !found {
|
||||
return Organization{}, problem(ErrorNotFound, "组织不存在。")
|
||||
}
|
||||
if patch.Name != nil {
|
||||
name := strings.TrimSpace(*patch.Name)
|
||||
if name == "" {
|
||||
return Organization{}, problem(ErrorValidation, "组织名称不能为空。")
|
||||
}
|
||||
current.Name = name
|
||||
}
|
||||
if patch.Status != nil {
|
||||
if !validStatus(*patch.Status) {
|
||||
return Organization{}, problem(ErrorValidation, "组织状态不正确。")
|
||||
}
|
||||
current.Status = *patch.Status
|
||||
}
|
||||
current.UpdatedAt = s.now()
|
||||
updated, err := s.store.UpdateOrganization(ctx, current)
|
||||
return updated, infrastructure("update organization", err)
|
||||
}
|
||||
func (s *Service) DeleteOrganization(ctx context.Context, actor Actor, id string) error {
|
||||
if err := requireSuper(actor); err != nil {
|
||||
return err
|
||||
}
|
||||
count, err := s.store.CountOrganizationMembers(ctx, id)
|
||||
if err != nil {
|
||||
return infrastructure("count organization members", err)
|
||||
}
|
||||
if count > 0 {
|
||||
return problem(ErrorConflict, "组织仍有账号,不能删除。")
|
||||
}
|
||||
if err := s.store.DeleteOrganization(ctx, id); err != nil {
|
||||
return infrastructure("delete organization", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user