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

34 lines
1.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { getDb } from '@/lib/db'
import { userStats } from '@/lib/db/schema'
import { sql } from 'drizzle-orm'
export async function GET(req: NextRequest) {
try {
const authHeader = req.headers.get('authorization')
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const db = getDb(process.env.DATABASE_URL!)
// Update streaks for users who voted yesterday
await db.execute(sql`
UPDATE user_stats
SET
current_streak = CASE
WHEN last_vote_date::date = CURRENT_DATE - INTERVAL '1 day' THEN current_streak + 1
WHEN last_vote_date::date = CURRENT_DATE THEN current_streak
ELSE 0
END,
longest_streak = GREATEST(longest_streak, current_streak + 1)
WHERE last_vote_date IS NOT NULL
`)
return NextResponse.json({ success: true })
} catch (error) {
console.error('Update streaks error:', error)
return NextResponse.json({ error: 'Failed to update streaks' }, { status: 500 })
}
}