Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
Only search for users by substrings of name, surname, and company, se…
Browse files Browse the repository at this point in the history
…e issue #92
  • Loading branch information
Logan Glasson committed Jul 12, 2018
1 parent ec14c81 commit fdc066e
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions app/api/v1/accounts/UserSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const chalk = require( 'chalk' )

const User = require( '../../../../models/User' )

// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
const escapeRegExp = ( string ) => string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ) // $& means the whole matched string

module.exports = function( req, res ) {
if ( !req.body.email ) {
res.status( 400 )
return res.send( { success: false, message: "Malformed request." } )
}
if ( req.body.email.length < 2 ) {
res.status( 400 )
return res.send( { success: false, message: "Please provide more than two letters." } )
}
User.find( { email: { "$regex": req.body.email, "$options": "i" } }, '_id name surname company' ).limit( 5 )
let conditions = {}
if ( req.body.name ) conditions.name = { '$regex': escapeRegExp( req.body.name ), '$options': 'i' }
if ( req.body.surname ) conditions.surname = { '$regex': escapeRegExp( req.body.surname ), '$options': 'i' }
if ( req.body.company ) conditions.company = { '$regex': escapeRegExp( req.body.company ), '$options': 'i' }

User.find( conditions, '_id name surname company' ).limit( 5 )
.then( myUsers => {
if ( !myUsers ) throw new Error( 'no users found.' )
myUsers.forEach( usr => usr.email = ' ' ) // backwards compat with admin app
Expand Down

0 comments on commit fdc066e

Please sign in to comment.