Include free response answers in FullMarket API response (#519)

This commit is contained in:
Ben Congdon 2022-06-16 15:14:59 -07:00 committed by GitHub
parent 6bb1a1f9ea
commit 60bb892601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 5 deletions

View File

@ -354,10 +354,11 @@ Requires no authorization.
- Response type: A `FullMarket`
```tsx
// A complete market, along with bets and comments
// A complete market, along with bets, comments, and answers (for free response markets)
type FullMarket = LiteMarket & {
bets: Bet[]
comments: Comment[]
answers?: Answer[]
}
type Bet = {

View File

@ -18,7 +18,7 @@ import { range, sortBy, sum } from 'lodash'
import { app } from './init'
import { getValues, listenForValue, listenForValues } from './utils'
import { BinaryContract, Contract } from 'common/contract'
import { BinaryContract, Contract, FreeResponseContract } from 'common/contract'
import { getDpmProbability } from 'common/calculate-dpm'
import { createRNG, shuffle } from 'common/util/random'
import { getCpmmProbability } from 'common/calculate-cpmm'
@ -28,7 +28,7 @@ import { MAX_FEED_CONTRACTS } from 'common/recommended-contracts'
import { Bet } from 'common/bet'
import { Comment } from 'common/comment'
import { ENV_CONFIG } from 'common/envs/constants'
export type { Contract }
export type { Contract, FreeResponseContract }
export function contractPath(contract: Contract) {
return `/${contract.creatorUsername}/${contract.slug}`

View File

@ -1,4 +1,5 @@
import { Bet } from 'common/bet'
import { Answer } from 'common/answer'
import { getProbability } from 'common/calculate'
import { Comment } from 'common/comment'
import { Contract } from 'common/contract'
@ -40,6 +41,7 @@ export type LiteMarket = {
export type FullMarket = LiteMarket & {
bets: Exclude<Bet, 'userId'>[]
comments: Comment[]
answers?: Answer[]
}
export type ApiError = {

View File

@ -1,7 +1,10 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Bet, listAllBets } from 'web/lib/firebase/bets'
import { listAllComments } from 'web/lib/firebase/comments'
import { getContractFromId } from 'web/lib/firebase/contracts'
import {
getContractFromId,
FreeResponseContract,
} from 'web/lib/firebase/contracts'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { FullMarket, ApiError, toLiteMarket } from '../_types'
@ -25,6 +28,8 @@ export default async function handler(
'userId'
>[]
const answers = (contract as FreeResponseContract).answers
if (!contract) {
res.status(404).json({ error: 'Contract not found' })
return
@ -36,5 +41,6 @@ export default async function handler(
...toLiteMarket(contract),
bets,
comments,
answers,
})
}

View File

@ -2,7 +2,10 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { Bet, listAllBets } from 'web/lib/firebase/bets'
import { listAllComments } from 'web/lib/firebase/comments'
import { getContractFromSlug } from 'web/lib/firebase/contracts'
import {
getContractFromSlug,
FreeResponseContract,
} from 'web/lib/firebase/contracts'
import { FullMarket, ApiError, toLiteMarket } from '../_types'
export default async function handler(
@ -24,6 +27,8 @@ export default async function handler(
listAllComments(contract.id),
])
const answers = (contract as FreeResponseContract).answers
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude<
Bet,
@ -36,5 +41,6 @@ export default async function handler(
...toLiteMarket(contract),
bets,
comments,
answers,
})
}