Skip to content

Commit

Permalink
Merge pull request #1129 from Giveth/feature_unify_project_count
Browse files Browse the repository at this point in the history
Unify project counts in userByAddress and projectsByUserId
  • Loading branch information
CarlosQ96 authored Sep 19, 2023
2 parents 7a0b6c1 + 1a46487 commit 0757d1c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -215,6 +209,7 @@ export class User extends BaseEntity {

return likedProjectsCount;
}

@Field(type => Int, { nullable: true })
async boostedProjectsCount() {
return findPowerBoostingsCountByUserId(this.id);
Expand Down
30 changes: 29 additions & 1 deletion src/repositories/userRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -35,6 +36,7 @@ export const isFirstTimeDonor = async (userId: number): Promise<boolean> => {
export const findUserByWalletAddress = async (
walletAddress: string,
includeSensitiveFields = true,
ownerUserId?: number,
): Promise<User | null> => {
const query = User.createQueryBuilder('user').where(
`LOWER("walletAddress") = :walletAddress`,
Expand All @@ -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<User | null> => {
Expand Down
2 changes: 2 additions & 0 deletions src/resolvers/userResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class UserResolver {
const foundUser = await findUserByWalletAddress(
address,
includeSensitiveFields,
user?.userId,
);
return {
isSignedIn: Boolean(user),
Expand All @@ -83,6 +84,7 @@ export class UserResolver {
const foundUser = await findUserByWalletAddress(
address,
includeSensitiveFields,
user?.userId,
);

if (!foundUser) return;
Expand Down

0 comments on commit 0757d1c

Please sign in to comment.