manifold/web/lib/firebase/bets.ts

68 lines
1.5 KiB
TypeScript

import {
collection,
collectionGroup,
query,
onSnapshot,
where,
} from 'firebase/firestore'
import _ from 'lodash'
import { db } from './init'
import { Bet } from '../../../common/bet'
import { Contract } from '../../../common/contract'
import { getValues } from './utils'
export type { Bet }
function getBetsCollection(contractId: string) {
return collection(db, 'contracts', contractId, 'bets')
}
export async function listAllBets(contractId: string) {
const bets = await getValues<Bet>(getBetsCollection(contractId))
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
return bets
}
export function listenForBets(
contractId: string,
setBets: (bets: Bet[]) => void
) {
return onSnapshot(getBetsCollection(contractId), (snap) => {
const bets = snap.docs.map((doc) => doc.data() as Bet)
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
setBets(bets)
})
}
export function listenForUserBets(
userId: string,
setBets: (bets: Bet[]) => void
) {
const userQuery = query(
collectionGroup(db, 'bets'),
where('userId', '==', userId)
)
return onSnapshot(userQuery, (snap) => {
const bets = snap.docs.map((doc) => doc.data() as Bet)
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
setBets(bets)
})
}
export function withoutAnteBets(contract: Contract, bets: Bet[]) {
const { createdTime } = contract
if (
bets.length >= 2 &&
bets[0].createdTime === createdTime &&
bets[1].createdTime === createdTime
) {
return bets.slice(2)
}
return bets
}