From 36b6c183018c883a9ab62d4c9610b83dfd8e916b Mon Sep 17 00:00:00 2001 From: Carlos Date: Thu, 14 Nov 2024 12:18:59 +0100 Subject: [PATCH 1/2] change recurring donation stat queries totalUSD --- src/services/recurringDonationService.ts | 38 +++++++++++++----------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/services/recurringDonationService.ts b/src/services/recurringDonationService.ts index 5241fd2c9..f1b41ba45 100644 --- a/src/services/recurringDonationService.ts +++ b/src/services/recurringDonationService.ts @@ -524,24 +524,24 @@ export const recurringDonationsStreamedCUsdTotal = async ( networkId?: number, onlyVerified?: boolean, ): Promise => { - const query = RecurringDonation.createQueryBuilder( - 'recurringDonation', - ).select('COALESCE(SUM(recurringDonation.totalUsdStreamed), 0)', 'total'); + const query = RecurringDonation.createQueryBuilder('recurringDonation') + .select('COALESCE(SUM(donations.valueUsd), 0)', 'total') + .innerJoin('recurringDonation.donations', 'donations'); if (fromDate) { - query.andWhere('recurringDonation.updatedAt >= :fromDate', { + query.andWhere('donations.createdAt >= :fromDate', { fromDate: new Date(fromDate), }); } if (toDate) { - query.andWhere('recurringDonation.updatedAt <= :toDate', { + query.andWhere('donations.createdAt <= :toDate', { toDate: new Date(toDate), }); } if (networkId) { - query.andWhere('recurringDonation.networkId = :networkId', { + query.andWhere('donations.transactionNetworkId = :networkId', { networkId, }); } @@ -571,23 +571,24 @@ export const recurringDonationsStreamedCUsdTotalPerMonth = async ( onlyVerified?: boolean, ): Promise => { const query = RecurringDonation.createQueryBuilder('recurringDonation') - .select('SUM(recurringDonation.totalUsdStreamed)', 'total') - .addSelect("TO_CHAR(recurringDonation.updatedAt, 'YYYY/MM')", 'date'); + .select('SUM(donations.valueUsd)', 'total') + .addSelect("TO_CHAR(donations.createdAt, 'YYYY/MM')", 'date') + .innerJoin('recurringDonation.donations', 'donations'); if (fromDate) { - query.andWhere('recurringDonation.updatedAt >= :fromDate', { + query.andWhere('donations.createdAt >= :fromDate', { fromDate: new Date(fromDate), }); } if (toDate) { - query.andWhere('recurringDonation.updatedAt <= :toDate', { + query.andWhere('donations.createdAt <= :toDate', { toDate: new Date(toDate), }); } if (networkId) { - query.andWhere('recurringDonation.networkId = :networkId', { + query.andWhere('donations.transactionNetworkId = :networkId', { networkId, }); } @@ -621,25 +622,26 @@ export const recurringDonationsTotalPerToken = async (params: { }): Promise<{ token: string; total: number }[]> => { const { fromDate, toDate, networkId, onlyVerified } = params; const query = RecurringDonation.createQueryBuilder('recurringDonation') - .select('recurringDonation.currency', 'token') - .addSelect('COALESCE(SUM(recurringDonation.totalUsdStreamed), 0)', 'total') - .groupBy('recurringDonation.currency') - .having('SUM(recurringDonation.totalUsdStreamed) > 0'); + .select('donations.currency', 'token') + .addSelect('COALESCE(SUM(donations.valueUsd), 0)', 'total') + .innerJoin('recurringDonation.donations', 'donations') + .groupBy('donations.currency') + .having('SUM(donations.valueUsd) > 0'); if (fromDate) { - query.andWhere('recurringDonation.updatedAt >= :fromDate', { + query.andWhere('donations.createdAt >= :fromDate', { fromDate: new Date(fromDate), }); } if (toDate) { - query.andWhere('recurringDonation.updatedAt <= :toDate', { + query.andWhere('donations.createdAt <= :toDate', { toDate: new Date(toDate), }); } if (networkId) { - query.andWhere('recurringDonation.networkId = :networkId', { + query.andWhere('donations.transactionNetworkId = :networkId', { networkId, }); } From 77f0f378547932302d07dec6403819da31cc6cc8 Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 19 Nov 2024 17:57:35 +0100 Subject: [PATCH 2/2] fix count for giveth stats recurring donations --- src/services/recurringDonationService.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/services/recurringDonationService.ts b/src/services/recurringDonationService.ts index f1b41ba45..3f800ed11 100644 --- a/src/services/recurringDonationService.ts +++ b/src/services/recurringDonationService.ts @@ -428,18 +428,19 @@ export const recurringDonationsCountPerDateRange = async ( ): Promise => { const query = RecurringDonation.createQueryBuilder('recurringDonation') .select('COALESCE(COUNT(recurringDonation.id), 0)', 'count') + .innerJoin('recurringDonation.donations', 'donations') .where('recurringDonation.status != :status', { status: RECURRING_DONATION_STATUS.FAILED, }); if (fromDate) { - query.andWhere('recurringDonation.createdAt >= :fromDate', { + query.andWhere('donations.createdAt >= :fromDate', { fromDate: new Date(fromDate), }); } if (toDate) { - query.andWhere('recurringDonation.createdAt <= :toDate', { + query.andWhere('donations.createdAt <= :toDate', { toDate: new Date(toDate), }); } @@ -477,18 +478,19 @@ export const recurringDonationsCountPerDateRangePerMonth = async ( const query = RecurringDonation.createQueryBuilder('recurringDonation') .select('COUNT(recurringDonation.id)', 'total') .addSelect("TO_CHAR(recurringDonation.createdAt, 'YYYY/MM')", 'date') + .innerJoin('recurringDonation.donations', 'donations') .where('recurringDonation.status != :status', { status: RECURRING_DONATION_STATUS.FAILED, }); if (fromDate) { - query.andWhere('recurringDonation.createdAt >= :fromDate', { + query.andWhere('donations.createdAt >= :fromDate', { fromDate: new Date(fromDate), }); } if (toDate) { - query.andWhere('recurringDonation.createdAt <= :toDate', { + query.andWhere('donations.createdAt <= :toDate', { toDate: new Date(toDate), }); } @@ -672,22 +674,23 @@ export const recurringDonationsCountPerToken = async (params: { }): Promise<{ token: string; total: number }[]> => { const { fromDate, toDate, networkId, onlyVerified } = params; const query = RecurringDonation.createQueryBuilder('recurringDonation') - .select('recurringDonation.currency', 'token') + .select('donations.currency', 'token') .addSelect('COALESCE(COUNT(recurringDonation.id), 0)', 'total') + .innerJoin('recurringDonation.donations', 'donations') .where('recurringDonation.status != :status', { status: RECURRING_DONATION_STATUS.FAILED, }) - .groupBy('recurringDonation.currency') + .groupBy('donations.currency') .having('COUNT(recurringDonation.id) > 0'); if (fromDate) { - query.andWhere('recurringDonation.createdAt >= :fromDate', { + query.andWhere('donations.createdAt >= :fromDate', { fromDate: new Date(fromDate), }); } if (toDate) { - query.andWhere('recurringDonation.createdAt <= :toDate', { + query.andWhere('donations.createdAt <= :toDate', { toDate: new Date(toDate), }); }