mirror of
https://github.com/ershisan99/www.git
synced 2026-01-05 21:02:08 +00:00
add refresh logic
This commit is contained in:
28
src/app/api/refresh-history/route.ts
Normal file
28
src/app/api/refresh-history/route.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { env } from '@/env'
|
||||
import { syncHistory } from '@/server/api/routers/history'
|
||||
import { headers } from 'next/headers'
|
||||
|
||||
const SECURE_TOKEN = env.CRON_SECRET
|
||||
|
||||
export async function POST() {
|
||||
const headersList = await headers()
|
||||
const authToken = headersList.get('authorization')?.replace('Bearer ', '')
|
||||
|
||||
if (authToken !== SECURE_TOKEN) {
|
||||
return new Response('unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
console.log('refreshing history...')
|
||||
await syncHistory()
|
||||
} catch (err) {
|
||||
console.error('history refresh failed:', err)
|
||||
return new Response('internal error', { status: 500 })
|
||||
}
|
||||
return Response.json({ success: true })
|
||||
} catch (err) {
|
||||
console.error('refresh failed:', err)
|
||||
return new Response('internal error', { status: 500 })
|
||||
}
|
||||
}
|
||||
34
src/app/api/refresh-leaderboard/route.ts
Normal file
34
src/app/api/refresh-leaderboard/route.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { env } from '@/env'
|
||||
import { LeaderboardService } from '@/server/services/leaderboard'
|
||||
import { RANKED_CHANNEL, VANILLA_CHANNEL } from '@/shared/constants'
|
||||
import { headers } from 'next/headers'
|
||||
|
||||
const SECURE_TOKEN = env.CRON_SECRET
|
||||
const CHANNEL_IDS = [RANKED_CHANNEL, VANILLA_CHANNEL]
|
||||
export async function POST() {
|
||||
const headersList = await headers()
|
||||
const authToken = headersList.get('authorization')?.replace('Bearer ', '')
|
||||
|
||||
if (authToken !== SECURE_TOKEN) {
|
||||
return new Response('unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const service = new LeaderboardService()
|
||||
|
||||
for (const channelId of CHANNEL_IDS) {
|
||||
try {
|
||||
console.log(`refreshing leaderboard for ${channelId}...`)
|
||||
await service.refreshLeaderboard(channelId)
|
||||
} catch (err) {
|
||||
console.error('refresh failed:', err)
|
||||
return new Response('internal error', { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
return Response.json({ success: true })
|
||||
} catch (err) {
|
||||
console.error('refresh failed:', err)
|
||||
return new Response('internal error', { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user