Skip to content

Commit

Permalink
src/donations: Fix wrong unique donors count
Browse files Browse the repository at this point in the history
Currently when we count unique donors we group them by billing_name, which gives us the total number of unique donors.
The number of total donors is then added, to the total amount of donations made by the first user in the returned query. This results in overestimating the actually count of unique donors.

Changed the query to plain SQL, as prisma doesn't yet support counting by distinct properties, and counting is relatively faster to process, than grouping or searching
Changed the query to count by distinct billing_email property, rather than billing_name, as email is the "more unique" property of the two.
  • Loading branch information
sashko9807 committed Dec 7, 2023
1 parent 3a41008 commit 48c5496
Showing 1 changed file with 6 additions and 14 deletions.
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<{
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

0 comments on commit 48c5496

Please sign in to comment.