Skip to content

Commit

Permalink
Merge pull request #141 from Giveth/update-sort-resolver
Browse files Browse the repository at this point in the history
add conditions for organization and source
  • Loading branch information
MohammadPCh authored Oct 29, 2024
2 parents fd02eec + 635d5cf commit 70906fa
Showing 1 changed file with 52 additions and 19 deletions.
71 changes: 52 additions & 19 deletions src/server-extension/project-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "reflect-metadata";
import { GraphQLResolveInfo } from "graphql";
import { Arg, Info, Query, Resolver } from "type-graphql";
import { Arg, Info, Int, Query, Resolver } from "type-graphql";
import type { EntityManager } from "typeorm";
import { EProjectSort, ProjectsSortedByVouchOrFlagType } from "./types"; // Custom ProjectType
import { EProjectSort, ProjectsSortedByVouchOrFlagType } from "./types";
import { getProjectSortBy } from "./helper";

@Resolver()
Expand All @@ -11,19 +11,58 @@ export class ProjectResolver {

@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
@Arg("organizations", () => [String], { nullable: true })
organizations?: string[],
@Arg("sources", () => [String], { nullable: true }) sources?: string[],
@Arg("sortBy", () => String, { nullable: true })
sortBy: EProjectSort = EProjectSort.HIGHEST_VOUCH_COUNT,
@Arg("limit", () => Int, { nullable: true }) limit: number = 10,
@Arg("offset", () => Int, { nullable: true }) offset: number = 0
): Promise<ProjectsSortedByVouchOrFlagType[]> {
try {
const manager = await this.tx();

const sortInfo = getProjectSortBy(sortBy);

const vouchValue = sortInfo.sortBy === "vouch";

// Initialize conditions and parameters for dynamic query construction
const conditions = [];
const parameters = [];
let paramIndex = 1;

// Add organization filter if organizations are provided
if (organizations && organizations.length > 0) {
conditions.push(`organisation.id = ANY($${paramIndex}::text[])`);
parameters.push(organizations);
paramIndex++;
}

// Add source filter if sources are provided
if (sources && sources.length > 0) {
conditions.push(`project.source = ANY($${paramIndex}::text[])`);
parameters.push(sources);
paramIndex++;
}

// Add vouch/flag condition
conditions.push(`organisation_project.vouch = $${paramIndex}`);
parameters.push(vouchValue);
paramIndex++;

// Construct WHERE clause
const whereClause =
conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";

// Add limit and offset parameters
parameters.push(limit);
const limitParamIndex = paramIndex;
paramIndex++;

parameters.push(offset);
const offsetParamIndex = paramIndex;
paramIndex++;

// Construct the final query
const query = `
SELECT
project.id,
Expand All @@ -34,22 +73,16 @@ export class ProjectResolver {
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
${whereClause}
GROUP BY
project.id
ORDER BY
SUM(organisation_project.count) ${sortInfo.order}
LIMIT $3 OFFSET $4;
SUM(organisation_project.count) ${sortInfo.order === "ASC" ? "ASC" : "DESC"}
LIMIT $${limitParamIndex} OFFSET $${offsetParamIndex};
`;

const rawProjects = await manager.query(query, [
orgIds,
vouchValue,
limit,
offset,
]);
// Execute the query with parameters
const rawProjects = await manager.query(query, parameters);

return rawProjects;
} catch (error) {
Expand Down

0 comments on commit 70906fa

Please sign in to comment.