Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @param {AdmitHub.Model.UserSearches} userSearch the userSearch object containing the query to run
- * @returns {String} an array of user ids matching the provided query
- */
- const performPseudoAggregationQuery = function (userSearch) {
- const groupMatchSets = [];
- // For each or-group of the search
- for (let searchGroup of userSearch.orGroups) {
- let termMatchSets = [];
- // For each search term in this or-group
- for (let searchTerm of searchGroup.searchTerms) {
- // Determine collection to search on and the field on that collection that ids a user
- const collectionClass = AdmitHub.Collection[searchTerm.searchCollection];
- const idField = (searchTerm.searchCollection === 'Users') ? '_id' : 'userId';
- // Perform query
- const termMatchSet = collectionClass.find(
- {[searchTerm.field]: {[searchTerm.operator]: searchTerm.value}},
- {fields: {[idField]: 1}}).map(u => u[idField]);
- termMatchSets.push(termMatchSet);
- }
- // Intersect term match sets (perform AND within group)
- const termMatches = termMatchSets.reduce((memo, current) => {
- return new Set([...memo].filter(u => current.has(u)));
- });
- groupMatchSets.push(termMatches);
- }
- // Union group match sets (perform OR between groups)
- const allMatches = groupMatchSets.reduce((memo, current) => {
- return [...memo, ...current];
- });
- return [...(new Set(allMatches))];
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement