|
| 1 | +import { and, eq, gte, sql } from 'drizzle-orm' |
| 2 | +import { defineField } from 'nitro-graphql/utils/define' |
| 3 | + |
| 4 | +export const appFieldsResolver = defineField({ |
| 5 | + App: { |
| 6 | + devices: { |
| 7 | + resolve: async (parent, _args, { context }) => { |
| 8 | + const { dataloaders } = context |
| 9 | + return await dataloaders.devicesByAppLoader.load(parent.id) |
| 10 | + }, |
| 11 | + }, |
| 12 | + |
| 13 | + notifications: { |
| 14 | + resolve: async (parent, _args, { context }) => { |
| 15 | + const { dataloaders } = context |
| 16 | + return await dataloaders.notificationsByAppLoader.load(parent.id) |
| 17 | + }, |
| 18 | + }, |
| 19 | + |
| 20 | + apiKeys: { |
| 21 | + resolve: async (parent, _args, { context }) => { |
| 22 | + const { dataloaders } = context |
| 23 | + return await dataloaders.apiKeysByAppLoader.load(parent.id) |
| 24 | + }, |
| 25 | + }, |
| 26 | + |
| 27 | + stats: { |
| 28 | + resolve: async (parent, _args, { context }) => { |
| 29 | + const { useDatabase, tables } = context |
| 30 | + const db = useDatabase() |
| 31 | + |
| 32 | + const today = new Date() |
| 33 | + today.setHours(0, 0, 0, 0) |
| 34 | + |
| 35 | + // Get total devices |
| 36 | + const totalDevicesResult = await db |
| 37 | + .select({ count: sql<number>`count(*)` }) |
| 38 | + .from(tables.device) |
| 39 | + .where(eq(tables.device.appId, parent.id)) |
| 40 | + |
| 41 | + // Get active devices |
| 42 | + const activeDevicesResult = await db |
| 43 | + .select({ count: sql<number>`count(*)` }) |
| 44 | + .from(tables.device) |
| 45 | + .where( |
| 46 | + and( |
| 47 | + eq(tables.device.appId, parent.id), |
| 48 | + eq(tables.device.status, 'ACTIVE'), |
| 49 | + ), |
| 50 | + ) |
| 51 | + |
| 52 | + // Get new devices today |
| 53 | + const newDevicesTodayResult = await db |
| 54 | + .select({ count: sql<number>`count(*)` }) |
| 55 | + .from(tables.device) |
| 56 | + .where( |
| 57 | + and( |
| 58 | + eq(tables.device.appId, parent.id), |
| 59 | + gte(tables.device.createdAt, today.toISOString()), |
| 60 | + ), |
| 61 | + ) |
| 62 | + |
| 63 | + // Get sent notifications today |
| 64 | + const sentTodayResult = await db |
| 65 | + .select({ count: sql<number>`count(*)` }) |
| 66 | + .from(tables.notification) |
| 67 | + .where( |
| 68 | + and( |
| 69 | + eq(tables.notification.appId, parent.id), |
| 70 | + gte(tables.notification.sentAt, today.toISOString()), |
| 71 | + ), |
| 72 | + ) |
| 73 | + |
| 74 | + // Get delivery stats for rate calculation |
| 75 | + const deliveryStatsResult = await db |
| 76 | + .select({ |
| 77 | + totalSent: sql<number>`sum(${tables.notification.totalSent})`, |
| 78 | + totalDelivered: sql<number>`sum(${tables.notification.totalDelivered})`, |
| 79 | + }) |
| 80 | + .from(tables.notification) |
| 81 | + .where(eq(tables.notification.appId, parent.id)) |
| 82 | + |
| 83 | + const totalSent = deliveryStatsResult[0]?.totalSent || 0 |
| 84 | + const totalDelivered = deliveryStatsResult[0]?.totalDelivered || 0 |
| 85 | + const deliveryRate = totalSent > 0 ? (totalDelivered / totalSent) * 100 : 0 |
| 86 | + |
| 87 | + return { |
| 88 | + totalDevices: Number(totalDevicesResult[0]?.count) || 0, |
| 89 | + activeDevices: Number(activeDevicesResult[0]?.count) || 0, |
| 90 | + newDevicesToday: Number(newDevicesTodayResult[0]?.count) || 0, |
| 91 | + sentToday: Number(sentTodayResult[0]?.count) || 0, |
| 92 | + deliveryRate, |
| 93 | + apiCalls: 0, // TODO: Implement API call tracking |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | +}) |
0 commit comments