Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. /**
  2. *
  3. * @param {AdmitHub.Model.UserSearches} userSearch the userSearch object containing the query to run
  4. * @returns {String} an array of user ids matching the provided query
  5. */
  6. const performPseudoAggregationQuery = function (userSearch) {
  7.  
  8. const groupMatchSets = [];
  9.  
  10. // For each or-group of the search
  11. for (let searchGroup of userSearch.orGroups) {
  12. let termMatchSets = [];
  13.  
  14. // For each search term in this or-group
  15. for (let searchTerm of searchGroup.searchTerms) {
  16.  
  17. // Determine collection to search on and the field on that collection that ids a user
  18. const collectionClass = AdmitHub.Collection[searchTerm.searchCollection];
  19. const idField = (searchTerm.searchCollection === 'Users') ? '_id' : 'userId';
  20.  
  21. // Perform query
  22. const termMatchSet = collectionClass.find(
  23. {[searchTerm.field]: {[searchTerm.operator]: searchTerm.value}},
  24. {fields: {[idField]: 1}}).map(u => u[idField]);
  25.  
  26. termMatchSets.push(termMatchSet);
  27. }
  28.  
  29. // Intersect term match sets (perform AND within group)
  30. const termMatches = termMatchSets.reduce((memo, current) => {
  31. return new Set([...memo].filter(u => current.has(u)));
  32. });
  33.  
  34. groupMatchSets.push(termMatches);
  35. }
  36.  
  37. // Union group match sets (perform OR between groups)
  38. const allMatches = groupMatchSets.reduce((memo, current) => {
  39. return [...memo, ...current];
  40. });
  41. return [...(new Set(allMatches))];
  42. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement