Shun Miyazawa 4 лет назад
Родитель
Сommit
82b3d772f3

+ 9 - 13
packages/app/src/server/routes/apiv3/user-group.js

@@ -42,7 +42,7 @@ module.exports = (crowi) => {
   } = crowi.models;
 
   validator.listChildren = [
-    query('parentIdsJoinedByComma', 'parentIds must be an string').optional().isString(),
+    query('parentIds', 'parentIds must be an array').optional().isArray(),
     query('includeGrandChildren', 'parentIds must be boolean').optional().isBoolean(),
   ];
 
@@ -93,22 +93,18 @@ module.exports = (crowi) => {
   // TODO 85062: improve sort
   router.get('/children', loginRequiredStrictly, adminRequired, validator.listChildren, async(req, res) => {
     try {
-      const { parentIdsJoinedByComma, includeGrandChildren = false } = req.query;
+      const { parentIds, includeGrandChildren = false } = req.query;
 
-      let parentIdList = [];
-      if (parentIdsJoinedByComma != null && parentIdsJoinedByComma !== '') { // null check & trim empty string
-        parentIdList = parentIdsJoinedByComma.split(',');
+      if (parentIds == null || parentIds.length < 0) {
+        return res.apiv3Err(new ErrorV3('parentId is required', 'child-user-group-list-fetch-failed'));
       }
 
-      let childUserGroups = [];
-      let grandChildUserGroups = [];
-      if (parentIdList.length > 0) {
-        const userGroupsResult = await UserGroup.findChildUserGroupsByParentIds(parentIdList, includeGrandChildren);
-        childUserGroups = userGroupsResult.childUserGroups;
-        grandChildUserGroups = userGroupsResult.grandChildUserGroups;
-      }
+      const userGroupsResult = await UserGroup.findChildUserGroupsByParentIds(parentIds, includeGrandChildren);
 
-      return res.apiv3({ childUserGroups, grandChildUserGroups });
+      return res.apiv3({
+        childUserGroups: userGroupsResult.childUserGroups,
+        grandChildUserGroups: userGroupsResult.grandChildUserGroups,
+      });
     }
     catch (err) {
       const msg = 'Error occurred in fetching child user group list';

+ 2 - 2
packages/app/src/stores/user-group.tsx

@@ -23,11 +23,11 @@ export const useSWRxUserGroupList = (initialData?: IUserGroupHasId[]): SWRRespon
 export const useSWRxChildUserGroupList = (
     parentIds?: string[], includeGrandChildren?: boolean, initialData?: IUserGroupHasId[],
 ): SWRResponse<IUserGroupHasId[], Error> => {
-  const shouldFetch = parentIds != null && parentIds.join() !== '';
+  const shouldFetch = parentIds != null && parentIds.length > 0;
   return useSWRImmutable<IUserGroupHasId[], Error>(
     shouldFetch ? ['/user-groups/children', parentIds, includeGrandChildren] : null,
     (endpoint, parentIds, includeGrandChildren) => apiv3Get<ChildUserGroupListResult>(
-      endpoint, { parentIdsJoinedByComma: parentIds.join(','), includeGrandChildren },
+      endpoint, { parentIds, includeGrandChildren },
     ).then(result => result.data.childUserGroups),
     {
       fallbackData: initialData,