2022-07-07 22:36:02 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
|
|
|
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
|
|
|
import { Bet, getUserBets } from 'web/lib/firebase/bets'
|
|
|
|
import { getUserByUsername } from 'web/lib/firebase/users'
|
|
|
|
import { ApiError } from '../../../_types'
|
|
|
|
|
|
|
|
export default async function handler(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<Bet[] | ApiError>
|
|
|
|
) {
|
|
|
|
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
|
|
|
|
const { username } = req.query
|
|
|
|
|
|
|
|
const user = await getUserByUsername(username as string)
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
res.status(404).json({ error: 'User not found' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 02:38:09 +00:00
|
|
|
const bets = await getUserBets(user.id)
|
|
|
|
const visibleBets = bets.filter((b) => !b.isRedemption && !b.isAnte)
|
2022-07-07 22:36:02 +00:00
|
|
|
|
|
|
|
res.setHeader('Cache-Control', 'max-age=0')
|
2022-09-02 02:38:09 +00:00
|
|
|
return res.status(200).json(visibleBets)
|
2022-07-07 22:36:02 +00:00
|
|
|
}
|