@@ -19,34 +19,14 @@ const logger = require('../../util/logger');
1919router . post ( '/' , async function ( req , res ) {
2020 if ( ! checkIfTokenSent ( req ) ) {
2121 return res . sendStatus ( FORBIDDEN ) ;
22- } else if ( ! checkIfTokenValid ( req , membershipState . OFFICER ) ) {
22+ } else if ( ! checkIfTokenValid ( req ) ) {
2323 return res . sendStatus ( UNAUTHORIZED ) ;
2424 }
2525
2626 if ( ! req . body . query ) {
2727 return res . status ( OK ) . send ( { items : [ ] } ) ;
2828 }
2929
30- const query = req . body . query . replace ( / [ * \s ] / g, '' ) ;
31-
32- // Create a fuzzy regex pattern to match characters in order, e.g., "pone" -> /p.*o.*n.*e/i
33- const fuzzyPattern = query . split ( '' ) . join ( '.*' ) ;
34- const pattern = new RegExp ( fuzzyPattern , 'i' ) ;
35-
36- const maybeOr = {
37- $or : [
38- {
39- $expr : {
40- $regexMatch : {
41- input : { $concat : [ '$firstName' , '$lastName' ] } ,
42- regex : pattern ,
43- }
44- }
45- } ,
46- { email : { $regex : new RegExp ( query , 'i' ) } }
47- ]
48- } ;
49-
5030 /**
5131 * Function to calculate scores based on token matches for sorting
5232 * @param {string } str - The string to score against
@@ -94,17 +74,21 @@ router.post('/', async function(req, res) {
9474 } ;
9575 } ;
9676
97- // Find user and sort results based on best match of full name or email
98- User . find ( maybeOr , { password : 0 } )
99- . limit ( 5 )
100- . then ( items => {
101- items . sort ( sortByMatch ( req . body . query ) ) ;
102- res . status ( OK ) . send ( { items } ) ;
103- } )
104- . catch ( ( error ) => {
105- logger . error ( '/shortcutsearchusers encountered an error:' , error ) ;
106- res . sendStatus ( BAD_REQUEST ) ;
107- } ) ;
77+ try {
78+ const users = await User . find ( { } ) ;
79+ for ( const user of users ) {
80+ await user . save ( ) ;
81+ }
82+ const topUsers = await User . fuzzySearch ( req . body . query )
83+ . limit ( 5 )
84+ . select ( '-password, -apiKey' ) ;
85+
86+ const sortUsers = topUsers . sort ( sortByMatch ( req . body . query ) ) ;
87+
88+ return res . status ( OK ) . send ( { items : sortUsers } ) ;
89+ } catch ( e ) {
90+ return res . sendStatus ( SERVER_ERROR ) ;
91+ }
10892} ) ;
10993
11094module . exports = router ;
0 commit comments