From 5861817e5a7d78f7819de4c9d59a7f9fa58d140d Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Thu, 21 Apr 2022 08:44:23 -0600 Subject: [PATCH] Send emails for comments without bets --- functions/src/emails.ts | 22 +++++++++++++--------- functions/src/on-create-comment.ts | 27 ++++++++++++++++----------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/functions/src/emails.ts b/functions/src/emails.ts index 143e938d..a366f34e 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -167,7 +167,7 @@ export const sendNewCommentEmail = async ( commentCreator: User, contract: Contract, comment: Comment, - bet: Bet, + bet?: Bet, answer?: Answer ) => { const privateUser = await getPrivateUser(userId) @@ -186,8 +186,11 @@ export const sendNewCommentEmail = async ( const { name: commentorName, avatarUrl: commentorAvatarUrl } = commentCreator const { text } = comment - const { amount, sale, outcome } = bet - let betDescription = `${sale ? 'sold' : 'bought'} M$ ${Math.round(amount)}` + let betDescription = '' + if (bet) { + const { amount, sale } = bet + betDescription = `${sale ? 'sold' : 'bought'} M$ ${Math.round(amount)}` + } const subject = `Comment on ${question}` const from = `${commentorName} ` @@ -208,16 +211,17 @@ export const sendNewCommentEmail = async ( comment: text, marketUrl, unsubscribeUrl, - betDescription, + betDescription: betDescription, }, { from } ) } else { - betDescription = `${betDescription} of ${toDisplayResolution( - contract, - outcome - )}` - + if (bet) { + betDescription = `${betDescription} of ${toDisplayResolution( + contract, + bet.outcome + )}` + } await sendTemplateEmail( privateUser.email, subject, diff --git a/functions/src/on-create-comment.ts b/functions/src/on-create-comment.ts index e48d6039..02ade1fe 100644 --- a/functions/src/on-create-comment.ts +++ b/functions/src/on-create-comment.ts @@ -6,6 +6,7 @@ import { getContract, getUser, getValues } from './utils' import { Comment } from '../../common/comment' import { sendNewCommentEmail } from './emails' import { Bet } from '../../common/bet' +import { Answer } from '../../common/answer' const firestore = admin.firestore() @@ -24,18 +25,22 @@ export const onCreateComment = functions.firestore const commentCreator = await getUser(comment.userId) if (!commentCreator) return - const betSnapshot = await firestore - .collection('contracts') - .doc(contractId) - .collection('bets') - .doc(comment.betId) - .get() - const bet = betSnapshot.data() as Bet + let bet: Bet | undefined + let answer: Answer | undefined + if (comment.betId) { + const betSnapshot = await firestore + .collection('contracts') + .doc(contractId) + .collection('bets') + .doc(comment.betId) + .get() + bet = betSnapshot.data() as Bet - const answer = - contract.outcomeType === 'FREE_RESPONSE' && contract.answers - ? contract.answers.find((answer) => answer.id === bet.outcome) - : undefined + answer = + contract.outcomeType === 'FREE_RESPONSE' && contract.answers + ? contract.answers.find((answer) => answer.id === bet?.outcome) + : undefined + } const comments = await getValues( firestore.collection('contracts').doc(contractId).collection('comments')