Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add qfroundraisedfunds filters #1128

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/entities/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export enum SortingField {
QualityScore = 'QualityScore',
GIVPower = 'GIVPower',
InstantBoosting = 'InstantBoosting',
ActiveQfRoundFundsRaised = 'ActiveQfRoundFundsRaised',
}

export enum FilterField {
Expand Down
40 changes: 38 additions & 2 deletions src/repositories/projectRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UpdateResult } from 'typeorm';
import { QueryBuilder, UpdateResult } from 'typeorm';
import {
FilterField,
Project,
Expand All @@ -17,6 +17,8 @@ import {
import { User, publicSelectionFields } from '../entities/user';
import { ResourcesTotalPerMonthAndYear } from '../resolvers/donationResolver';
import { OrderDirection, ProjectResolver } from '../resolvers/projectResolver';
import { findActiveQfRound } from './qfRoundRepository';
import { Donation } from '../entities/donation';

export const findProjectById = (projectId: number): Promise<Project | null> => {
// return Project.findOne({ id: projectId });
Expand Down Expand Up @@ -60,7 +62,9 @@ export type FilterProjectQueryInputParams = {
sortingBy?: SortingField;
qfRoundId?: number;
};
export const filterProjectsQuery = (params: FilterProjectQueryInputParams) => {
export const filterProjectsQuery = async (
params: FilterProjectQueryInputParams,
) => {
const {
limit,
skip,
Expand Down Expand Up @@ -115,6 +119,35 @@ export const filterProjectsQuery = (params: FilterProjectQueryInputParams) => {
'projectInstantPower.totalPower',
'projectInstantPower.powerRank',
]);
} else if (sortingBy === SortingField.ActiveQfRoundFundsRaised) {
const activeQfRound = await findActiveQfRound();

const subquery = Donation.createQueryBuilder('donation')
.select('donation."projectId", donation."qfRoundId"')
.addSelect('SUM(donation."valueUsd")', 'qfRoundRaisedFunds')
.where(`donation."qfRoundId" = ${activeQfRound?.id || 0}`)
.groupBy('donation."projectId", donation."qfRoundId"')
.getQuery();

query = query.leftJoin(
'(' + subquery + ')',
'donationSummary',
'project.id = donationSummary."projectId"',
);

// query = query
// .leftJoinAndSelect('project.donations', 'donation', 'donation.projectId = project.id AND donation.qfRoundId = :qfRoundId', { qfRoundId: activeQfRound?.id || 0 })
// // .leftJoin(
// // qb => {
// // return qb
// // .select('donation."projectId", donation."qfRoundId"')
// // .addSelect('SUM(donation."valueUsd")', 'qfRoundRaisedFunds')
// // .from(Donation, 'donation')
// // .groupBy('donation."projectId", donation."qfRoundId"');
// // },
// // 'donation',
// // `donation."projectId" = project.id AND donation."qfRoundId" = ${activeQfRound?.id || 0}`
// // )
}

// Filters
Expand Down Expand Up @@ -168,6 +201,9 @@ export const filterProjectsQuery = (params: FilterProjectQueryInputParams) => {
'NULLS LAST',
);
break;
case SortingField.ActiveQfRoundFundsRaised:
query.orderBy('donation."qfRoundRaisedFunds"', 'DESC');
break;
default:
query
.orderBy('projectInstantPower.totalPower', OrderDirection.DESC)
Expand Down
64 changes: 64 additions & 0 deletions src/resolvers/projectResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,70 @@ function allProjectsTestCases() {
);
});

// it('should return projects, sort by project raised funds in the active QF round DESC', async () => {
// const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
// const project1 = await saveProjectDirectlyToDb({
// ...createProjectData(),
// title: String(new Date().getTime()),
// slug: String(new Date().getTime()),
// });
// const project2 = await saveProjectDirectlyToDb({
// ...createProjectData(),
// title: String(new Date().getTime()),
// slug: String(new Date().getTime()),
// });

// const qfRound = await QfRound.create({
// isActive: true,
// name: 'test filter by qfRoundId',
// minimumPassportScore: 10,
// allocatedFund: 100,
// beginDate: new Date(),
// endDate: new Date(),
// }).save();
// project1.qfRounds = [qfRound];
// await project1.save();
// project2.qfRounds = [qfRound];
// await project2.save();

// await saveDonationDirectlyToDb(
// {
// ...createDonationData(),
// status: 'verified',
// qfRoundId: qfRound.id,
// valueUsd: 2,
// },
// donor.id,
// project1.id,
// );

// await saveDonationDirectlyToDb(
// {
// ...createDonationData(),
// status: 'verified',
// qfRoundId: qfRound.id,
// valueUsd: 20,
// },
// donor.id,
// project1.id,
// );

// const result = await axios.post(graphqlUrl, {
// query: fetchMultiFilterAllProjectsQuery,
// variables: {
// sortingBy: SortingField.ActiveQfRoundFundsRaised,
// limit: 10,
// },
// });

// assert.equal(result.data.data.allProjects.projects.length, 3);
// // result.data.data.allProjects.projects.forEach(project => {
// // assert.equal(project.qfRounds[0].id, qfRound.id);
// // });
// qfRound.isActive = false;
// await qfRound.save();
// });

it('should return projects, sort by project instant power DESC', async () => {
await PowerBoosting.clear();
await InstantPowerBalance.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/projectResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ export class ProjectResolver {
filterQueryParams.slugArray = campaign.relatedProjectsSlugs;
}

const projectsQuery = filterProjectsQuery(filterQueryParams);
const projectsQuery = await filterProjectsQuery(filterQueryParams);

const projectsQueryCacheKey = await projectsFiltersThreadPool.queue(
hasher =>
Expand Down
2 changes: 1 addition & 1 deletion src/services/campaignService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const fillCampaignProjects = async (params: {
return campaign;
}

const projectsQuery = filterProjectsQuery(projectsQueryParams);
const projectsQuery = await filterProjectsQuery(projectsQueryParams);
const projectsQueryCacheKey = await projectsFiltersThreadPool.queue(hasher =>
hasher.hashProjectFilters({
...projectsQueryParams,
Expand Down