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

Change recurring donation stat queries totalUSD #1873

Merged
merged 2 commits into from
Nov 19, 2024
Merged
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
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
Loading