Skip to content

Commit

Permalink
Merge branch 'staging' of github.com:Giveth/impact-graph into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadranjbarz committed Sep 25, 2023
2 parents 97e6ac2 + 28c1299 commit 6adbc81
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
4 changes: 2 additions & 2 deletions migration/1694295208252-AddEligibleNetworksToQfRoundEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export class AddEligibleNetworksToQfRoundEntity1694295208252
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "qf_round"
ADD COLUMN IF NOT EXIST "eligibleNetworks" integer array DEFAULT ARRAY[]::integer[]
ALTER TABLE public.qf_round
ADD COLUMN IF NOT EXISTS "eligibleNetworks" integer array DEFAULT ARRAY[]::integer[]
`);
}

Expand Down
18 changes: 9 additions & 9 deletions migration/1694635872128-AddEligibleNetworksToPreviousQfRounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ export class AddEligibleNetworksToPreviousQfRounds1694635872128
? [1, 3, 5, 100, 137, 10, 420, 56, 42220, 44787] // Include testnets for staging
: [1, 137, 56, 42220, 100, 10]; // Exclude testnets for non-staging

// Convert the eligibleNetworks array to a comma-separated string
const eligibleNetworksString = eligibleNetworks.join(', ');

// Update the "qf_round" table with the new eligibleNetworks values
await queryRunner.query(`
UPDATE "qf_round"
SET eligibleNetworks = '{${eligibleNetworksString}}'
`);
await queryRunner.query(
`
UPDATE public.qf_round
SET "eligibleNetworks" = $1
`,
[eligibleNetworks],
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
UPDATE "qf_round"
SET eligibleNetworks = '{}'
UPDATE public.qf_round
SET "eligibleNetworks" = '{}'
`);
}
}
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
13 changes: 13 additions & 0 deletions src/repositories/donationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ export const fillQfRoundDonationsUserScores = async (): Promise<void> => {
`);
};

export const addressHasDonated = async (address: string) => {
const projectAddress = await Donation.query(
`
SELECT "id"
FROM donation
where lower("fromWalletAddress") = $1
limit 1
`,
[address.toLowerCase()],
);
return projectAddress.length > 0;
};

export const createDonation = async (data: {
amount: number;
project: Project;
Expand Down
29 changes: 28 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 @@ -45,8 +46,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,
includeSensitiveFields,
);

return user;
};

export const fetchUserProjectsCount = async (
userId: number,
includeSensitiveFields: boolean,
) => {
const projectsCount = Project.createQueryBuilder('project').where(
'project."adminUserId" = :id',
{ id: userId },
);

if (!includeSensitiveFields) {
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
29 changes: 28 additions & 1 deletion src/resolvers/userResolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Arg, Ctx, Mutation, Query, Resolver } from 'type-graphql';
import {
Arg,
Ctx,
Field,
Mutation,
ObjectType,
Query,
Resolver,
} from 'type-graphql';
import { Repository } from 'typeorm';

import { User } from '../entities/user';
Expand All @@ -18,6 +26,17 @@ import { SegmentAnalyticsSingleton } from '../services/segment/segmentAnalyticsS
import { AppDataSource } from '../orm';
import { getGitcoinAdapter } from '../adapters/adaptersFactory';
import { logger } from '../utils/logger';
import { isWalletAddressInPurpleList } from '../repositories/projectAddressRepository';
import { addressHasDonated } from '../repositories/donationRepository';

@ObjectType()
class UserRelatedAddressResponse {
@Field(type => Boolean, { nullable: false })
hasRelatedProject: boolean;

@Field(type => Boolean, { nullable: false })
hasDonated: boolean;
}

@Resolver(of => User)
export class UserResolver {
Expand All @@ -29,6 +48,14 @@ export class UserResolver {
// return User.create(data).save();
}

@Query(returns => UserRelatedAddressResponse)
async walletAddressUsed(@Arg('address') address: string) {
return {
hasRelatedProject: await isWalletAddressInPurpleList(address),
hasDonated: await addressHasDonated(address),
};
}

@Query(returns => UserByAddressResponse, { nullable: true })
async userByAddress(
@Arg('address', type => String) address: string,
Expand Down

0 comments on commit 6adbc81

Please sign in to comment.