Skip to content

Commit

Permalink
Merge pull request #1704 from Giveth/fix/track_recurring_donations
Browse files Browse the repository at this point in the history
feat: add tacking recurring donations queries
  • Loading branch information
Meriem-BM authored Jul 23, 2024
2 parents b12b70c + b659c58 commit c217c1c
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 310 deletions.
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

0 comments on commit c217c1c

Please sign in to comment.