diff --git a/src/resolvers/qAccResolver.test.ts b/src/resolvers/qAccResolver.test.ts index 2a24b3d26..3a41a1de6 100644 --- a/src/resolvers/qAccResolver.test.ts +++ b/src/resolvers/qAccResolver.test.ts @@ -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, @@ -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', @@ -517,6 +519,12 @@ function qAccStatTestCases() { roundUSDCapPerUserPerProjectWithGitcoinScoreOnly: 1000, tokenPrice: 0.5, }).save(); + earlyAccessRound = await EarlyAccessRound.create({ + roundNumber: Math.floor(Math.random() * 10000), + startDate: moment().subtract(3, 'days').toDate(), + endDate: moment().add(10, 'days').toDate(), + tokenPrice: undefined, + }); sinon.useFakeTimers({ now: new Date('2001-01-15').getTime(), }); @@ -525,7 +533,7 @@ 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); sinon.restore(); }); @@ -538,6 +546,7 @@ function qAccStatTestCases() { ...createDonationData(), amount: nonQfDonationAmount, status: DONATION_STATUS.VERIFIED, + earlyAccessRoundId: earlyAccessRound.id, }, user.id, project.id, @@ -547,7 +556,7 @@ function qAccStatTestCases() { ...createDonationData(), amount: qfDonationAmount, status: DONATION_STATUS.VERIFIED, - qfRoundId: qfRound1.id, + qfRoundId: qfRound.id, }, user.id, project.id, @@ -568,10 +577,15 @@ 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') @@ -579,6 +593,13 @@ function qAccStatTestCases() { .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, { diff --git a/src/services/qAccService.ts b/src/services/qAccService.ts index 97c3d3155..0960a618f 100644 --- a/src/services/qAccService.ts +++ b/src/services/qAccService.ts @@ -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'; @@ -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, }; };