2022-05-01 16:36:54 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { keyBy } from 'lodash'
|
2022-05-01 16:36:54 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
import { Bet, LimitBet } from '../../common/bet'
|
|
|
|
import { getContract, getUser, getValues } from './utils'
|
|
|
|
import { createBetFillNotification } from './create-notification'
|
|
|
|
import { filterDefined } from '../../common/util/array'
|
2022-05-01 16:36:54 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
|
|
|
export const onCreateBet = functions.firestore
|
|
|
|
.document('contracts/{contractId}/bets/{betId}')
|
|
|
|
.onCreate(async (change, context) => {
|
|
|
|
const { contractId } = context.params as {
|
|
|
|
contractId: string
|
|
|
|
}
|
2022-07-10 18:05:44 +00:00
|
|
|
const { eventId } = context
|
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
const bet = change.data() as Bet
|
|
|
|
const lastBetTime = bet.createdTime
|
|
|
|
|
|
|
|
await firestore
|
|
|
|
.collection('contracts')
|
2022-06-17 07:15:37 +00:00
|
|
|
.doc(contractId)
|
2022-05-01 16:36:54 +00:00
|
|
|
.update({ lastBetTime, lastUpdatedTime: Date.now() })
|
2022-07-10 18:05:44 +00:00
|
|
|
|
|
|
|
await notifyFills(bet, contractId, eventId)
|
2022-05-01 16:36:54 +00:00
|
|
|
})
|
2022-07-10 18:05:44 +00:00
|
|
|
|
|
|
|
const notifyFills = async (bet: Bet, contractId: string, eventId: string) => {
|
|
|
|
if (!bet.fills) return
|
|
|
|
|
|
|
|
const user = await getUser(bet.userId)
|
|
|
|
if (!user) return
|
|
|
|
const contract = await getContract(contractId)
|
|
|
|
if (!contract) return
|
|
|
|
|
|
|
|
const matchedFills = bet.fills.filter((fill) => fill.matchedBetId !== null)
|
|
|
|
const matchedBets = (
|
|
|
|
await Promise.all(
|
|
|
|
matchedFills.map((fill) =>
|
|
|
|
getValues<LimitBet>(
|
|
|
|
firestore.collectionGroup('bets').where('id', '==', fill.matchedBetId)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).flat()
|
|
|
|
|
|
|
|
const betUsers = await Promise.all(
|
|
|
|
matchedBets.map((bet) => getUser(bet.userId))
|
|
|
|
)
|
|
|
|
const betUsersById = keyBy(filterDefined(betUsers), 'id')
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
matchedBets.map((matchedBet) => {
|
|
|
|
const matchedUser = betUsersById[matchedBet.userId]
|
|
|
|
if (!matchedUser) return
|
|
|
|
|
|
|
|
return createBetFillNotification(
|
|
|
|
user,
|
|
|
|
matchedUser,
|
|
|
|
bet,
|
|
|
|
matchedBet,
|
|
|
|
contract,
|
|
|
|
eventId
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|