Skip to content

Instantly share code, notes, and snippets.

@Ketcap
Created June 17, 2018 05:48
Show Gist options
  • Save Ketcap/9daf1eefa2cd680eaf4cffbf92273d0a to your computer and use it in GitHub Desktop.
Save Ketcap/9daf1eefa2cd680eaf4cffbf92273d0a to your computer and use it in GitHub Desktop.
export const CreateUserSearchQuery = (query) => {
let search1 = { $or: [] };
let search2 = { $or: [] };
let searchQuery = [];
if (!!query.text) {
search1['$or'].push({ 'profile.name': { $regex: query.text, $options: 'i' } });
search1['$or'].push({ 'profile.surname': { $regex: query.text, $options: 'i' } })
search1['$or'].push({ 'username': { $regex: query.text, $options: 'i' } })
searchQuery.push(search1);
}
if (!!query.role && query.role !== 'all') {
search2['$or'].push({ 'roles': { $in: [query.role] } })
searchQuery.push(search2);
}
if (query.role === 'all') {
const role = Meteor.user().roles[0];
search2['$or'].push({ 'roles': { $in: RolesReach[role].map(e => e.id) } });
searchQuery.push(search2)
}
if (!AlaningRoles.userIsInRole(Meteor.userId(), 'admin')) {
const query = { companyId: { $in: [Meteor.user().companyId] } };
searchQuery.push(query)
}
return {
_id: { $ne: Meteor.userId() },
$and: searchQuery
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment