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

Feat/add get organisation user vouch count by source #145

Merged
merged 3 commits into from
Nov 5, 2024
Merged
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
78 changes: 77 additions & 1 deletion src/server-extension/organization-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Arg, Query, Resolver } from "type-graphql";
import type { EntityManager } from "typeorm";
import { VouchCountPerMonth, VouchCountResult } from "./types";
import {
VouchCountByUser,
VouchCountByUserResult,
VouchCountPerMonth,
VouchCountResult,
} from "./types";
import { isValidDate } from "./helper";

@Resolver()
Expand Down Expand Up @@ -84,4 +89,75 @@ export class OrganisationResolver {
throw new Error(`Failed to fetch vouch count by date: ${error.message}`);
}
}

@Query(() => VouchCountByUserResult)
async getOrganisationUserVouchCountBySource(
@Arg("organisationId", () => String) organisationId: string,
@Arg("source", () => String) source: string,
@Arg("fromDate", () => String) fromDate: string,
@Arg("toDate", () => String) toDate: string
): Promise<VouchCountByUserResult> {
MohammadPCh marked this conversation as resolved.
Show resolved Hide resolved
// Validate date format
if (!isValidDate(fromDate) || !isValidDate(toDate)) {
throw new Error(
"Invalid date format. Dates must be in YYYY-MM-DD format."
);
}

const from = new Date(fromDate);
const to = new Date(toDate);
if (from > to) {
throw new Error("`fromDate` cannot be later than `toDate`.");
}

try {
const manager = await this.tx();

const query = `
SELECT
attestor_organisation.attestor_id AS attestor_id,
COUNT(*) AS total_count
FROM
project_attestation
JOIN attestor_organisation
ON project_attestation.attestor_organisation_id = attestor_organisation.id
JOIN project
ON project_attestation.project_id = project.id
WHERE
attestor_organisation.organisation_id = $1
AND project.source = $2
AND project_attestation.vouch = true
AND project_attestation.attest_timestamp BETWEEN $3 AND $4
GROUP BY
attestor_organisation.attestor_id
ORDER BY
total_count DESC;
`;

const params = [organisationId, source, fromDate, toDate];

const result = await manager.query(query, params);

let totalVouches = 0;
const vouchCountByUser: VouchCountByUser[] = result.map((row: any) => {
const totalCount = Number(row.total_count);
totalVouches += totalCount;

return {
attestorId: row.attestor_id,
totalCount,
};
});

return {
totalVouches,
vouchCountByUser,
};
} catch (error: any) {
console.error("Error fetching attestor vouch count by source:", error);
throw new Error(
`Failed to fetch attestor vouch count by source: ${error.message}`
);
}
}
}
18 changes: 18 additions & 0 deletions src/server-extension/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,21 @@ export class VouchCountPerMonth {
@Field()
countWithoutComments: number = 0;
}

@ObjectType()
export class VouchCountByUser {
@Field()
attestorId: string = "";

@Field(() => Int)
totalCount: number = 0;
}

@ObjectType()
export class VouchCountByUserResult {
@Field(() => Int)
totalVouches: number = 0;

@Field(() => [VouchCountByUser])
vouchCountByUser: VouchCountByUser[] = [];
}