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

feat: add recurring donations statistics queries for dashboard analytics #1721

Merged
merged 1 commit into from
Jul 24, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "giveth-graphql-api",
"version": "1.24.1",
"version": "1.24.2",
"description": "Backend GraphQL server for Giveth originally forked from Topia",
"main": "./dist/index.js",
"dependencies": {
Expand Down
238 changes: 0 additions & 238 deletions src/resolvers/recurringDonationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
updateRecurringDonationQuery,
updateRecurringDonationQueryById,
updateRecurringDonationStatusMutation,
fetchRecurringDonationStatsQuery,
} from '../../test/graphqlQueries';

describe(
Expand Down Expand Up @@ -57,11 +56,6 @@ describe(
updateRecurringDonationByIdTestCases,
);

describe(
'getRecurringDonationStatsTestCases test cases',
getRecurringDonationStatsTestCases,
);

function createRecurringDonationTestCases() {
it('should create recurringDonation successfully', async () => {
const projectOwner = await saveUserDirectlyToDb(
Expand Down Expand Up @@ -2943,235 +2937,3 @@ function updateRecurringDonationStatusTestCases() {
);
});
}

function getRecurringDonationStatsTestCases() {
const lastYear = new Date().getFullYear() - 1;
const beginDate = `${lastYear}-01-01`;
const endDate = `${lastYear}-03-01`;

it(`should return the correct stats for a given date range (${beginDate} to ${endDate})`, async () => {
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 400,
createdAt: new Date(`${lastYear}-01-02`),
},
});

await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 100,
createdAt: new Date(`${lastYear}-01-24`),
},
});

// we are querying from January 1st of last year to the 1st of March of last year
const result = await axios.post(graphqlUrl, {
query: fetchRecurringDonationStatsQuery,
variables: {
beginDate,
endDate,
},
});

const stats = result.data.data.getRecurringDonationStats;
assert.equal(stats.activeRecurringDonationsCount, 0);
assert.equal(stats.totalStreamedUsdValue, 500);
});

it(`should return the correct stats for a given date range (${beginDate} -> ${endDate}) and currency`, async () => {
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 400,
currency: 'DAI',
createdAt: new Date(`${lastYear}-01-01`),
},
});
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 100,
currency: 'USDT',
createdAt: new Date(`${lastYear}-02-01`),
},
});

const result = await axios.post(graphqlUrl, {
query: fetchRecurringDonationStatsQuery,
variables: {
beginDate,
endDate,
currency: 'USDT',
},
});

const stats = result.data.data.getRecurringDonationStats;
assert.equal(stats.activeRecurringDonationsCount, 0);
assert.equal(stats.totalStreamedUsdValue, 600);
});

it(`should return the correct stats for a given date range (${beginDate} -> ${endDate}) with correct active count`, async () => {
const donor1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const donor2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());

await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor1.id,
totalUsdStreamed: 400,
status: RECURRING_DONATION_STATUS.ACTIVE,
currency: 'DAI',
createdAt: new Date(`${lastYear}-01-01`),
},
});
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor2.id,
totalUsdStreamed: 100,
status: RECURRING_DONATION_STATUS.ACTIVE,
currency: 'DAI',
createdAt: new Date(`${lastYear}-02-01`),
},
});
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor1.id,
totalUsdStreamed: 200,
currency: 'DAI',
createdAt: new Date(`${lastYear}-02-01`),
},
});
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor2.id,
totalUsdStreamed: 100,
status: RECURRING_DONATION_STATUS.ACTIVE,
currency: 'USDT',
createdAt: new Date(`${lastYear}-02-01`),
},
});

const result = await axios.post(graphqlUrl, {
query: fetchRecurringDonationStatsQuery,
variables: {
beginDate,
endDate,
status: RECURRING_DONATION_STATUS.ACTIVE,
currency: 'DAI',
},
});

const stats = result.data.data.getRecurringDonationStats;
assert.equal(stats.activeRecurringDonationsCount, 2);
assert.equal(stats.totalStreamedUsdValue, 1100);
});

it('should return the correct stats for the given date range where beginDate', async () => {
const lastYear15thOfJanuary = new Date(`${lastYear}-01-15T09:00:00`);

const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 400,
status: RECURRING_DONATION_STATUS.ACTIVE,
createdAt: lastYear15thOfJanuary,
},
});
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 100,
createdAt: lastYear15thOfJanuary,
},
});

const result = await axios.post(graphqlUrl, {
query: fetchRecurringDonationStatsQuery,
variables: {
beginDate: `${lastYear}-01-15T09:00:00`,
endDate: `${lastYear}-01-15T09:00:00`,
},
});

const stats = result.data.data.getRecurringDonationStats;

assert.equal(stats.activeRecurringDonationsCount, 1);
assert.equal(stats.totalStreamedUsdValue, 500);
});

it(`should return empty stats for the given date range where beginDate is same as endDate`, async () => {
const result = await axios.post(graphqlUrl, {
query: fetchRecurringDonationStatsQuery,
variables: {
beginDate: `${lastYear}-04-01`,
endDate: `${lastYear}-05-01`,
},
});

const stats = result.data.data.getRecurringDonationStats;
assert.equal(stats.activeRecurringDonationsCount, 0);
assert.equal(stats.totalStreamedUsdValue, 0);
});

it('should return an error for the given an empty date range', async () => {
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 400,
status: RECURRING_DONATION_STATUS.ACTIVE,
createdAt: new Date(),
},
});
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 100,
createdAt: new Date(),
},
});

const result = await axios.post(graphqlUrl, {
query: fetchRecurringDonationStatsQuery,
variables: {
beginDate: '',
endDate: '',
},
});

assert.isNotNull(result.data.errors);
});

it('should return an error for the given an invalid date range', async () => {
const donor = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 400,
status: RECURRING_DONATION_STATUS.ACTIVE,
createdAt: new Date(),
},
});
await saveRecurringDonationDirectlyToDb({
donationData: {
donorId: donor.id,
totalUsdStreamed: 100,
createdAt: new Date(),
},
});

const result = await axios.post(graphqlUrl, {
query: fetchRecurringDonationStatsQuery,
variables: {
beginDate: 'invalid date',
endDate: 'invalid date',
},
});

assert.isNotNull(result.data.errors);
});
}
Loading
Loading