diff --git a/functions/src/emails.ts b/functions/src/emails.ts index 84a5e3cb..a73208a6 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -244,7 +244,8 @@ export const sendNewCommentEmail = async ( contract: Contract, comment: Comment, bet?: Bet, - answer?: Answer + answerText?: string, + answerId?: string ) => { const privateUser = await getPrivateUser(userId) if ( @@ -273,9 +274,8 @@ export const sendNewCommentEmail = async ( const subject = `Comment on ${question}` const from = `${commentorName} ` - if (contract.outcomeType === 'FREE_RESPONSE') { - const answerText = answer?.text ?? '' - const answerNumber = `#${answer?.id ?? ''}` + if (contract.outcomeType === 'FREE_RESPONSE' && answerId && answerText) { + const answerNumber = `#${answerId}` await sendTemplateEmail( privateUser.email, diff --git a/functions/src/on-create-comment.ts b/functions/src/on-create-comment.ts index 18fc6757..7699843c 100644 --- a/functions/src/on-create-comment.ts +++ b/functions/src/on-create-comment.ts @@ -34,7 +34,14 @@ export const onCreateComment = functions.firestore let bet: Bet | undefined let answer: Answer | undefined - if (comment.betId) { + if (comment.answerOutcome) { + answer = + contract.outcomeType === 'FREE_RESPONSE' && contract.answers + ? contract.answers?.find( + (answer) => answer.id === comment.answerOutcome + ) + : undefined + } else if (comment.betId) { const betSnapshot = await firestore .collection('contracts') .doc(contractId) @@ -66,7 +73,8 @@ export const onCreateComment = functions.firestore contract, comment, bet, - answer + answer?.text, + answer?.id ) ) )