@@ -8,6 +8,7 @@ const firestore = require("../utils/firestore");
8
8
const { fetchWallet, createWallet } = require ( "../models/wallets" ) ;
9
9
const { arraysHaveCommonItem } = require ( "../utils/array" ) ;
10
10
const { ALLOWED_FILTER_PARAMS } = require ( "../constants/users" ) ;
11
+ const { BATCH_SIZE_IN_CLAUSE } = require ( "../constants/firebase" ) ;
11
12
const userModel = firestore . collection ( "users" ) ;
12
13
const joinModel = firestore . collection ( "applicants" ) ;
13
14
const itemModel = firestore . collection ( "itemTags" ) ;
@@ -178,26 +179,38 @@ const fetchPaginatedUsers = async (query) => {
178
179
}
179
180
} ;
180
181
181
- const fetchAllUsers = async ( ) => {
182
+ const fetchUsers = async ( usernames = [ ] ) => {
182
183
try {
183
184
const dbQuery = userModel ;
185
+ const filterdUsersWithDetails = [ ] ;
184
186
185
- const snapshot = await dbQuery . get ( ) ;
187
+ const groups = [ ] ;
188
+ for ( let i = 0 ; i < usernames . length ; i += BATCH_SIZE_IN_CLAUSE ) {
189
+ groups . push ( usernames . slice ( i , i + BATCH_SIZE_IN_CLAUSE ) ) ;
190
+ }
186
191
187
- const allUsers = [ ] ;
192
+ // For each group, write a separate query
193
+ const promises = groups . map ( ( group ) => {
194
+ return dbQuery . where ( "github_id" , "in" , group ) . get ( ) ;
195
+ } ) ;
188
196
189
- snapshot . forEach ( ( doc ) => {
190
- allUsers . push ( {
191
- id : doc . id ,
192
- ...doc . data ( ) ,
193
- phone : undefined ,
194
- email : undefined ,
195
- tokens : undefined ,
196
- chaincode : undefined ,
197
+ const snapshots = await Promise . all ( promises ) ;
198
+
199
+ snapshots . forEach ( ( snapshot ) => {
200
+ snapshot . forEach ( ( doc ) => {
201
+ filterdUsersWithDetails . push ( {
202
+ id : doc . id ,
203
+ ...doc . data ( ) ,
204
+ phone : undefined ,
205
+ email : undefined ,
206
+ tokens : undefined ,
207
+ chaincode : undefined ,
208
+ } ) ;
197
209
} ) ;
198
210
} ) ;
211
+
199
212
return {
200
- allUsers ,
213
+ filterdUsersWithDetails ,
201
214
} ;
202
215
} catch ( err ) {
203
216
logger . error ( "Error retrieving user data" , err ) ;
@@ -392,6 +405,6 @@ module.exports = {
392
405
getJoinData,
393
406
getSuggestedUsers,
394
407
fetchUserSkills,
395
- fetchAllUsers ,
408
+ fetchUsers ,
396
409
getUsersBasedOnFilter,
397
410
} ;
0 commit comments