2022-02-17 20:56:24 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-05-08 20:33:58 +00:00
|
|
|
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
|
|
|
import { listAllComments } from 'web/lib/firebase/comments'
|
|
|
|
import { getContractFromId } from 'web/lib/firebase/contracts'
|
|
|
|
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
2022-02-17 23:22:51 +00:00
|
|
|
import { FullMarket, ApiError, toLiteMarket } from '../_types'
|
2022-02-17 20:56:24 +00:00
|
|
|
|
|
|
|
export default async function handler(
|
|
|
|
req: NextApiRequest,
|
2022-02-17 23:22:51 +00:00
|
|
|
res: NextApiResponse<FullMarket | ApiError>
|
2022-02-17 20:56:24 +00:00
|
|
|
) {
|
2022-04-30 20:30:49 +00:00
|
|
|
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
|
2022-02-17 20:56:24 +00:00
|
|
|
const { id } = req.query
|
|
|
|
const contractId = id as string
|
|
|
|
|
2022-02-20 04:51:51 +00:00
|
|
|
const [contract, allBets, comments] = await Promise.all([
|
2022-02-17 20:56:24 +00:00
|
|
|
getContractFromId(contractId),
|
|
|
|
listAllBets(contractId),
|
|
|
|
listAllComments(contractId),
|
|
|
|
])
|
|
|
|
|
2022-06-13 04:42:41 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-02-20 04:51:51 +00:00
|
|
|
const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude<
|
|
|
|
Bet,
|
|
|
|
'userId'
|
|
|
|
>[]
|
|
|
|
|
2022-02-17 20:56:24 +00:00
|
|
|
if (!contract) {
|
|
|
|
res.status(404).json({ error: 'Contract not found' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache on Vercel edge servers for 2min
|
|
|
|
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
|
|
|
|
return res.status(200).json({
|
2022-02-17 23:22:51 +00:00
|
|
|
...toLiteMarket(contract),
|
2022-02-17 20:56:24 +00:00
|
|
|
bets,
|
|
|
|
comments,
|
|
|
|
})
|
|
|
|
}
|