Send emails for comments without bets

This commit is contained in:
Ian Philips 2022-04-21 08:44:23 -06:00
parent 8291a05f32
commit 5861817e5a
2 changed files with 29 additions and 20 deletions

View File

@ -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} <info@manifold.markets>`
@ -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,

View File

@ -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<Comment>(
firestore.collection('contracts').doc(contractId).collection('comments')