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 recurring donation count #1504

Merged
merged 7 commits into from
Apr 26, 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
9 changes: 2 additions & 7 deletions src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import { ProjectVerificationForm } from './projectVerificationForm';
import { PowerBoosting } from './powerBoosting';
import { findPowerBoostingsCountByUserId } from '../repositories/powerBoostingRepository';
import { ReferredEvent } from './referredEvent';
import {
RECURRING_DONATION_STATUS,
RecurringDonation,
} from './recurringDonation';
import { RecurringDonation } from './recurringDonation';
import { NOTIFICATIONS_EVENT_NAMES } from '../analytics/analytics';

export const publicSelectionFields = [
Expand Down Expand Up @@ -224,9 +221,7 @@ export class User extends BaseEntity {
'recurring_donation',
)
.where(`recurring_donation."donorId" = :donorId`, { donorId: this.id })
.andWhere(`recurring_donation.status = :status`, {
status: RECURRING_DONATION_STATUS.ACTIVE,
})
.andWhere('recurring_donation.totalUsdStreamed > 0')
.getCount();

// Sum of both counts
Expand Down
31 changes: 25 additions & 6 deletions src/repositories/recurringDonationRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { NETWORK_IDS } from '../provider';
import { addNewAnchorAddress } from './anchorContractAddressRepository';
import {
countOfActiveRecurringDonationsByProjectId,
nonZeroRecurringDonationsByProjectId,
createNewRecurringDonation,
findRecurringDonationById,
findRecurringDonationByProjectIdAndUserIdAndCurrency,
Expand Down Expand Up @@ -256,14 +256,15 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
donor: creator,
anchorContractAddress,
flowRate: '100',
totalUsdStreamed: 1,
currency,
project,
anonymous: false,
isBatch: false,
});
recurringDonation.status = RECURRING_DONATION_STATUS.ACTIVE;
await recurringDonation.save();
const count = await countOfActiveRecurringDonationsByProjectId(project.id);
const count = await nonZeroRecurringDonationsByProjectId(project.id);
assert.equal(count, 1);
});
it('should return count correctly, when there is more than 1 active recurring donation', async () => {
Expand Down Expand Up @@ -298,6 +299,7 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
project,
anonymous: false,
isBatch: false,
totalUsdStreamed: 1,
});
recurringDonation.status = RECURRING_DONATION_STATUS.ACTIVE;
await recurringDonation.save();
Expand All @@ -312,11 +314,12 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
project,
anonymous: false,
isBatch: false,
totalUsdStreamed: 1,
});
recurringDonation2.status = RECURRING_DONATION_STATUS.ACTIVE;
await recurringDonation2.save();

const count = await countOfActiveRecurringDonationsByProjectId(project.id);
const count = await nonZeroRecurringDonationsByProjectId(project.id);
assert.equal(count, 2);
});
it('should return count correctly, when there is active and non active donations', async () => {
Expand All @@ -341,6 +344,21 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
});
const currency = 'USD';

const recurringDonation0 = await createNewRecurringDonation({
txHash: generateRandomEvmTxHash(),
networkId: NETWORK_IDS.OPTIMISTIC,
donor: creator,
anchorContractAddress,
flowRate: '100',
currency,
project,
anonymous: false,
isBatch: false,
totalUsdStreamed: 0,
});
recurringDonation0.status = RECURRING_DONATION_STATUS.ACTIVE;
await recurringDonation0.save();

const recurringDonation1 = await createNewRecurringDonation({
txHash: generateRandomEvmTxHash(),
networkId: NETWORK_IDS.OPTIMISTIC,
Expand All @@ -351,6 +369,7 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
project,
anonymous: false,
isBatch: false,
totalUsdStreamed: 1,
});
recurringDonation1.status = RECURRING_DONATION_STATUS.ACTIVE;
await recurringDonation1.save();
Expand All @@ -368,7 +387,6 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
});
recurringDonation2.status = RECURRING_DONATION_STATUS.PENDING;
await recurringDonation2.save();
await recurringDonation1.save();

