29 lines
866 B
TypeScript
29 lines
866 B
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getAllPolls } from '@/lib/polls'
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const polls = await getAllPolls()
|
|
const now = Date.now()
|
|
const oneHourAgo = now - 3600000
|
|
|
|
const trending = polls
|
|
.filter(p => p.status === 'active')
|
|
.map(poll => {
|
|
const recentVotes = poll.totalVotes
|
|
const ageHours = (now - poll.createdAt) / 3600000
|
|
const velocity = recentVotes / Math.max(ageHours, 0.1)
|
|
const score = velocity * poll.totalAmount
|
|
|
|
return { ...poll, trendingScore: score }
|
|
})
|
|
.sort((a, b) => b.trendingScore - a.trendingScore)
|
|
.slice(0, 20)
|
|
|
|
return NextResponse.json({ polls: trending })
|
|
} catch (error) {
|
|
console.error('Trending API error:', error)
|
|
return NextResponse.json({ polls: [] })
|
|
}
|
|
}
|