Taichi Masuyama 4 лет назад
Родитель
Сommit
8d3e6f30c3
2 измененных файлов с 13 добавлено и 3 удалено
  1. 1 1
      packages/app/src/interfaces/user.ts
  2. 12 2
      packages/app/src/server/service/user-group.ts

+ 1 - 1
packages/app/src/interfaces/user.ts

@@ -18,7 +18,7 @@ export type IUserGroup = {
   name: string;
   createdAt: Date;
   description: string;
-  parent: Ref<IUserGroup>;
+  parent: Ref<IUserGroup> | null;
 }
 
 export type IUserHasId = IUser & HasObjectId;

+ 12 - 2
packages/app/src/server/service/user-group.ts

@@ -27,7 +27,7 @@ class UserGroupService {
 
   // TODO 85062: write test code
   // ref: https://dev.growi.org/61b2cdabaa330ce7d8152844
-  async updateGroup(id, name, description, parentId, forceUpdateParents = false) {
+  async updateGroup(id, name: string, description: string, parentId?: string, forceUpdateParents = false) {
     const userGroup = await UserGroup.findById(id);
     if (userGroup == null) {
       throw new Error('The group does not exist');
@@ -46,11 +46,21 @@ class UserGroupService {
     if (userGroup.parent === parentId) {
       return userGroup.save();
     }
+    // set parent to null and return when parentId is null
+    if (parentId == null) {
+      userGroup.parent = null;
+      return userGroup.save();
+    }
 
     const parent = await UserGroup.findById(parentId);
 
+    if (parent == null) { // it should not be null
+      throw Error('parent does not exist.');
+    }
+
+
     // throw if parent was in its descendants
-    const descendantsWithTarget = await this.findGroupsWithDescendantsRecursively([userGroup]);
+    const descendantsWithTarget = await UserGroup.findGroupsWithDescendantsRecursively([userGroup]);
     const descendants = descendantsWithTarget.filter(d => compareObjectId(d._id, userGroup._id));
     if (includesObjectId(descendants, parent._id)) {
       throw Error('It is not allowed to choose parent from descendant groups.');