Просмотр исходного кода

refs 126149: set group parent to null when there is no parent

Futa Arai 2 лет назад
Родитель
Сommit
7b7f0d44bb

+ 1 - 1
apps/app/src/components/Admin/UserGroup/UserGroupModal.tsx

@@ -32,7 +32,7 @@ export const UserGroupModal: FC<Props> = (props: Props) => {
    */
   const [currentName, setName] = useState('');
   const [currentDescription, setDescription] = useState('');
-  const [currentParent, setParent] = useState<Ref<IUserGroup> | undefined>();
+  const [currentParent, setParent] = useState<Ref<IUserGroup> | null>(null);
 
   /*
    * Function

+ 1 - 1
apps/app/src/components/Admin/UserGroup/UserGroupTable.tsx

@@ -107,7 +107,7 @@ export const UserGroupTable: FC<Props> = ({
 
     try {
       await onRemove(userGroup);
-      userGroup.parent = undefined;
+      userGroup.parent = null;
     }
     catch {
       //

+ 1 - 1
apps/app/src/features/external-user-group/interfaces/external-user-group.ts

@@ -6,7 +6,7 @@ export const ExternalGroupProviderType = { ldap: 'ldap' } as const;
 export type ExternalGroupProviderType = typeof ExternalGroupProviderType[keyof typeof ExternalGroupProviderType];
 
 export interface IExternalUserGroup extends Omit<IUserGroup, 'parent'> {
-  parent?: Ref<IExternalUserGroup>
+  parent: Ref<IExternalUserGroup> | null
   externalId: string // identifier used in external app/server
   provider: ExternalGroupProviderType
 }

+ 6 - 8
apps/app/src/features/external-user-group/server/models/external-user-group.ts

@@ -36,16 +36,14 @@ schema.plugin(mongoosePaginate);
  * @returns ExternalUserGroupDocument[]
  */
 schema.statics.findAndUpdateOrCreateGroup = async function(name: string, externalId: string, provider: string, description?: string, parentId?: string) {
-  // create without parent
-  if (parentId == null) {
-    return this.findOneAndUpdate({ externalId }, { name, description, provider }, { upsert: true, new: true });
+  let parent: ExternalUserGroupDocument | null = null;
+  if (parentId != null) {
+    parent = await this.findOne({ _id: parentId });
+    if (parent == null) {
+      throw Error('Parent does not exist.');
+    }
   }
 
-  // create with parent
-  const parent = await this.findOne({ _id: parentId });
-  if (parent == null) {
-    throw Error('Parent does not exist.');
-  }
   return this.findOneAndUpdate({ externalId }, {
     name, description, provider, parent,
   }, { upsert: true, new: true });

+ 6 - 8
apps/app/src/server/models/user-group.ts

@@ -71,16 +71,14 @@ schema.statics.countUserGroups = function() {
 };
 
 schema.statics.createGroup = async function(name, description, parentId) {
-  // create without parent
-  if (parentId == null) {
-    return this.create({ name, description });
+  let parent: UserGroupDocument | null = null;
+  if (parentId != null) {
+    parent = await this.findOne({ _id: parentId });
+    if (parent == null) {
+      throw Error('Parent does not exist.');
+    }
   }
 
-  // create with parent
-  const parent = await this.findOne({ _id: parentId });
-  if (parent == null) {
-    throw Error('Parent does not exist.');
-  }
   return this.create({ name, description, parent });
 };
 

+ 7 - 4
apps/app/src/server/service/user-group.ts

@@ -61,7 +61,7 @@ class UserGroupService {
 
     // set parent to null and return when parentId is null
     if (parentId == null) {
-      userGroup.parent = undefined;
+      userGroup.parent = null;
       return userGroup.save();
     }
 
@@ -113,10 +113,13 @@ class UserGroupService {
     return userGroup.save();
   }
 
-  async removeCompletelyByRootGroupId(
+  async removeCompletelyByRootGroupId<
+    D extends UserGroupDocument,
+    RD extends UserGroupRelationDocument,
+  >(
       deleteRootGroupId, action, transferToUserGroupId, user,
-      userGroupModel: Model<UserGroupDocument> & UserGroupModel = UserGroup,
-      userGroupRelationModel: Model<UserGroupRelationDocument> & UserGroupRelationModel = UserGroupRelation,
+      userGroupModel: Model<D> & UserGroupModel = UserGroup,
+      userGroupRelationModel: Model<RD> & UserGroupRelationModel = UserGroupRelation,
   ) {
     const rootGroup = await userGroupModel.findById(deleteRootGroupId);
     if (rootGroup == null) {

+ 2 - 2
apps/app/test/integration/service/external-user-group-sync.test.ts

@@ -97,7 +97,7 @@ const checkSync = async(autoGenerateUserOnGroupSync = true) => {
     name: 'grandParentGroup',
     description: 'this is a grand parent group',
     provider: 'ldap',
-    parent: undefined,
+    parent: null,
   });
   const grandParentGroupRelations = await ExternalUserGroupRelation
     .find({ relatedGroup: grandParentGroup._id });
@@ -160,7 +160,7 @@ const checkSync = async(autoGenerateUserOnGroupSync = true) => {
     name: 'previouslySyncedGroup',
     description: 'this is a previouslySynced group',
     provider: 'ldap',
-    parent: undefined,
+    parent: null,
   });
   const previouslySyncedGroupRelations = await ExternalUserGroupRelation
     .find({ relatedGroup: previouslySyncedGroup._id });

+ 1 - 1
apps/app/test/integration/service/user-groups.test.ts

@@ -177,7 +177,7 @@ describe('UserGroupService', () => {
     const userGroup = await UserGroup.findOne({ _id: groupId3 });
     const updatedUserGroup = await crowi.userGroupService.updateGroup(userGroup._id, userGroup.name, userGroup.description, null);
 
-    expect(updatedUserGroup.parent).toBeUndefined();
+    expect(updatedUserGroup.parent).toBeNull();
   });
 
   /*

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

@@ -36,7 +36,7 @@ export type IUserGroup = {
   name: string;
   createdAt: Date;
   description: string;
-  parent?: Ref<IUserGroupHasId>;
+  parent: Ref<IUserGroupHasId> | null;
 }
 
 export const USER_STATUS = {