Skip to content

Commit 9032e0b

Browse files
Merge pull request #444 from topcoder-platform/feature/performance-fix-groups
Performance fixes on groups
2 parents cfaa104 + d8dd990 commit 9032e0b

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

src/common/helper.js

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -820,36 +820,21 @@ async function validateChallengeTerms (terms = []) {
820820
*/
821821
async function _filterChallengesByGroupsAccess (currentUser, challenges) {
822822
const res = []
823-
let userGroups
824823
const needToCheckForGroupAccess = !currentUser ? true : !currentUser.isMachine && !hasAdminRole(currentUser)
825-
const subGroupsMap = {}
824+
if(!needToCheckForGroupAccess) return challenges
825+
826+
let userGroups
827+
826828
for (const challenge of challenges) {
827829
challenge.groups = _.filter(challenge.groups, g => !_.includes(['null', 'undefined'], _.toString(g).toLowerCase()))
828-
let expandedGroups = []
829830
if (!challenge.groups || _.get(challenge, 'groups.length', 0) === 0 || !needToCheckForGroupAccess) {
830831
res.push(challenge)
831832
} else if (currentUser) {
832-
// get user groups if not yet
833833
if (_.isNil(userGroups)) {
834-
userGroups = await getUserGroups(currentUser.userId)
835-
}
836-
// Expand challenge groups by subGroups
837-
// results are being saved on a hashmap for efficiency
838-
for (const group of challenge.groups) {
839-
let subGroups
840-
if (subGroupsMap[group]) {
841-
subGroups = subGroupsMap[group]
842-
} else {
843-
subGroups = await expandWithSubGroups(group)
844-
subGroupsMap[group] = subGroups
845-
}
846-
expandedGroups = [
847-
..._.concat(expandedGroups, subGroups)
848-
]
834+
userGroups = await getCompleteUserGroupTreeIds(currentUser.userId)
849835
}
850-
// check if there is matched group
851-
// logger.debug('Groups', challenge.groups, userGroups)
852-
if (_.find(expandedGroups, (group) => !!_.find(userGroups, (ug) => ug.id === group))) {
836+
// get user groups if not yet
837+
if (_.find(challenge.groups, (group) => !!_.find(userGroups, (ug) => ug.id === group))) {
853838
res.push(challenge)
854839
}
855840
}

0 commit comments

Comments
 (0)