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

Feature/donation report queries #694

Merged
merged 5 commits into from
Oct 24, 2022
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
108 changes: 108 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 126 additions & 3 deletions src/resolvers/donationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
donationsFromWallets,
createDonationMutation,
updateDonationStatusMutation,
fetchTotalDonationsUsdAmount,
fetchTotalDonors,
fetchTotalDonationsPerCategoryPerDate,
} from '../../test/graphqlQueries';
import { NETWORK_IDS } from '../provider';
import { User } from '../entities/user';
Expand All @@ -44,11 +47,131 @@ describe('createDonation() test cases', createDonationTestCases);
describe('updateDonationStatus() test cases', updateDonationStatusTestCases);
describe('donationsToWallets() test cases', donationsToWalletsTestCases);
describe('donationsFromWallets() test cases', donationsFromWalletsTestCases);
describe('totalDonationsUsdAmount() test cases', donationsUsdAmountTestCases);
describe('totalDonorsCountPerDate() test cases', donorsCountPerDateTestCases);
describe(
'totalDonationsPerCategoryPerDate() test cases',
totalDonationsPerCategoryPerDateTestCases,
);

// describe('tokens() test cases', tokensTestCases);
// // describe('tokens() test cases', tokensTestCases);

// TODO I think we can delete addUserVerification query
// describe('addUserVerification() test cases', addUserVerificationTestCases);
// // TODO I think we can delete addUserVerification query
// // describe('addUserVerification() test cases', addUserVerificationTestCases);

function totalDonationsPerCategoryPerDateTestCases() {
it('should return donations count per category per time range', async () => {
const donationsResponse = await axios.post(graphqlUrl, {
query: fetchTotalDonationsPerCategoryPerDate,
});
const foodDonationsTotalUsd = await Donation.createQueryBuilder('donation')
.select('COALESCE(SUM(donation."valueUsd")) AS sum')
.where(`donation.status = 'verified'`)
.getRawMany();

assert.isOk(donationsResponse);

const foodDonationsResponseTotal =
donationsResponse.data.data.totalDonationsPerCategory.find(
d => d.title === 'food',
);
assert.equal(
foodDonationsResponseTotal.totalUsd,
foodDonationsTotalUsd[0].sum,
);
});
}

function donorsCountPerDateTestCases() {
it('should return donors unique total count in a time range', async () => {
const project = await saveProjectDirectlyToDb(createProjectData());
const walletAddress = generateRandomEtheriumAddress();
const user = await saveUserDirectlyToDb(walletAddress);
// should count as 1 as its the same user
const donation = await saveDonationDirectlyToDb(
createDonationData({
status: DONATION_STATUS.VERIFIED,
createdAt: moment().add(50, 'days').toDate(),
valueUsd: 20,
}),
user.id,
project.id,
);
const donation2 = await saveDonationDirectlyToDb(
createDonationData({
status: DONATION_STATUS.VERIFIED,
createdAt: moment().add(50, 'days').toDate(),
valueUsd: 20,
}),
user.id,
project.id,
);

// anonymous donations count as separate
const anonymousDonation1 = await saveDonationDirectlyToDb(
createDonationData({
status: DONATION_STATUS.VERIFIED,
createdAt: moment().add(50, 'days').toDate(),
valueUsd: 20,
anonymous: true,
}),
undefined,
project.id,
);
const anonymousDonation2 = await saveDonationDirectlyToDb(
createDonationData({
status: DONATION_STATUS.VERIFIED,
createdAt: moment().add(50, 'days').toDate(),
valueUsd: 20,
anonymous: true,
}),
undefined,
project.id,
);

const donationsResponse = await axios.post(graphqlUrl, {
query: fetchTotalDonors,
variables: {
fromDate: moment().add(49, 'days').toDate().toISOString().split('T')[0],
toDate: moment().add(51, 'days').toDate().toISOString().split('T')[0],
},
});
assert.isOk(donationsResponse);
// 1 unique donor and 2 anonymous
assert.equal(donationsResponse.data.data.totalDonorsCountPerDate, 3);
});
}

function donationsUsdAmountTestCases() {
it('should return total usd amount for donations made in a time range', async () => {
const project = await saveProjectDirectlyToDb(createProjectData());
const walletAddress = generateRandomEtheriumAddress();
const user = await saveUserDirectlyToDb(walletAddress);
const donation = await saveDonationDirectlyToDb(
createDonationData({
status: DONATION_STATUS.VERIFIED,
createdAt: moment().add(100, 'days').toDate(),
valueUsd: 20,
}),
user.id,
project.id,
);

const donationsResponse = await axios.post(graphqlUrl, {
query: fetchTotalDonationsUsdAmount,
variables: {
fromDate: moment().add(99, 'days').toDate().toISOString().split('T')[0],
toDate: moment().add(101, 'days').toDate().toISOString().split('T')[0],
},
});

assert.isOk(donationsResponse);
assert.equal(
donationsResponse.data.data.donationsTotalUsdPerDate,
donation.valueUsd,
);
});
}

function donationsTestCases() {
it('should throw error if send invalid fromDate format', async () => {
Expand Down
Loading