2021-12-14 05:40:38 +00:00
|
|
|
import {
|
|
|
|
collection,
|
|
|
|
collectionGroup,
|
|
|
|
query,
|
|
|
|
onSnapshot,
|
|
|
|
where,
|
|
|
|
} from 'firebase/firestore'
|
2022-01-05 06:32:52 +00:00
|
|
|
import _ from 'lodash'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
import { db } from './init'
|
|
|
|
import { Bet } from '../../../common/bet'
|
2022-01-13 17:01:20 +00:00
|
|
|
import { Contract } from '../../../common/contract'
|
2022-01-15 00:16:25 +00:00
|
|
|
import { getValues } from './utils'
|
2022-01-10 21:07:57 +00:00
|
|
|
export type { Bet }
|
2021-12-10 17:14:05 +00:00
|
|
|
|
2021-12-12 22:14:52 +00:00
|
|
|
function getBetsCollection(contractId: string) {
|
|
|
|
return collection(db, 'contracts', contractId, 'bets')
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
export async function listAllBets(contractId: string) {
|
|
|
|
const bets = await getValues<Bet>(getBetsCollection(contractId))
|
|
|
|
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
|
|
|
return bets
|
|
|
|
}
|
|
|
|
|
2021-12-12 22:14:52 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-12-10 17:14:05 +00:00
|
|
|
}
|
2021-12-14 05:40:38 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2022-01-13 17:01:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|