Taichi Masuyama 4 лет назад
Родитель
Сommit
6cd573845a

+ 1 - 1
packages/app/src/server/models/user-group.ts

@@ -119,4 +119,4 @@ schema.statics.findAllAncestorGroups = async function(parent, ancestors = [paren
   return this.findAllAncestorGroups(nextParent, ancestors);
 };
 
-return getOrCreateModel<UserGroupDocument, UserGroupModel>('UserGroup', schema);
+export default getOrCreateModel<UserGroupDocument, UserGroupModel>('UserGroup', schema);

+ 5 - 8
packages/app/src/server/service/user-group.ts

@@ -1,7 +1,7 @@
 import mongoose from 'mongoose';
 
 import loggerFactory from '~/utils/logger';
-import getUserGroupModel, { UserGroupModel } from '~/server/models/user-group';
+import UserGroup from '~/server/models/user-group';
 
 const logger = loggerFactory('growi:service:UserGroupService'); // eslint-disable-line no-unused-vars
 
@@ -15,11 +15,8 @@ class UserGroupService {
 
   crowi: any;
 
-  UserGroup: UserGroupModel;
-
   constructor(crowi) {
     this.crowi = crowi;
-    this.UserGroup = getUserGroupModel(crowi);
   }
 
   async init() {
@@ -30,13 +27,13 @@ class UserGroupService {
   // TODO 85062: write test code
   // ref: https://dev.growi.org/61b2cdabaa330ce7d8152844
   async updateGroup(id, name, description, parentId, forceUpdateParents = false) {
-    const userGroup = await this.UserGroup.findById(id);
+    const userGroup = await UserGroup.findById(id);
     if (userGroup == null) {
       throw new Error('The group does not exist');
     }
 
     // check if the new group name is available
-    const isExist = (await this.UserGroup.countDocuments({ name })) > 0;
+    const isExist = (await UserGroup.countDocuments({ name })) > 0;
     if (userGroup.name !== name && isExist) {
       throw new Error('The group name is already taken');
     }
@@ -49,7 +46,7 @@ class UserGroupService {
       return userGroup.save();
     }
 
-    const parent = await this.UserGroup.findById(parentId);
+    const parent = await UserGroup.findById(parentId);
 
     // find users for comparison
     const [targetGroupUsers, parentGroupUsers] = await Promise.all(
@@ -59,7 +56,7 @@ class UserGroupService {
     const usersBelongsToTargetButNotParent = targetGroupUsers.filter(user => !parentGroupUsers.includes(user));
     // add the target group's users to all ancestors
     if (forceUpdateParents) {
-      const ancestorGroups = await this.UserGroup.findAllAncestorGroups(parent);
+      const ancestorGroups = await UserGroup.findAllAncestorGroups(parent);
       const ancestorGroupIds = ancestorGroups.map(group => group._id);
 
       await UserGroupRelation.createByGroupIdsAndUserIds(ancestorGroupIds, usersBelongsToTargetButNotParent);