2022-02-17 20:56:24 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-06-17 07:35:38 +00:00
|
|
|
import { listAllBets } from 'web/lib/firebase/bets'
|
2022-05-08 20:33:58 +00:00
|
|
|
import { listAllComments } from 'web/lib/firebase/comments'
|
2022-06-17 07:20:43 +00:00
|
|
|
import { getContractFromId } from 'web/lib/firebase/contracts'
|
2022-05-08 20:33:58 +00:00
|
|
|
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
2022-06-30 22:11:45 +00:00
|
|
|
import { FullMarket, ApiError, toFullMarket } from '../../_types'
|
2022-10-01 18:37:55 +00:00
|
|
|
import { marketCacheStrategy } from '../../markets'
|
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-06-17 07:35:38 +00:00
|
|
|
const [contract, bets, comments] = await Promise.all([
|
2022-02-17 20:56:24 +00:00
|
|
|
getContractFromId(contractId),
|
|
|
|
listAllBets(contractId),
|
|
|
|
listAllComments(contractId),
|
|
|
|
])
|
|
|
|
|
|
|
|
if (!contract) {
|
|
|
|
res.status(404).json({ error: 'Contract not found' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-01 18:37:55 +00:00
|
|
|
res.setHeader('Cache-Control', marketCacheStrategy)
|
2022-06-17 07:20:43 +00:00
|
|
|
return res.status(200).json(toFullMarket(contract, comments, bets))
|
2022-02-17 20:56:24 +00:00
|
|
|
}
|