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

src/donations: Fix wrong unique donors count #581

Merged
merged 1 commit into from
Dec 12, 2023
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
20 changes: 6 additions & 14 deletions apps/api/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,22 +770,14 @@ export class DonationsService {
}

async getDonorsCount() {
const donorsCount = await this.prisma.donation.groupBy({
by: ['billingName'],
where: { status: DonationStatus.succeeded },
_count: {
_all: true,
},
orderBy: { billingName: { sort: 'asc', nulls: 'first' } },
})

// get count of the donations with billingName == null
const anonymousDonations = donorsCount[0]._count._all
const donorsCount = await this.prisma.$queryRaw<{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queryRaw - this is usefull to learn :)

count: number
}>`SELECT COUNT (*) FROM (SELECT DISTINCT billing_email FROM donations WHERE status::text=${DonationStatus.succeeded}) AS unique_donors`

const totalCount = donorsCount.length - 1 + anonymousDonations
//Return result is BigInt which can't be serialized. Convert result to string first.
const uniqueDonors = donorsCount[0].count.toString()

// substract one because we don't want to include anonymousDonation again
return { count: totalCount }
return { count: Number(uniqueDonors) }
}

/**
Expand Down
Loading