Ver Fonte

fix unwind

Futa Arai há 2 anos atrás
pai
commit
6db5b0f30b

+ 8 - 2
apps/app/src/server/models/page.ts

@@ -338,7 +338,10 @@ export class PageQueryBuilder {
 
 
     if (userGroups != null && userGroups.length > 0) {
     if (userGroups != null && userGroups.length > 0) {
       grantConditions.push(
       grantConditions.push(
-        { grant: GRANT_USER_GROUP, 'grantedGroups.item': { $in: userGroups } },
+        {
+          grant: GRANT_USER_GROUP,
+          grantedGroups: { $elemMatch: { item: { $in: userGroups } } },
+        },
       );
       );
     }
     }
 
 
@@ -943,7 +946,10 @@ export function generateGrantCondition(
   }
   }
   else if (userGroups != null && userGroups.length > 0) {
   else if (userGroups != null && userGroups.length > 0) {
     grantConditions.push(
     grantConditions.push(
-      { grant: GRANT_USER_GROUP, 'grantedGroups.item': { $in: userGroups } },
+      {
+        grant: GRANT_USER_GROUP,
+        grantedGroups: { $elemMatch: { item: { $in: userGroups } } },
+      },
     );
     );
   }
   }
 
 

+ 12 - 12
apps/app/src/server/service/page-grant.ts

@@ -367,21 +367,23 @@ class PageGrantService {
           grantedGroups: 1,
           grantedGroups: 1,
         },
         },
       },
       },
-      { // remove duplicates from pipeline
-        $group: {
-          _id: '$grant',
-          grantedGroupsSet: { $addToSet: '$grantedGroups' },
-          grantedUsersSet: { $addToSet: '$grantedUsers' },
+      {
+        $unwind: { // preprocess for creating groups set
+          path: '$grantedGroups',
+          preserveNullAndEmptyArrays: true,
         },
         },
       },
       },
-      { // flatten granted user set
-        $unwind: {
+      {
+        $unwind: { // preprocess for creating users set
           path: '$grantedUsersSet',
           path: '$grantedUsersSet',
+          preserveNullAndEmptyArrays: true,
         },
         },
       },
       },
-      { // flatten granted group set
-        $unwind: {
-          path: '$grantedGroupsSet',
+      { // remove duplicates from pipeline
+        $group: {
+          _id: '$grant',
+          grantedGroupsSet: { $addToSet: '$grantedGroups' },
+          grantedUsersSet: { $addToSet: '$grantedUsers' },
         },
         },
       },
       },
     ]);
     ]);
@@ -651,7 +653,6 @@ class PageGrantService {
       const isNonApplicableGroupExist = excludeTestIdsFromTargetIds(
       const isNonApplicableGroupExist = excludeTestIdsFromTargetIds(
         [...descendantPagesGrantInfo.grantedUserGroupIds], [...operatorGrantInfo.userGroupIds],
         [...descendantPagesGrantInfo.grantedUserGroupIds], [...operatorGrantInfo.userGroupIds],
       ).length > 0;
       ).length > 0;
-
       if (isNonApplicableGroupExist) {
       if (isNonApplicableGroupExist) {
         return false;
         return false;
       }
       }
@@ -689,7 +690,6 @@ class PageGrantService {
       const isUpdateGroupUsersIncludeAllDescendantsOwners = excludeTestIdsFromTargetIds(
       const isUpdateGroupUsersIncludeAllDescendantsOwners = excludeTestIdsFromTargetIds(
         [...descendantPagesGrantInfo.grantedUserIds], [...updateGrantInfo.grantedUserGroupInfo.userIds],
         [...descendantPagesGrantInfo.grantedUserIds], [...updateGrantInfo.grantedUserGroupInfo.userIds],
       ).length === 0; // b.
       ).length === 0; // b.
-
       return isAllDescendantGroupsChildrenOrItselfOfUpdateGroup && isUpdateGroupUsersIncludeAllDescendantsOwners;
       return isAllDescendantGroupsChildrenOrItselfOfUpdateGroup && isUpdateGroupUsersIncludeAllDescendantsOwners;
     }
     }