-
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 #149 from Giveth/add-getAttestorsTotalCount
Add get attesters total count
- Loading branch information
Showing
4 changed files
with
45 additions
and
2 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,7 @@ | ||
import { Field, ObjectType } from "type-graphql"; | ||
|
||
@ObjectType() | ||
export class AttestorsTotalCountResult { | ||
@Field() | ||
totalCount: number = 0; | ||
} |
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,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"); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { ProjectResolver } from "../project-resolver"; | ||
export { OrganisationResolver } from "../organization-resolver"; | ||
export { AttestorResolver } from "../attestor-resolver"; |