diff --git a/common/contract.ts b/common/contract.ts index 6e362de0..8aad82fe 100644 --- a/common/contract.ts +++ b/common/contract.ts @@ -21,6 +21,8 @@ export type FullContract< createdTime: number // Milliseconds since epoch lastUpdatedTime: number // If the question or description was changed + lastBetTime?: number + lastCommentTime?: number closeTime?: number // When no more trading is allowed isResolved: boolean diff --git a/functions/src/index.ts b/functions/src/index.ts index d87e88f9..004a22b0 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -12,6 +12,7 @@ export * from './create-contract' export * from './create-user' export * from './create-fold' export * from './create-answer' +export * from './on-create-bet' export * from './on-create-comment' export * from './on-fold-follow' export * from './on-fold-delete' diff --git a/functions/src/on-create-bet.ts b/functions/src/on-create-bet.ts new file mode 100644 index 00000000..12e6e720 --- /dev/null +++ b/functions/src/on-create-bet.ts @@ -0,0 +1,27 @@ +import * as functions from 'firebase-functions' +import * as admin from 'firebase-admin' +import * as _ from 'lodash' + +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 + + await firestore + .collection('contracts') + .doc(contract.id) + .update({ lastBetTime: bet.createdTime }) + }) diff --git a/functions/src/on-create-comment.ts b/functions/src/on-create-comment.ts index 02ade1fe..d232e2ed 100644 --- a/functions/src/on-create-comment.ts +++ b/functions/src/on-create-comment.ts @@ -18,12 +18,18 @@ export const onCreateComment = functions.firestore } const contract = await getContract(contractId) - if (!contract) return + if (!contract) + throw new Error('Could not find contract corresponding with comment') const comment = change.data() as Comment const commentCreator = await getUser(comment.userId) - if (!commentCreator) return + if (!commentCreator) throw new Error('Could not find contract creator') + + await firestore + .collection('contracts') + .doc(contract.id) + .update({ lastCommentTime: comment.createdTime }) let bet: Bet | undefined let answer: Answer | undefined