|
1 | | -import { redirect } from "next/navigation"; |
| 1 | +import prisma from "@/lib/prisma"; |
| 2 | +import { AdminDashboard } from "@/components/admin/admin-dashboard"; |
2 | 3 |
|
3 | | -export default function AdminPage() { |
4 | | - redirect("/admin/styles"); |
| 4 | +export default async function AdminPage() { |
| 5 | + const now = new Date(); |
| 6 | + const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); |
| 7 | + const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); |
| 8 | + |
| 9 | + const [ |
| 10 | + totalUsers, |
| 11 | + newUsers30d, |
| 12 | + totalWorkspaces, |
| 13 | + activeWorkspaces, |
| 14 | + totalQuotes, |
| 15 | + quotes30d, |
| 16 | + completedQuotes, |
| 17 | + failedQuotes, |
| 18 | + totalImages, |
| 19 | + images30d, |
| 20 | + subscriptionsByTier, |
| 21 | + totalChannels, |
| 22 | + activeChannels, |
| 23 | + newContactSubmissions, |
| 24 | + recentQuotes, |
| 25 | + recentUsers, |
| 26 | + tokenPurchaseRevenue, |
| 27 | + weeklyActiveWorkspaceIds, |
| 28 | + ] = await Promise.all([ |
| 29 | + prisma.user.count(), |
| 30 | + prisma.user.count({ where: { createdAt: { gte: thirtyDaysAgo } } }), |
| 31 | + prisma.workspace.count(), |
| 32 | + prisma.workspace.count({ where: { isActive: true } }), |
| 33 | + prisma.quote.count(), |
| 34 | + prisma.quote.count({ where: { createdAt: { gte: thirtyDaysAgo } } }), |
| 35 | + prisma.quote.count({ where: { status: "COMPLETED" } }), |
| 36 | + prisma.quote.count({ where: { status: "FAILED" } }), |
| 37 | + prisma.imageGeneration.count({ where: { status: "COMPLETED" } }), |
| 38 | + prisma.imageGeneration.count({ |
| 39 | + where: { status: "COMPLETED", createdAt: { gte: thirtyDaysAgo } }, |
| 40 | + }), |
| 41 | + prisma.subscription.groupBy({ |
| 42 | + by: ["tier"], |
| 43 | + _count: { id: true }, |
| 44 | + }), |
| 45 | + prisma.channel.count(), |
| 46 | + prisma.channel.count({ where: { isActive: true } }), |
| 47 | + prisma.contactFormSubmission.count({ where: { status: "new" } }), |
| 48 | + prisma.quote.findMany({ |
| 49 | + where: { status: "COMPLETED" }, |
| 50 | + orderBy: { createdAt: "desc" }, |
| 51 | + take: 5, |
| 52 | + select: { |
| 53 | + id: true, |
| 54 | + quoteText: true, |
| 55 | + imageUrl: true, |
| 56 | + slackUserName: true, |
| 57 | + createdAt: true, |
| 58 | + workspace: { select: { slackTeamName: true } }, |
| 59 | + }, |
| 60 | + }), |
| 61 | + prisma.user.findMany({ |
| 62 | + orderBy: { createdAt: "desc" }, |
| 63 | + take: 5, |
| 64 | + select: { |
| 65 | + id: true, |
| 66 | + name: true, |
| 67 | + email: true, |
| 68 | + createdAt: true, |
| 69 | + }, |
| 70 | + }), |
| 71 | + prisma.tokenPurchase.aggregate({ |
| 72 | + _sum: { amountPaid: true }, |
| 73 | + }), |
| 74 | + prisma.quote.findMany({ |
| 75 | + where: { createdAt: { gte: sevenDaysAgo } }, |
| 76 | + select: { workspaceId: true }, |
| 77 | + distinct: ["workspaceId"], |
| 78 | + }), |
| 79 | + ]); |
| 80 | + |
| 81 | + const tierBreakdown: Record<string, number> = {}; |
| 82 | + for (const tier of subscriptionsByTier) { |
| 83 | + tierBreakdown[tier.tier] = tier._count.id; |
| 84 | + } |
| 85 | + |
| 86 | + const paidSubscriptions = |
| 87 | + (tierBreakdown["STARTER"] || 0) + |
| 88 | + (tierBreakdown["TEAM"] || 0) + |
| 89 | + (tierBreakdown["BUSINESS"] || 0) + |
| 90 | + (tierBreakdown["ENTERPRISE"] || 0); |
| 91 | + |
| 92 | + const totalSubscriptions = Object.values(tierBreakdown).reduce( |
| 93 | + (a, b) => a + b, |
| 94 | + 0, |
| 95 | + ); |
| 96 | + |
| 97 | + const conversionRate = |
| 98 | + totalSubscriptions > 0 |
| 99 | + ? parseFloat(((paidSubscriptions / totalSubscriptions) * 100).toFixed(1)) |
| 100 | + : 0; |
| 101 | + |
| 102 | + const quoteSuccessRate = |
| 103 | + completedQuotes + failedQuotes > 0 |
| 104 | + ? parseFloat( |
| 105 | + ((completedQuotes / (completedQuotes + failedQuotes)) * 100).toFixed( |
| 106 | + 1, |
| 107 | + ), |
| 108 | + ) |
| 109 | + : 100; |
| 110 | + |
| 111 | + return ( |
| 112 | + <AdminDashboard |
| 113 | + stats={{ |
| 114 | + totalUsers, |
| 115 | + newUsers30d, |
| 116 | + totalWorkspaces, |
| 117 | + activeWorkspaces, |
| 118 | + weeklyActiveWorkspaces: weeklyActiveWorkspaceIds.length, |
| 119 | + totalQuotes, |
| 120 | + quotes30d, |
| 121 | + totalImages, |
| 122 | + images30d, |
| 123 | + totalChannels, |
| 124 | + activeChannels, |
| 125 | + newContactSubmissions, |
| 126 | + paidSubscriptions, |
| 127 | + freeSubscriptions: tierBreakdown["FREE"] || 0, |
| 128 | + conversionRate, |
| 129 | + quoteSuccessRate, |
| 130 | + completedQuotes, |
| 131 | + failedQuotes, |
| 132 | + totalTokenRevenue: (tokenPurchaseRevenue._sum.amountPaid || 0) / 100, |
| 133 | + tierBreakdown, |
| 134 | + }} |
| 135 | + recentQuotes={recentQuotes.map((q) => ({ |
| 136 | + ...q, |
| 137 | + createdAt: q.createdAt.toISOString(), |
| 138 | + }))} |
| 139 | + recentUsers={recentUsers.map((u) => ({ |
| 140 | + ...u, |
| 141 | + createdAt: u.createdAt.toISOString(), |
| 142 | + }))} |
| 143 | + /> |
| 144 | + ); |
5 | 145 | } |
0 commit comments