Skip to content

Commit

Permalink
Merge pull request #149 from Giveth/add-getAttestorsTotalCount
Browse files Browse the repository at this point in the history
Add get attesters total count
  • Loading branch information
MohammadPCh authored Nov 11, 2024
2 parents 712a557 + b9952e6 commit f61af4f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/server-extension/attestor-resolver-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Field, ObjectType } from "type-graphql";

@ObjectType()
export class AttestorsTotalCountResult {
@Field()
totalCount: number = 0;
}
36 changes: 36 additions & 0 deletions src/server-extension/attestor-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Query, Resolver } from "type-graphql";
import type { EntityManager } from "typeorm";
import { AttestorsTotalCountResult } from "./attestor-resolver-types";

@Resolver()
export class AttestorResolver {
constructor(private tx: () => Promise<EntityManager>) {}

@Query(() => AttestorsTotalCountResult)
async getAttestorsTotalCount(): Promise<AttestorsTotalCountResult> {
try {
const manager = await this.tx();

// Construct the final query
const query = `
SELECT COUNT(DISTINCT attestor_organisation.attestor_id) AS total_unique_attesters
FROM project_attestation
JOIN attestor_organisation ON project_attestation.attestor_organisation_id = attestor_organisation.id;
`;

// Execute the query with parameters
const rawProjects = await manager.query(query);

if (!rawProjects?.[0]?.total_unique_attesters) {
return { totalCount: 0 };
}

return {
totalCount: rawProjects[0].total_unique_attesters,
};
} catch (error) {
console.error("Error counting attestors:", error);
throw new Error("Failed to count attestors");
}
}
}
3 changes: 1 addition & 2 deletions src/server-extension/project-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "reflect-metadata";
import { GraphQLResolveInfo } from "graphql";
import { Arg, Info, Int, Query, Resolver } from "type-graphql";
import { Arg, Int, Query, Resolver } from "type-graphql";
import type { EntityManager } from "typeorm";
import { EProjectSort, ProjectsSortedByVouchOrFlagType } from "./types";
import { getProjectSortBy } from "./helper";
Expand Down
1 change: 1 addition & 0 deletions src/server-extension/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { ProjectResolver } from "../project-resolver";
export { OrganisationResolver } from "../organization-resolver";
export { AttestorResolver } from "../attestor-resolver";

0 comments on commit f61af4f

Please sign in to comment.