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

Fix qacc stat #156

Merged
merged 2 commits into from
Dec 29, 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
36 changes: 30 additions & 6 deletions src/resolvers/qAccResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios, { AxiosResponse } from 'axios';
import sinon from 'sinon';
import { ExecutionResult } from 'graphql';
import { assert } from 'chai';
import { Brackets } from 'typeorm';
import {
createDonationData,
createProjectData,
Expand Down Expand Up @@ -499,11 +500,12 @@ function userCapsTestCases() {
function qAccStatTestCases() {
let project;
let user;
let qfRound1: QfRound;
let qfRound: QfRound;
let earlyAccessRound: EarlyAccessRound;
beforeEach(async () => {
project = await saveProjectDirectlyToDb(createProjectData());
user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
qfRound1 = await QfRound.create({
qfRound = await QfRound.create({
roundNumber: 1,
isActive: true,
name: new Date().toString() + ' - 1',
Expand All @@ -517,6 +519,14 @@ function qAccStatTestCases() {
roundUSDCapPerUserPerProjectWithGitcoinScoreOnly: 1000,
tokenPrice: 0.5,
}).save();
earlyAccessRound = await EarlyAccessRound.create({
roundNumber: generateEARoundNumber(),
startDate: new Date('2000-01-14'),
endDate: new Date('2000-01-16'),
roundUSDCapPerProject: 1000000,
roundUSDCapPerUserPerProject: 50000,
tokenPrice: 0.1,
}).save();
sinon.useFakeTimers({
now: new Date('2001-01-15').getTime(),
});
Expand All @@ -525,7 +535,8 @@ function qAccStatTestCases() {
// Clean up the database after each test
await ProjectRoundRecord.delete({});
await Donation.delete({ projectId: project.id });
await QfRound.delete(qfRound1.id);
await QfRound.delete(qfRound.id);
await EarlyAccessRound.delete(earlyAccessRound.id);

sinon.restore();
});
Expand All @@ -538,6 +549,7 @@ function qAccStatTestCases() {
...createDonationData(),
amount: nonQfDonationAmount,
status: DONATION_STATUS.VERIFIED,
earlyAccessRoundId: earlyAccessRound.id,
},
user.id,
project.id,
Expand All @@ -547,7 +559,7 @@ function qAccStatTestCases() {
...createDonationData(),
amount: qfDonationAmount,
status: DONATION_STATUS.VERIFIED,
qfRoundId: qfRound1.id,
qfRoundId: qfRound.id,
},
user.id,
project.id,
Expand All @@ -568,17 +580,29 @@ function qAccStatTestCases() {

const dataSource = AppDataSource.getDataSource();
const totalDonations = await dataSource.query(`
SELECT COALESCE(SUM(amount), 0) as totalCollected from donation where status = '${DONATION_STATUS.VERIFIED}'
SELECT COALESCE(SUM(amount), 0) as totalCollected
from donation
where status = '${DONATION_STATUS.VERIFIED}'
AND ( "qfRoundId" IS NOT NULL OR "earlyAccessRoundId" IS NOT NULL)
`);
const qfTotalDonations = await dataSource.query(`
SELECT COALESCE(SUM(amount), 0) as qfTotalCollected from donation where status = '${DONATION_STATUS.VERIFIED}' AND "qfRoundId" IS NOT NULL
SELECT COALESCE(SUM(amount), 0) as qfTotalCollected
from donation
where status = '${DONATION_STATUS.VERIFIED}' AND "qfRoundId" IS NOT NULL
`);
// count unique contributors
const contributorsCount = await Donation.createQueryBuilder('donation')
.select('COUNT(DISTINCT "userId")', 'contributorsCount')
.where('donation.status = :status', {
status: DONATION_STATUS.VERIFIED,
})
.andWhere(
new Brackets(qb => {
qb.orWhere('donation."qfRoundId" IS NOT NULL').orWhere(
'donation."earlyAccessRoundId" IS NOT NULL',
);
}),
)
.getRawOne();

assert.deepEqual(result.data.data?.qAccStat, {
Expand Down
57 changes: 28 additions & 29 deletions src/services/qAccService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FindOneOptions } from 'typeorm';
import { Brackets, FindOneOptions } from 'typeorm';
import { EarlyAccessRound } from '../entities/earlyAccessRound';
import { ProjectRoundRecord } from '../entities/projectRoundRecord';
import { ProjectUserRecord } from '../entities/projectUserRecord';
Expand Down Expand Up @@ -276,38 +276,37 @@ const getQAccStat = async (): Promise<{
qfTotalCollected: number;
totalContributors: number;
}> => {
const [qfTotalCollected, totalCollected, totalContributors] =
await Promise.all([
Donation.createQueryBuilder('donation')
.select('COALESCE(sum(donation.amount), 0)', 'total_qf_collected')
.where('donation.status = :status', {
status: DONATION_STATUS.VERIFIED,
})
.andWhere('donation."qfRoundId" IS NOT NULL')
.cache('qf_total_collected_donation', 1000)
.getRawOne(),

Donation.createQueryBuilder('donation')
.select('COALESCE(sum(donation.amount), 0)', 'total_collected')
.where('donation.status = :status', {
status: DONATION_STATUS.VERIFIED,
})
.cache('total_collected_donation', 1000)
.getRawOne(),

Donation.createQueryBuilder('donation')
.select('count(distinct donation."userId")', 'total_contributors')
.where('donation.status = :status', {
status: DONATION_STATUS.VERIFIED,
})
.cache('total_contributors', 1000)
.getRawOne(),
]);
const [qfTotalCollected, totalCollected] = await Promise.all([
Donation.createQueryBuilder('donation')
.select('COALESCE(sum(donation.amount), 0)', 'total_qf_collected')
.where('donation.status = :status', {
status: DONATION_STATUS.VERIFIED,
})
.andWhere('donation."qfRoundId" IS NOT NULL')
.cache('qf_total_collected_donation', 1000)
.getRawOne(),

Donation.createQueryBuilder('donation')
.select('COALESCE(sum(donation.amount), 0)', 'total_collected')
.addSelect('count(distinct donation."userId")', 'total_contributors')
.where('donation.status = :status', {
status: DONATION_STATUS.VERIFIED,
})
.andWhere(
new Brackets(qb => {
qb.orWhere('donation."qfRoundId" IS NOT NULL').orWhere(
'donation."earlyAccessRoundId" IS NOT NULL',
);
}),
)
.cache('total_collected_donation', 1000)
.getRawOne(),
]);

return {
totalCollected: totalCollected.total_collected,
qfTotalCollected: qfTotalCollected.total_qf_collected,
totalContributors: totalContributors.total_contributors,
totalContributors: totalCollected.total_contributors,
};
};

Expand Down
Loading