-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from Giveth/add-organization-sort
Add organization sort
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { EProjectSort } from "./types"; | ||
|
||
export const getProjectSortBy = (sortBy: EProjectSort) => { | ||
switch (sortBy) { | ||
case EProjectSort.HIGHEST_VOUCH_COUNT: | ||
return { sortBy: "vouch", order: "DESC" }; | ||
case EProjectSort.LOWEST_VOUCH_COUNT: | ||
return { sortBy: "vouch", order: "ASC" }; | ||
case EProjectSort.HIGHEST_FLAG: | ||
return { sortBy: "flag", order: "DESC" }; | ||
case EProjectSort.LOWEST_FLAG: | ||
return { sortBy: "flag", order: "ASC" }; | ||
default: | ||
return { sortBy: "vouch", order: "DESC" }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import "reflect-metadata"; | ||
import { GraphQLResolveInfo } from "graphql"; | ||
import { Arg, Info, Query, Resolver } from "type-graphql"; | ||
import type { EntityManager } from "typeorm"; | ||
import { EProjectSort, ProjectsSortedByVouchOrFlagType } from "./types"; // Custom ProjectType | ||
import { getProjectSortBy } from "./helper"; | ||
|
||
@Resolver() | ||
export class ProjectResolver { | ||
constructor(private tx: () => Promise<EntityManager>) {} | ||
|
||
@Query(() => [ProjectsSortedByVouchOrFlagType]) | ||
async getProjectsSortedByVouchOrFlag( | ||
@Arg("orgIds", () => [String]) orgIds: string[], | ||
@Arg("sortBy", () => String) sortBy: EProjectSort, | ||
@Arg("limit", () => Number, { nullable: true }) limit: number = 10, | ||
@Arg("offset", () => Number, { nullable: true }) offset: number = 0, | ||
@Info() info: GraphQLResolveInfo | ||
): Promise<ProjectsSortedByVouchOrFlagType[]> { | ||
try { | ||
const manager = await this.tx(); | ||
|
||
const sortInfo = getProjectSortBy(sortBy); | ||
|
||
const vouchValue = sortInfo.sortBy === "vouch"; | ||
|
||
const query = ` | ||
SELECT | ||
project.id, | ||
SUM(organisation_project.count) AS total_count | ||
FROM | ||
project | ||
LEFT JOIN organisation_project | ||
ON project.id = organisation_project.project_id | ||
LEFT JOIN organisation | ||
ON organisation_project.organisation_id = organisation.id | ||
WHERE | ||
organisation.id = ANY($1) | ||
AND organisation_project.vouch = $2 | ||
GROUP BY | ||
project.id | ||
ORDER BY | ||
SUM(organisation_project.count) ${sortInfo.order} | ||
LIMIT $3 OFFSET $4; | ||
`; | ||
|
||
const rawProjects = await manager.query(query, [ | ||
orgIds, | ||
vouchValue, | ||
limit, | ||
offset, | ||
]); | ||
|
||
return rawProjects; | ||
} catch (error) { | ||
console.error("Error fetching and sorting projects:", error); | ||
throw new Error("Failed to fetch and sort projects"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ProjectResolver } from "../resolver"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Field, ObjectType, ID, Int } from "type-graphql"; | ||
|
||
export enum EProjectSort { | ||
HIGHEST_VOUCH_COUNT = "totalVouches_DESC", | ||
LOWEST_VOUCH_COUNT = "totalVouches_ASC", | ||
HIGHEST_FLAG = "totalFlags_DESC", | ||
LOWEST_FLAG = "totalFlags_ASC", | ||
} | ||
@ObjectType() | ||
export class ProjectsSortedByVouchOrFlagType { | ||
@Field(() => ID) | ||
id!: string; | ||
} |