manifold/functions/src/on-create-bet.ts
Marshall Polaris 47f10301c8
Change lodash stuff so that it can be tree-shaken out of build (#233)
* Set common package.json sideEffects: false

* Configure SWC to modularize lodash imports

* Import specific lodash functions instead of _

* Add an eslint rule to avoid full lodash import
2022-05-22 01:36:05 -07:00

28 lines
783 B
TypeScript

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { getContract } from './utils'
import { Bet } from '../../common/bet'
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
}
const contract = await getContract(contractId)
if (!contract)
throw new Error('Could not find contract corresponding with bet')
const bet = change.data() as Bet
const lastBetTime = bet.createdTime
await firestore
.collection('contracts')
.doc(contract.id)
.update({ lastBetTime, lastUpdatedTime: Date.now() })
})