manifold/functions/src/on-create-answer.ts
Marshall Polaris 312b244e2a
Small backend cleanups (#643)
* Reuse DAY_MS in update-metrics job

* More concise transaction in cancelbet

* Remove some meaningless awaits

* Do less work in onCreateLiquidityProvision

* Do less work in onCreateAnswer
2022-07-24 00:45:45 -07:00

34 lines
1009 B
TypeScript

import * as functions from 'firebase-functions'
import { getContract, getUser } from './utils'
import { createNotification } from './create-notification'
import { Answer } from '../../common/answer'
export const onCreateAnswer = functions.firestore
.document('contracts/{contractId}/answers/{answerNumber}')
.onCreate(async (change, context) => {
const { contractId } = context.params as {
contractId: string
}
const { eventId } = context
const answer = change.data() as Answer
// Ignore ante answer.
if (answer.number === 0) return
const contract = await getContract(contractId)
if (!contract)
throw new Error('Could not find contract corresponding with answer')
const answerCreator = await getUser(answer.userId)
if (!answerCreator) throw new Error('Could not find answer creator')
await createNotification(
answer.id,
'answer',
'created',
answerCreator,
eventId,
answer.text,
{ contract }
)
})