From 1a46487eec8974949b741e2395b877e1a9ad7ca4 Mon Sep 17 00:00:00 2001 From: Carlos Date: Mon, 18 Sep 2023 23:10:33 -0500 Subject: [PATCH] unify project counts in userByAddress and projectsByUserId --- src/entities/user.ts | 9 ++------- src/repositories/userRepository.ts | 30 +++++++++++++++++++++++++++++- src/resolvers/userResolver.ts | 2 ++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/entities/user.ts b/src/entities/user.ts index 6c6817f37..d9f2b17c0 100644 --- a/src/entities/user.ts +++ b/src/entities/user.ts @@ -183,13 +183,7 @@ export class User extends BaseEntity { createdAt: Date; @Field(type => Int, { nullable: true }) - async projectsCount() { - const projectsCount = await Project.createQueryBuilder('project') - .where('project."admin" = :id', { id: String(this.id) }) - .getCount(); - - return projectsCount; - } + projectsCount?: number; @Field(type => Int, { nullable: true }) async donationsCount() { @@ -215,6 +209,7 @@ export class User extends BaseEntity { return likedProjectsCount; } + @Field(type => Int, { nullable: true }) async boostedProjectsCount() { return findPowerBoostingsCountByUserId(this.id); diff --git a/src/repositories/userRepository.ts b/src/repositories/userRepository.ts index 7e074598e..e262d59d3 100644 --- a/src/repositories/userRepository.ts +++ b/src/repositories/userRepository.ts @@ -3,6 +3,7 @@ import { SegmentAnalyticsSingleton } from '../services/segment/segmentAnalyticsS import { Donation } from '../entities/donation'; import { Reaction } from '../entities/reaction'; import { PowerBoosting } from '../entities/powerBoosting'; +import { Project, ProjStatus, ReviewStatus } from '../entities/project'; export const findAdminUserByEmail = async ( email: string, @@ -35,6 +36,7 @@ export const isFirstTimeDonor = async (userId: number): Promise => { export const findUserByWalletAddress = async ( walletAddress: string, includeSensitiveFields = true, + ownerUserId?: number, ): Promise => { const query = User.createQueryBuilder('user').where( `LOWER("walletAddress") = :walletAddress`, @@ -45,8 +47,34 @@ export const findUserByWalletAddress = async ( if (!includeSensitiveFields) { query.select(publicSelectionFields); } + const user = await query.getOne(); + if (!user) return null; - return query.getOne(); + user.projectsCount = await fetchUserProjectsCount( + user!.id, + user?.id === ownerUserId, + ); + + return user; +}; + +export const fetchUserProjectsCount = async ( + userId: number, + ownerViewing: boolean, +) => { + const projectsCount = Project.createQueryBuilder('project').where( + 'project."adminUserId" = :id', + { id: userId }, + ); + + if (!ownerViewing) { + projectsCount.andWhere( + `project.statusId = ${ProjStatus.active} AND project.reviewStatus = :reviewStatus`, + { reviewStatus: ReviewStatus.Listed }, + ); + } + + return projectsCount.getCount(); }; export const findUserById = (userId: number): Promise => { diff --git a/src/resolvers/userResolver.ts b/src/resolvers/userResolver.ts index 109655e75..7a9f9d38f 100644 --- a/src/resolvers/userResolver.ts +++ b/src/resolvers/userResolver.ts @@ -66,6 +66,7 @@ export class UserResolver { const foundUser = await findUserByWalletAddress( address, includeSensitiveFields, + user?.userId, ); return { isSignedIn: Boolean(user), @@ -83,6 +84,7 @@ export class UserResolver { const foundUser = await findUserByWalletAddress( address, includeSensitiveFields, + user?.userId, ); if (!foundUser) return;