From e67c2a69081c49e763903986070962be63ec7dd9 Mon Sep 17 00:00:00 2001 From: ChaituVR Date: Sat, 20 Jul 2024 13:30:35 +0530 Subject: [PATCH] Query leaderboard only to users who don't have a created --- src/graphql/operations/users.ts | 51 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/graphql/operations/users.ts b/src/graphql/operations/users.ts index 7c2ebbde..89837f70 100644 --- a/src/graphql/operations/users.ts +++ b/src/graphql/operations/users.ts @@ -28,8 +28,12 @@ export default async function (parent, args) { const query = ` SELECT - u.* + u.*, + SUM(l.vote_count) as votesCount, + SUM(l.proposal_count) as proposalsCount, + MAX(l.last_vote) as lastVote FROM users u + INNER JOIN leaderboard l ON l.user = u.id WHERE 1=1 ${queryStr} GROUP BY u.id ORDER BY ${orderBy} ${orderDirection} LIMIT ?, ? @@ -44,24 +48,33 @@ export default async function (parent, args) { }); if (!users.length) return []; - const counts = await db.queryAsync(` - SELECT - user, - SUM(vote_count) as votesCount, - SUM(proposal_count) as proposalsCount, - MAX(last_vote) as lastVote - FROM leaderboard - WHERE user IN (${users.map((u: any) => `'${u.id}'`).join(',')}) - GROUP BY user - `); - counts.forEach((count: any) => { - const user = users.find((u: any) => u.id === count.user); - if (user) { - user.votesCount = count.votesCount; - user.proposalsCount = count.proposalsCount; - user.lastVote = count.lastVote; - } - }); + const usersWithOutCreated = users + .filter((u: any) => !u.created) + .map((u: any) => u.id); + if (usersWithOutCreated.length) { + const counts = await db.queryAsync( + ` + SELECT + user, + SUM(vote_count) as votesCount, + SUM(proposal_count) as proposalsCount, + MAX(last_vote) as lastVote + FROM leaderboard + WHERE user IN (?) + GROUP BY user + `, + [usersWithOutCreated] + ); + console.log('counts', counts); + counts.forEach((count: any) => { + const user = users.find((u: any) => u.id === count.user); + if (user) { + user.votesCount = count.votesCount; + user.proposalsCount = count.proposalsCount; + user.lastVote = count.lastVote; + } + }); + } return users.map(formatUser); } catch (e: any) { log.error(`[graphql] users, ${JSON.stringify(e)}`);