|
| 1 | +import { NextResponse } from "next/server" |
| 2 | +import { createClient } from "@/lib/supabase/server" |
| 3 | + |
| 4 | +export async function GET() { |
| 5 | + try { |
| 6 | + const supabase = await createClient() |
| 7 | + |
| 8 | + // Check if user is admin |
| 9 | + const { data: { user } } = await supabase.auth.getUser() |
| 10 | + |
| 11 | + if (!user) { |
| 12 | + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }) |
| 13 | + } |
| 14 | + |
| 15 | + const { data: profile } = await supabase |
| 16 | + .from("profiles") |
| 17 | + .select("is_admin") |
| 18 | + .eq("id", user.id) |
| 19 | + .single() |
| 20 | + |
| 21 | + if (!profile?.is_admin) { |
| 22 | + return NextResponse.json({ error: "Forbidden" }, { status: 403 }) |
| 23 | + } |
| 24 | + |
| 25 | + const activities: Array<{ |
| 26 | + type: string |
| 27 | + message: string |
| 28 | + timestamp: string |
| 29 | + status: string |
| 30 | + created_at: string |
| 31 | + }> = [] |
| 32 | + |
| 33 | + // Fetch recent user signups (last 7 days to ensure we have data) |
| 34 | + const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString() |
| 35 | + |
| 36 | + const { data: recentUsers } = await supabase |
| 37 | + .from("profiles") |
| 38 | + .select("email, created_at") |
| 39 | + .gte("created_at", sevenDaysAgo) |
| 40 | + .order("created_at", { ascending: false }) |
| 41 | + .limit(3) |
| 42 | + |
| 43 | + if (recentUsers) { |
| 44 | + recentUsers.forEach(user => { |
| 45 | + activities.push({ |
| 46 | + type: "user_signup", |
| 47 | + message: `New user registered: ${user.email}`, |
| 48 | + timestamp: getTimeAgo(user.created_at), |
| 49 | + status: "success", |
| 50 | + created_at: user.created_at |
| 51 | + }) |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + // Fetch recent blog posts |
| 56 | + const { data: recentBlogs } = await supabase |
| 57 | + .from("blogs") |
| 58 | + .select("title, author, date") |
| 59 | + .order("date", { ascending: false }) |
| 60 | + .limit(3) |
| 61 | + |
| 62 | + if (recentBlogs) { |
| 63 | + recentBlogs.forEach(blog => { |
| 64 | + activities.push({ |
| 65 | + type: "blog_published", |
| 66 | + message: `Blog post "${blog.title}" published by ${blog.author}`, |
| 67 | + timestamp: getTimeAgo(blog.date), |
| 68 | + status: "success", |
| 69 | + created_at: blog.date |
| 70 | + }) |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + // Fetch recent events |
| 75 | + const { data: recentEvents } = await supabase |
| 76 | + .from("events") |
| 77 | + .select("title, created_at") |
| 78 | + .order("created_at", { ascending: false }) |
| 79 | + .limit(2) |
| 80 | + |
| 81 | + if (recentEvents) { |
| 82 | + recentEvents.forEach(event => { |
| 83 | + activities.push({ |
| 84 | + type: "event_created", |
| 85 | + message: `New event "${event.title}" created`, |
| 86 | + timestamp: getTimeAgo(event.created_at), |
| 87 | + status: "info", |
| 88 | + created_at: event.created_at |
| 89 | + }) |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + // Fetch recent hackathons |
| 94 | + const { data: recentHackathons } = await supabase |
| 95 | + .from("hackathons") |
| 96 | + .select("title, created_at") |
| 97 | + .order("created_at", { ascending: false }) |
| 98 | + .limit(2) |
| 99 | + |
| 100 | + if (recentHackathons) { |
| 101 | + recentHackathons.forEach(hackathon => { |
| 102 | + activities.push({ |
| 103 | + type: "event_created", |
| 104 | + message: `New hackathon "${hackathon.title}" created`, |
| 105 | + timestamp: getTimeAgo(hackathon.created_at), |
| 106 | + status: "info", |
| 107 | + created_at: hackathon.created_at |
| 108 | + }) |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + // Sort all activities by timestamp and limit to 5 |
| 113 | + activities.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) |
| 114 | + const topActivities = activities.slice(0, 5).map((activity, index) => ({ |
| 115 | + id: index + 1, |
| 116 | + type: activity.type, |
| 117 | + message: activity.message, |
| 118 | + timestamp: activity.timestamp, |
| 119 | + status: activity.status |
| 120 | + })) |
| 121 | + |
| 122 | + return NextResponse.json({ activities: topActivities }) |
| 123 | + } catch (error) { |
| 124 | + console.error("Recent activities error:", error) |
| 125 | + return NextResponse.json({ error: "Internal server error" }, { status: 500 }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function getTimeAgo(dateString: string): string { |
| 130 | + const date = new Date(dateString) |
| 131 | + const now = new Date() |
| 132 | + const seconds = Math.floor((now.getTime() - date.getTime()) / 1000) |
| 133 | + |
| 134 | + if (seconds < 60) return `${seconds} seconds ago` |
| 135 | + if (seconds < 3600) return `${Math.floor(seconds / 60)} minutes ago` |
| 136 | + if (seconds < 86400) return `${Math.floor(seconds / 3600)} hours ago` |
| 137 | + if (seconds < 604800) return `${Math.floor(seconds / 86400)} days ago` |
| 138 | + return `${Math.floor(seconds / 604800)} weeks ago` |
| 139 | +} |
0 commit comments