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` - Response type: A `FullMarket`
```tsx ```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 & { type FullMarket = LiteMarket & {
bets: Bet[] bets: Bet[]
comments: Comment[] comments: Comment[]
answers?: Answer[]
} }
type Bet = { type Bet = {

View File

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

View File

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

View File

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

View File

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