Skip to content

Commit

Permalink
Merge pull request #156 from GeneralMagicio/fix/qacc-stat
Browse files Browse the repository at this point in the history
Fix qacc stat
  • Loading branch information
aminlatifi authored Dec 29, 2024
2 parents 0aa3e84 + acf04d0 commit 2df604e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
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

0 comments on commit 2df604e

Please sign in to comment.