Skip to content

Commit

Permalink
Change recurring donation stat queries totalUSD (#1873)
Browse files Browse the repository at this point in the history
* change recurring donation stat queries totalUSD

* fix count for giveth stats recurring donations
  • Loading branch information
CarlosQ96 authored Nov 19, 2024
1 parent 6dc5c77 commit d30ad0f
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions src/services/recurringDonationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,18 +428,19 @@ export const recurringDonationsCountPerDateRange = async (
): Promise<number> => {
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),
});
}
Expand Down Expand Up @@ -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),
});
}
Expand Down Expand Up @@ -524,24 +526,24 @@ export const recurringDonationsStreamedCUsdTotal = async (
networkId?: number,
onlyVerified?: boolean,
): Promise<number> => {
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,
});
}
Expand Down Expand Up @@ -571,23 +573,24 @@ export const recurringDonationsStreamedCUsdTotalPerMonth = async (
onlyVerified?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
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,
});
}
Expand Down Expand Up @@ -621,25 +624,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,
});
}
Expand Down Expand Up @@ -670,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),
});
}
Expand Down

0 comments on commit d30ad0f

Please sign in to comment.