Skip to content

Commit

Permalink
Merge branch 'master' into update-recurring-donation-tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
divine-comedian authored Nov 26, 2024
2 parents 69d0bb1 + 37c81c7 commit 5f1b756
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/repositories/recurringDonationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export const updateRecurringDonationFromTheStreamDonations = async (
SELECT COALESCE(SUM(d."amount"), 0)
FROM donation as d
WHERE d."recurringDonationId" = $1
)
),
"updatedAt" = NOW()
WHERE "id" = $1
`,
[recurringDonationId],
Expand Down
63 changes: 34 additions & 29 deletions src/services/recurringDonationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,19 +427,20 @@ export const recurringDonationsCountPerDateRange = async (
onlyVerified?: boolean,
): Promise<number> => {
const query = RecurringDonation.createQueryBuilder('recurringDonation')
.select('COALESCE(COUNT(recurringDonation.id), 0)', 'count')
.select('COALESCE(COUNT(DISTINCT 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 @@ -475,20 +476,21 @@ export const recurringDonationsCountPerDateRangePerMonth = async (
onlyVerified?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = RecurringDonation.createQueryBuilder('recurringDonation')
.select('COUNT(recurringDonation.id)', 'total')
.select('COUNT(DISTINCT 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.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),
});
}

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.createdAt, '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.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),
});
}

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.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),
});
}

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')
.addSelect('COALESCE(COUNT(recurringDonation.id), 0)', 'total')
.select('donations.currency', 'token')
.addSelect('COALESCE(COUNT(DISTINCT 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 5f1b756

Please sign in to comment.