diff --git a/apps/api/src/donations/donations.service.ts b/apps/api/src/donations/donations.service.ts index 2b6795aad..ed5609543 100644 --- a/apps/api/src/donations/donations.service.ts +++ b/apps/api/src/donations/donations.service.ts @@ -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) } } /**