mirror of
https://github.com/ershisan99/www.git
synced 2026-01-10 12:35:12 +00:00
add refresh history by date endpoint
This commit is contained in:
41
src/app/api/refresh-history-by-date/route.ts
Normal file
41
src/app/api/refresh-history-by-date/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { env } from '@/env'
|
||||
import { syncHistoryByDateRange } from '@/server/api/routers/history'
|
||||
import { headers } from 'next/headers'
|
||||
|
||||
const SECURE_TOKEN = env.CRON_SECRET
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const headersList = await headers()
|
||||
const authToken = headersList.get('authorization')?.replace('Bearer ', '')
|
||||
|
||||
if (authToken !== SECURE_TOKEN) {
|
||||
return new Response('unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse request body to get date range parameters
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const startDate = body.start_date
|
||||
const endDate = body.end_date
|
||||
|
||||
try {
|
||||
console.log('refreshing history by date range...')
|
||||
if (startDate) {
|
||||
console.log(`Start date: ${startDate}`)
|
||||
}
|
||||
if (endDate) {
|
||||
console.log(`End date: ${endDate}`)
|
||||
}
|
||||
|
||||
await syncHistoryByDateRange(startDate, endDate)
|
||||
} catch (err) {
|
||||
console.error('history refresh by date range 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 })
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,16 @@ export const history_router = createTRPCRouter({
|
||||
sync: publicProcedure.mutation(async () => {
|
||||
return syncHistory()
|
||||
}),
|
||||
syncByDateRange: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
start_date: z.string().optional(),
|
||||
end_date: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
return syncHistoryByDateRange(input.start_date, input.end_date)
|
||||
}),
|
||||
})
|
||||
|
||||
export async function syncHistory() {
|
||||
@@ -80,6 +90,36 @@ export async function syncHistory() {
|
||||
return data
|
||||
}
|
||||
|
||||
export async function syncHistoryByDateRange(
|
||||
start_date?: string,
|
||||
end_date?: string
|
||||
) {
|
||||
const searchParams: Record<string, string> = {}
|
||||
|
||||
if (start_date) {
|
||||
searchParams.start_date = start_date
|
||||
}
|
||||
|
||||
if (end_date) {
|
||||
searchParams.end_date = end_date
|
||||
}
|
||||
|
||||
const data = await ky
|
||||
.get('https://api.neatqueue.com/api/history/1226193436521267223', {
|
||||
searchParams,
|
||||
timeout: 60000,
|
||||
})
|
||||
.json<any>()
|
||||
console.log(data)
|
||||
const chunkedData = chunk(data.data, 100)
|
||||
for (const chunk of chunkedData) {
|
||||
await insertGameHistory(chunk).catch((e) => {
|
||||
console.error(e)
|
||||
})
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
function processGameEntry(gameId: number, game_num: number, entry: any) {
|
||||
const parsedEntry = typeof entry === 'string' ? JSON.parse(entry) : entry
|
||||
if (parsedEntry.game === '1v1-attrition') {
|
||||
|
||||
Reference in New Issue
Block a user