diff --git a/apps/api/src/donations/donations.controller.ts b/apps/api/src/donations/donations.controller.ts index b11b935b9..5f503e5fb 100644 --- a/apps/api/src/donations/donations.controller.ts +++ b/apps/api/src/donations/donations.controller.ts @@ -94,6 +94,18 @@ export class DonationsController { return await this.donationsService.getDonationsByUser(user.sub) } + @Get('money') + @Public() + async totalDonatedMoney() { + return this.donationsService.getTotalDonatedMoney() + } + + @Get('donors-count') + @Public() + async donorsCount() { + return await this.donationsService.getDonorsCount() + } + @Get('listPublic') @Public() @ApiQuery({ name: 'campaignId', required: false, type: String }) diff --git a/apps/api/src/donations/donations.service.ts b/apps/api/src/donations/donations.service.ts index 121864482..da943ac57 100644 --- a/apps/api/src/donations/donations.service.ts +++ b/apps/api/src/donations/donations.service.ts @@ -677,6 +677,33 @@ export class DonationsService { return user.id } + async getTotalDonatedMoney() { + const totalMoney = await this.prisma.donation.aggregate({ + _sum: { + amount: true, + }, + where: { status: DonationStatus.succeeded }, + }) + return { total: totalMoney._sum.amount } + } + + 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 + + // substract one because we don't want to include anonymousDonation again + return { count: donorsCount.length - 1 + anonymousDonations } + } + /** * @param res - Response object to be used for the export to excel file */ diff --git a/apps/api/src/vault/vault.controller.ts b/apps/api/src/vault/vault.controller.ts index fe23092b8..54745de11 100644 --- a/apps/api/src/vault/vault.controller.ts +++ b/apps/api/src/vault/vault.controller.ts @@ -16,7 +16,7 @@ import { CampaignService } from '../campaign/campaign.service' import { CreateVaultDto } from './dto/create-vault.dto' import { UpdateVaultDto } from './dto/update-vault.dto' import { KeycloakTokenParsed } from '../auth/keycloak' -import { ApiTags } from '@nestjs/swagger'; +import { ApiTags } from '@nestjs/swagger' @ApiTags('vault') @Controller('vault') diff --git a/schema.prisma b/schema.prisma index 8eefb2a28..a08810a66 100644 --- a/schema.prisma +++ b/schema.prisma @@ -1,6 +1,7 @@ generator client { provider = "prisma-client-js" binaryTargets = ["native"] + previewFeatures = ["orderByNulls"] } generator dbml {