|
|
@@ -2,6 +2,7 @@ import mongoose from 'mongoose';
|
|
|
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
import UserGroup from '~/server/models/user-group';
|
|
|
+import { compareObjectId, isIncludesObjectId } from '~/server/util/compare-objectId';
|
|
|
|
|
|
const logger = loggerFactory('growi:service:UserGroupService'); // eslint-disable-line no-unused-vars
|
|
|
|
|
|
@@ -26,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');
|
|
|
@@ -45,9 +46,26 @@ 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 UserGroup.findGroupsWithDescendantsRecursively([userGroup]);
|
|
|
+ const descendants = descendantsWithTarget.filter(d => compareObjectId(d._id, userGroup._id));
|
|
|
+ if (isIncludesObjectId(descendants, parent._id)) {
|
|
|
+ throw Error('It is not allowed to choose parent from descendant groups.');
|
|
|
+ }
|
|
|
+
|
|
|
// find users for comparison
|
|
|
const [targetGroupUsers, parentGroupUsers] = await Promise.all(
|
|
|
[UserGroupRelation.findUserIdsByGroupId(userGroup._id), UserGroupRelation.findUserIdsByGroupId(parent?._id)], // TODO 85062: consider when parent is null to update the group as the root
|