diff --git a/src/modules/admin-dashboard/admin-dashboard.service.ts b/src/modules/admin-dashboard/admin-dashboard.service.ts index 79600c2..f0acec6 100644 --- a/src/modules/admin-dashboard/admin-dashboard.service.ts +++ b/src/modules/admin-dashboard/admin-dashboard.service.ts @@ -11,6 +11,8 @@ export class AdminDashboardService { const tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); + const safe = async (fn: () => Promise, fallback: T): Promise => { try { return await fn() } catch { return fallback } }; + const [ totalUsers, newUsersToday, @@ -21,24 +23,14 @@ export class AdminDashboardService { totalFiles, storageAgg, ] = await Promise.all([ - this.prisma.user.count({ where: { deletedAt: null } }), - this.prisma.user.count({ - where: { createdAt: { gte: today, lt: tomorrow }, deletedAt: null }, - }), - this.prisma.dailyLearningActivity.count({ - where: { activityDate: { gte: today, lt: tomorrow } }, - }), - this.prisma.knowledgeBase.count({ where: { deletedAt: null } }), - this.prisma.knowledgeBase.count({ - where: { createdAt: { gte: today, lt: tomorrow }, deletedAt: null }, - }), - this.prisma.aiUsageLog.count({ - where: { createdAt: { gte: today, lt: tomorrow } }, - }), - this.prisma.uploadedFile.count(), - this.prisma.uploadedFile.aggregate({ - _sum: { sizeBytes: true }, - }), + safe(() => this.prisma.user.count({ where: { deletedAt: null } }), 0), + safe(() => this.prisma.user.count({ where: { createdAt: { gte: today, lt: tomorrow }, deletedAt: null } }), 0), + safe(() => this.prisma.dailyLearningActivity.count({ where: { activityDate: { gte: today, lt: tomorrow } } }), 0), + safe(() => this.prisma.knowledgeBase.count({ where: { deletedAt: null } }), 0), + safe(() => this.prisma.knowledgeBase.count({ where: { createdAt: { gte: today, lt: tomorrow }, deletedAt: null } }), 0), + safe(() => this.prisma.aiUsageLog.count({ where: { createdAt: { gte: today, lt: tomorrow } } }), 0), + safe(() => this.prisma.uploadedFile.count(), 0), + safe(() => this.prisma.uploadedFile.aggregate({ _sum: { sizeBytes: true } }), { _sum: { sizeBytes: 0 } }), ]); const userTrend = await this.getUserTrend(30);