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