Skip to content

Commit 5c08d16

Browse files
authored
fix: Correctly cast statistics response before sending them back to FE (#697)
1 parent 8a493ae commit 5c08d16

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

apps/api/src/statistics/statistics.service.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,50 @@ export class StatisticsService {
3131
? Prisma.sql`GROUP BY DATE_TRUNC('WEEK', d.created_at)`
3232
: Prisma.sql`GROUP BY DATE_TRUNC('DAY', d.created_at)`
3333

34-
return this.prisma.$queryRaw`
34+
const result = await this.prisma.$queryRaw`
3535
SELECT COALESCE(SUM(d.amount), 0)::NUMERIC as sum, COUNT(d.id)::INTEGER as count, ${date}
3636
FROM api.donations d
3737
INNER JOIN api.payments p ON p.id = d.payment_id
3838
WHERE p.status::text = 'succeeded'
3939
${Prisma.sql`AND d.target_vault_id IN ( SELECT id from api.vaults WHERE campaign_id = ${campaignId}::uuid)`}
4040
${group}
4141
ORDER BY date ASC `
42+
43+
return (result as GroupedDonationsDto[]).map((row) => ({
44+
sum: Number(row.sum),
45+
count: Number(row.count),
46+
date: new Date(row.date),
47+
}))
4248
}
4349

4450
async listUniqueDonations(campaignId: string): Promise<UniqueDonationsDto[]> {
45-
return this.prisma.$queryRaw`
51+
const result = await this.prisma.$queryRaw`
4652
SELECT d.amount::INTEGER as amount, COUNT(d.id)::INTEGER AS count
4753
FROM api.donations d
4854
INNER JOIN api.payments p ON p.id = d.payment_id
4955
WHERE p.status::text = 'succeeded'
5056
${Prisma.sql`AND d.target_vault_id IN ( SELECT id from api.vaults WHERE campaign_id = ${campaignId}::uuid)`}
5157
GROUP BY d.amount
5258
ORDER BY amount ASC`
59+
return (result as UniqueDonationsDto[]).map((row) => ({
60+
amount: Number(row.amount),
61+
count: Number(row.count),
62+
}))
5363
}
5464

5565
async listHourlyDonations(campaignId: string): Promise<HourlyDonationsDto[]> {
56-
return this.prisma.$queryRaw`
66+
const result = await this.prisma.$queryRaw`
5767
SELECT EXTRACT(HOUR from d.created_at)::INTEGER AS hour, COUNT(d.id)::INTEGER AS count
5868
FROM api.donations d
5969
INNER JOIN api.payments p ON p.id = d.payment_id
6070
WHERE p.status::text = 'succeeded'
6171
${Prisma.sql`AND d.target_vault_id IN ( SELECT id from api.vaults WHERE campaign_id = ${campaignId}::uuid)`}
6272
GROUP BY hour
6373
ORDER BY hour ASC`
74+
75+
return (result as HourlyDonationsDto[]).map((row) => ({
76+
hour: Number(row.hour),
77+
count: Number(row.count),
78+
}))
6479
}
6580
}

0 commit comments

Comments
 (0)