const recurringDonation3 = await createNewRecurringDonation({
txHash: generateRandomEvmTxHash(),
Expand All @@ -380,6 +398,7 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
project,
anonymous: false,
isBatch: false,
totalUsdStreamed: 2,
});
recurringDonation3.status = RECURRING_DONATION_STATUS.ENDED;
await recurringDonation3.save();
Expand All @@ -398,8 +417,8 @@ function countOfActiveRecurringDonationsByProjectIdTestCases() {
recurringDonation4.status = RECURRING_DONATION_STATUS.FAILED;
await recurringDonation4.save();

const count = await countOfActiveRecurringDonationsByProjectId(project.id);
assert.equal(count, 1);
const count = await nonZeroRecurringDonationsByProjectId(project.id);
assert.equal(count, 2);
});
}

Expand Down
10 changes: 5 additions & 5 deletions src/repositories/recurringDonationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export const createNewRecurringDonation = async (params: {
currency: string;
anonymous: boolean;
isBatch: boolean;
totalUsdStreamed?: number;
}): Promise<RecurringDonation> => {
const recurringDonation = await RecurringDonation.create({
const recurringDonation = RecurringDonation.create({
project: params.project,
donor: params.donor,
anchorContractAddress: params.anchorContractAddress,
Expand All @@ -30,6 +31,7 @@ export const createNewRecurringDonation = async (params: {
flowRate: params.flowRate,
anonymous: params.anonymous,
isBatch: params.isBatch,
totalUsdStreamed: params.totalUsdStreamed,
});
return recurringDonation.save();
};
Expand Down Expand Up @@ -129,14 +131,12 @@ export const findRecurringDonationById = async (
.getOne();
};

export const countOfActiveRecurringDonationsByProjectId = async (
export const nonZeroRecurringDonationsByProjectId = async (
projectId: number,
): Promise<number> => {
return await RecurringDonation.createQueryBuilder('recurringDonation')
.where(`recurringDonation.projectId = :projectId`, { projectId })
.andWhere(`recurringDonation.status = :status`, {
status: RECURRING_DONATION_STATUS.ACTIVE,
})
.andWhere('recurringDonation.totalUsdStreamed > 0')
.getCount();
};

Expand Down
2 changes: 2 additions & 0 deletions src/resolvers/donationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,7 @@ function donationsByProjectIdTestCases() {
project,
anonymous: false,
isBatch: false,
totalUsdStreamed: 1,
});
recurringDonation.status = RECURRING_DONATION_STATUS.ACTIVE;
await recurringDonation.save();
Expand All @@ -3439,6 +3440,7 @@ function donationsByProjectIdTestCases() {
project,
anonymous: false,
isBatch: false,
totalUsdStreamed: 1,
});
recurringDonation2.status = RECURRING_DONATION_STATUS.ACTIVE;
await recurringDonation2.save();
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {
DRAFT_DONATION_STATUS,
DraftDonation,
} from '../entities/draftDonation';
import { countOfActiveRecurringDonationsByProjectId } from '../repositories/recurringDonationRepository';
import { nonZeroRecurringDonationsByProjectId } from '../repositories/recurringDonationRepository';

const draftDonationEnabled = process.env.ENABLE_DRAFT_DONATION === 'true';

Expand Down Expand Up @@ -590,7 +590,7 @@ export class DonationResolver {
}

const recurringDonationsCount =
await countOfActiveRecurringDonationsByProjectId(projectId);
await nonZeroRecurringDonationsByProjectId(projectId);

const [donations, donationsCount] = await query
.take(take)
Expand Down
6 changes: 2 additions & 4 deletions src/resolvers/userResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ function userByAddressTestCases() {
flowRate: '300',
anonymous: false,
currency: 'USDT',
totalUsdStreamed: 200,
status: RECURRING_DONATION_STATUS.PENDING,
},
});
Expand All @@ -350,7 +349,6 @@ function userByAddressTestCases() {
flowRate: '300',
anonymous: false,
currency: 'USDT',
totalUsdStreamed: 200,
status: RECURRING_DONATION_STATUS.FAILED,
},
});
Expand All @@ -377,8 +375,8 @@ function userByAddressTestCases() {
userData.walletAddress,
);
// for totalDonated we consider all recurring donations but for donationsCount we consider only active recurring donations
assert.equal(result.data.data.userByAddress.totalDonated, 800);
assert.equal(result.data.data.userByAddress.donationsCount, 1);
assert.equal(result.data.data.userByAddress.totalDonated, 400);
assert.equal(result.data.data.userByAddress.donationsCount, 2);
});
it('Get donationsCount and totalDonated correctly, when there is both recurringDonations and one time donation', async () => {
const userData = {
Expand Down
Loading