2025-12-08 16:31:30 +03:00

53 lines
1.5 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { redis } from '@/lib/redis'
import { getAllPolls } from '@/lib/polls'
import { isAdmin } from '@/lib/auth/admin'
export async function GET(req: NextRequest) {
try {
const fid = req.headers.get('x-farcaster-fid')
if (!fid || !isAdmin(fid)) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const allUsers = await redis.keys('user:*')
const totalUsers = allUsers.length
const polls = await getAllPolls()
const totalPolls = polls.length
const activePolls = polls.filter(p => p.status === 'active').length
let totalVotes = 0
let totalVolume = 0
const now = Date.now()
const oneDayAgo = now - 24 * 60 * 60 * 1000
let activeUsers24h = 0
for (const poll of polls) {
totalVotes += poll.totalVotes
totalVolume += poll.totalAmount
}
for (const userKey of allUsers) {
const votes = await redis.lrange(userKey.replace('user:', 'votes:'), 0, -1)
const recentVotes = votes.filter(v => {
const vote = JSON.parse(v)
return vote.timestamp > oneDayAgo
})
if (recentVotes.length > 0) activeUsers24h++
}
return NextResponse.json({
totalUsers,
totalPolls,
totalVotes,
totalVolume,
activeUsers24h,
activePolls
})
} catch (error) {
console.error('Admin stats error:', error)
return NextResponse.json({ error: 'Failed to fetch stats' }, { status: 500 })
}
}