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

+ 1 - 1
packages/app/src/components/Admin/UserGroup/UserGroupPage.tsx

@@ -97,7 +97,7 @@ const UserGroupPage: FC<Props> = (props: Props) => {
       setSelectedUserGroup(undefined);
       setDeleteModalShown(false);
 
-      toastSuccess(`Deleted ${xss.process(res.data.userGroups.length)} groups.`);
+      toastSuccess(`Deleted ${res.data.userGroups.length} groups.`);
     }
     catch (err) {
       toastError(new Error('Unable to delete the groups'));

+ 1 - 0
packages/app/src/components/Admin/UserGroupDetail/UserGroupDetailPage.tsx

@@ -104,6 +104,7 @@ const UserGroupDetailPage: FC = () => {
     return users;
   }, [searchType, isAlsoMailSearched, isAlsoNameSearched]);
 
+  // TODO 85062: will be used in UserGroupUserFormByInput
   const addUserByUsername = useCallback(async(username: string) => {
     await apiv3Post(`/user-groups/${userGroup._id}/users/${username}`);
 

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

@@ -240,7 +240,7 @@ class UserGroupRelation {
     });
   }
 
-  static createRelations(userGroupIds, user) {
+  static async createRelations(userGroupIds, user) {
     const documentsToInsertMany = userGroupIds.map((groupId) => {
       return {
         relatedGroup: groupId,

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

@@ -66,23 +66,6 @@ schema.statics.findChildUserGroupsByParentIds = async function(parentIds, includ
   };
 };
 
-// schema.statics.removeCompletelyById = async function(deleteGroupId, action, transferToUserGroupId, user) { // TODO 85062: move this to the service layer
-//   const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO 85062: Typescriptize model
-
-//   const groupToDelete = await this.findById(deleteGroupId);
-//   if (groupToDelete == null) {
-//     throw Error(`UserGroup data is not exists. id: ${deleteGroupId}`);
-//   }
-//   const deletedGroup = await groupToDelete.remove();
-
-//   await Promise.all([
-//     UserGroupRelation.removeAllByUserGroup(deletedGroup),
-//     crowi.pageService.handlePrivatePagesForDeletedGroup(deletedGroup, action, transferToUserGroupId, user),
-//   ]);
-
-//   return deletedGroup;
-// };
-
 schema.statics.countUserGroups = function() {
   return this.estimatedDocumentCount();
 };

+ 5 - 1
packages/app/src/server/routes/apiv3/user-group.js

@@ -1,5 +1,9 @@
 import loggerFactory from '~/utils/logger';
+<<<<<<< HEAD
 import { filterIdsByIds } from '~/server/util/compare-objectId';
+=======
+import { excludeTestIdsFromTargetIds } from '~/server/util/compare-objectId';
+>>>>>>> feat/user-group-v5
 
 const logger = loggerFactory('growi:routes:apiv3:user-group'); // eslint-disable-line no-unused-vars
 
@@ -443,7 +447,7 @@ module.exports = (crowi) => {
       const existingRelations = await UserGroupRelation.find({ relatedGroup: { $in: userGroupIds }, relatedUser: user._id });
       const existingGroupIds = existingRelations.map(r => r.relatedGroup);
 
-      const groupIdsOfRelationToCreate = filterIdsByIds(userGroupIds, existingGroupIds);
+      const groupIdsOfRelationToCreate = excludeTestIdsFromTargetIds(userGroupIds, existingGroupIds);
 
       const insertedRelations = await UserGroupRelation.createRelations(groupIdsOfRelationToCreate, user);
       const serializedUser = serializeUserSecurely(user);

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

@@ -2,7 +2,7 @@ import mongoose from 'mongoose';
 
 import loggerFactory from '~/utils/logger';
 import UserGroup from '~/server/models/user-group';
-import { compareObjectId, includesObjectId } from '~/server/util/compare-objectId';
+import { compareObjectId, isIncludesObjectId } from '~/server/util/compare-objectId';
 
 const logger = loggerFactory('growi:service:UserGroupService'); // eslint-disable-line no-unused-vars
 
@@ -62,7 +62,7 @@ class UserGroupService {
     // throw if parent was in its descendants
     const descendantsWithTarget = await UserGroup.findGroupsWithDescendantsRecursively([userGroup]);
     const descendants = descendantsWithTarget.filter(d => compareObjectId(d._id, userGroup._id));
-    if (includesObjectId(descendants, parent._id)) {
+    if (isIncludesObjectId(descendants, parent._id)) {
       throw Error('It is not allowed to choose parent from descendant groups.');
     }
 

+ 17 - 14
packages/app/src/server/util/compare-objectId.ts

@@ -3,28 +3,31 @@ import mongoose from 'mongoose';
 type IObjectId = mongoose.Types.ObjectId;
 const ObjectId = mongoose.Types.ObjectId;
 
-export const compareObjectId = (_id1: IObjectId, _id2: IObjectId): boolean => {
-  const id1 = _id1.toString();
-  const id2 = _id2.toString();
-
-  return id1 === id2;
+export const compareObjectId = (id1: IObjectId, id2: IObjectId): boolean => {
+  return id1.toString() === id2.toString();
 };
 
-export const includesObjectId = (_arr: IObjectId[], _id: IObjectId): boolean => {
-  const arr = _arr.map(i => i.toString());
-  const id = _id.toString();
+export const isIncludesObjectId = (arr: IObjectId[], id: IObjectId): boolean => {
+  const _arr = arr.map(i => i.toString());
+  const _id = id.toString();
 
-  return arr.includes(id);
+  return _arr.includes(_id);
 };
 
-export const filterIdsByIds = (_arr1: IObjectId[], _arr2: IObjectId[]): IObjectId[] => {
+/**
+ * Exclude ObjectIds which exist in testIds from targetIds
+ * @param targetIds Array of mongoose.Types.ObjectId
+ * @param testIds Array of mongoose.Types.ObjectId
+ * @returns Array of mongoose.Types.ObjectId
+ */
+export const excludeTestIdsFromTargetIds = (targetIds: IObjectId[], testIds: IObjectId[]): IObjectId[] => {
   // cast to string
-  const arr1 = _arr1.map(e => e.toString());
-  const arr2 = _arr2.map(e => e.toString());
+  const arr1 = targetIds.map(e => e.toString());
+  const arr2 = testIds.map(e => e.toString());
 
   // filter
-  const filtered = arr1.filter(e => !arr2.includes(e));
+  const excluded = arr2.filter(e => !arr1.includes(e));
 
   // cast to ObjectId
-  return filtered.map(e => new ObjectId(e));
+  return excluded.map(e => new ObjectId(e));
 };