2022-02-17 20:56:24 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-02-20 04:51:51 +00:00
|
|
|
import { Bet, listAllBets } from '../../../../lib/firebase/bets'
|
2022-02-17 20:56:24 +00:00
|
|
|
import { listAllComments } from '../../../../lib/firebase/comments'
|
|
|
|
import { getContractFromSlug } from '../../../../lib/firebase/contracts'
|
2022-02-17 23:22:51 +00:00
|
|
|
import { FullMarket, ApiError, toLiteMarket } from '../_types'
|
2022-04-30 20:30:49 +00:00
|
|
|
import { applyCorsHeaders, CORS_UNRESTRICTED } from '../../../../lib/api/cors'
|
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 { slug } = req.query
|
|
|
|
|
|
|
|
const contract = await getContractFromSlug(slug as string)
|
|
|
|
|
|
|
|
if (!contract) {
|
|
|
|
res.status(404).json({ error: 'Contract not found' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-20 04:51:51 +00:00
|
|
|
const [allBets, comments] = await Promise.all([
|
2022-02-17 20:56:24 +00:00
|
|
|
listAllBets(contract.id),
|
|
|
|
listAllComments(contract.id),
|
|
|
|
])
|
|
|
|
|
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
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
}
|