@@ -337,11 +337,46 @@ export class UserResolver {
337337 if ( walletAddress ) {
338338 whereCondition . walletAddress = walletAddress ;
339339 }
340- const [ users , totalCount ] = await UserRankMaterializedView . findAndCount ( {
341- where : whereCondition ,
342- order : { [ orderBy . field ] : orderBy . direction } ,
343- take,
344- skip,
340+
341+ // Get data from materialized view for efficient filtering/pagination
342+ const [ rankedUsers , totalCount ] =
343+ await UserRankMaterializedView . findAndCount ( {
344+ where : whereCondition ,
345+ order : { [ orderBy . field ] : orderBy . direction } ,
346+ take,
347+ skip,
348+ } ) ;
349+
350+ // If no users found, return early
351+ if ( rankedUsers . length === 0 ) {
352+ return { users : [ ] , totalCount } ;
353+ }
354+
355+ // Extract user IDs to fetch only needed additional fields
356+ const userIds = rankedUsers . map ( user => user . id ) ;
357+
358+ // Fetch only the needed fields that are missing from materialized view
359+ const additionalUserData = await this . userRepository
360+ . createQueryBuilder ( 'user' )
361+ . select ( [ 'user.id' , 'user.avatar' , 'user.username' ] )
362+ . where ( 'user.id IN (:...userIds)' , { userIds } )
363+ . getMany ( ) ;
364+
365+ // Create a map for quick lookup of additional user data
366+ const userMap = new Map ( additionalUserData . map ( user => [ user . id , user ] ) ) ;
367+
368+ // Merge the ranked data with additional user data, preserving the order from materialized view
369+ const users = rankedUsers . map ( rankedUser => {
370+ const additionalData = userMap . get ( rankedUser . id ) ;
371+ if ( additionalData ) {
372+ // Combine materialized view data with additional fields
373+ return {
374+ ...rankedUser ,
375+ avatar : additionalData . avatar ,
376+ username : additionalData . username ,
377+ } ;
378+ }
379+ return rankedUser ; // Fallback to ranked user if additional data not found
345380 } ) ;
346381
347382 return { users, totalCount } ;
0 commit comments