@@ -6,10 +6,13 @@ const walletConstants = require("../constants/wallets");
6
6
7
7
const firestore = require ( "../utils/firestore" ) ;
8
8
const { fetchWallet, createWallet } = require ( "../models/wallets" ) ;
9
+ const { arraysHaveCommonItem } = require ( "../utils/array" ) ;
10
+ const { ALLOWED_FILTER_PARAMS } = require ( "../constants/users" ) ;
9
11
const userModel = firestore . collection ( "users" ) ;
10
12
const joinModel = firestore . collection ( "applicants" ) ;
11
13
const itemModel = firestore . collection ( "itemTags" ) ;
12
-
14
+ const userStatusModel = firestore . collection ( "usersStatus" ) ;
15
+ const { ITEM_TAG , USER_STATE } = ALLOWED_FILTER_PARAMS ;
13
16
/**
14
17
* Adds or updates the user data
15
18
*
@@ -292,6 +295,64 @@ const fetchUserSkills = async (id) => {
292
295
}
293
296
} ;
294
297
298
+ /**
299
+ * Fetches user data based on the filter query
300
+ *
301
+ * @param {Object } query - Object with query parameters
302
+ * @param {Array } query.levelId - Array of levelIds to filter the users on
303
+ * @param {Array } query.levelName - Array of levelNames to filter the users on
304
+ * @param {Array } query.levelNumber - Array of levelNumbers to filter the users on
305
+ * @param {Array } query.tagId - Array of tagIds to filter the users on
306
+ * @param {Array } query.state - Array of states to filter the users on
307
+ * @return {Promise<Array> } - Array of user documents that match the filter criteria
308
+ */
309
+
310
+ const getUsersBasedOnFilter = async ( query ) => {
311
+ const allQueryKeys = Object . keys ( query ) ;
312
+ const doesTagQueryExist = arraysHaveCommonItem ( ITEM_TAG , allQueryKeys ) ;
313
+ const doesStateQueryExist = arraysHaveCommonItem ( USER_STATE , allQueryKeys ) ;
314
+
315
+ const calls = {
316
+ item : itemModel ,
317
+ state : userStatusModel ,
318
+ } ;
319
+ calls . item = calls . item . where ( "itemType" , "==" , "USER" ) . where ( "tagType" , "==" , "SKILL" ) ;
320
+
321
+ Object . entries ( query ) . forEach ( ( [ key , value ] ) => {
322
+ const isTagKey = ITEM_TAG . includes ( key ) ;
323
+ const isStateKey = USER_STATE . includes ( key ) ;
324
+ const isValueArray = Array . isArray ( value ) ;
325
+
326
+ if ( isTagKey ) {
327
+ calls . item = isValueArray ? calls . item . where ( key , "in" , value ) : calls . item . where ( key , "==" , value ) ;
328
+ } else if ( isStateKey ) {
329
+ calls . state = isValueArray
330
+ ? calls . state . where ( "currentStatus.state" , "in" , value )
331
+ : calls . state . where ( "currentStatus.state" , "==" , value ) ;
332
+ }
333
+ } ) ;
334
+
335
+ const tagItems = doesTagQueryExist ? ( await calls . item . get ( ) ) . docs . map ( ( doc ) => ( { id : doc . id , ...doc . data ( ) } ) ) : [ ] ;
336
+ const stateItems = doesStateQueryExist
337
+ ? ( await calls . state . get ( ) ) . docs . map ( ( doc ) => ( { id : doc . id , ...doc . data ( ) } ) )
338
+ : [ ] ;
339
+ let finalItems = [ ] ;
340
+
341
+ if ( doesTagQueryExist && doesStateQueryExist ) {
342
+ const stateItemIds = new Set ( stateItems . map ( ( item ) => item . userId ) ) ;
343
+ finalItems = tagItems . filter ( ( item ) => stateItemIds . has ( item . itemId ) ) . map ( ( item ) => item . itemId ) ;
344
+ } else if ( doesStateQueryExist ) {
345
+ finalItems = stateItems . map ( ( item ) => item . userId ) ;
346
+ } else if ( doesTagQueryExist ) {
347
+ finalItems = tagItems . map ( ( item ) => item . itemId ) ;
348
+ }
349
+
350
+ finalItems = [ ...new Set ( finalItems ) ] ;
351
+ const userRefs = finalItems . map ( ( itemId ) => userModel . doc ( itemId ) ) ;
352
+ const userDocs = ( await firestore . getAll ( ...userRefs ) ) . map ( ( doc ) => ( { id : doc . id , ...doc . data ( ) } ) ) ;
353
+ return userDocs ;
354
+ } ;
355
+
295
356
module . exports = {
296
357
addOrUpdate,
297
358
fetchUsers,
@@ -304,4 +365,5 @@ module.exports = {
304
365
getJoinData,
305
366
getSuggestedUsers,
306
367
fetchUserSkills,
368
+ getUsersBasedOnFilter,
307
369
} ;
0 commit comments