Files
lukenyaMarathon_frontend/app/api/my-race/route.ts
T
KabundiNjuki ff743a1397 Initial commit
2026-10-05 16:40:36 +03:00

26 lines
1.2 KiB
TypeScript

import { NextResponse } from 'next/server'
import { FrappeError, METHODS, frappeCall } from '@/lib/lukenya-api'
// POST { token } → the runner's entry. POST { email } → email a fresh My Race link.
// Tokens travel in the request body, not the URL, so they don't end up in access logs.
export async function POST(req: Request) {
const body = await req.json().catch(() => ({}))
try {
if (typeof body.token === 'string' && body.token) {
const entry = await frappeCall<Record<string, unknown>>(METHODS.myRace, { args: { token: body.token }, post: true })
return NextResponse.json(entry, { headers: { 'Cache-Control': 'no-store' } })
}
if (typeof body.email === 'string' && body.email.includes('@')) {
const result = await frappeCall<{ message: string }>(METHODS.myRaceLink, {
args: { email: body.email.trim().toLowerCase() },
post: true,
})
return NextResponse.json(result)
}
return NextResponse.json({ error: 'Enter the email you registered with.' }, { status: 400 })
} catch (err) {
const status = err instanceof FrappeError && err.status < 500 ? err.status : 502
return NextResponse.json({ error: (err as Error).message }, { status })
}
}