From d1cc05577d6c5734599c783b0ac97f9b2f250c00 Mon Sep 17 00:00:00 2001 From: Adam Basha <110662505+Bashamega@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:35:39 +0300 Subject: [PATCH] fix: check if there is data (#28) * fix: check if there is data * fix: remove the else * fix: remove the function * fix: use format for dates --- src/app.service.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/app.service.ts b/src/app.service.ts index 0e5ee2a..5bca0f4 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -12,10 +12,19 @@ export class AppService { constructor(private prisma: PrismaService) {} async getStats(): Promise { - return this.prisma.trending.groupBy({ + const stats = await this.prisma.trending.groupBy({ by: ['createdAt', 'type'], _count: { id: true }, }); + if (stats.length > 0) { + return stats; + } + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + + const getYesterdaysDate = format(yesterday, 'yyyy-MM-dd'); + + this.getTrending('daily', getYesterdaysDate); } async getTrending(type: string = 'daily', date: string): Promise {