From 0cbc0010c194e745abf0ffe2c803e7741ae4a3d3 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Fri, 19 Aug 2022 17:02:52 -0500 Subject: [PATCH] schedule emails from onCreateUser; send interesting markets on D1 --- functions/src/create-user.ts | 5 +--- functions/src/emails.ts | 5 ++-- functions/src/index.ts | 2 +- functions/src/on-create-user.ts | 41 +++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 functions/src/on-create-user.ts diff --git a/functions/src/create-user.ts b/functions/src/create-user.ts index c0b03e23..7156855e 100644 --- a/functions/src/create-user.ts +++ b/functions/src/create-user.ts @@ -16,7 +16,6 @@ import { cleanDisplayName, cleanUsername, } from '../../common/util/clean-username' -import { sendPersonalFollowupEmail, sendWelcomeEmail } from './emails' import { isWhitelisted } from '../../common/envs/constants' import { CATEGORIES_GROUP_SLUG_POSTFIX, @@ -93,10 +92,8 @@ export const createuser = newEndpoint(opts, async (req, auth) => { } await firestore.collection('private-users').doc(auth.uid).create(privateUser) - await addUserToDefaultGroups(user) - await sendWelcomeEmail(user, privateUser) - await sendPersonalFollowupEmail(user, privateUser) + await track(auth.uid, 'create user', { username }, { ip: req.ip }) return { user, privateUser } diff --git a/functions/src/emails.ts b/functions/src/emails.ts index 97ffce10..6768e8ea 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -1,4 +1,3 @@ -import * as dayjs from 'dayjs' import { DOMAIN } from '../../common/envs/constants' import { Answer } from '../../common/answer' @@ -170,7 +169,8 @@ export const sendWelcomeEmail = async ( export const sendPersonalFollowupEmail = async ( user: User, - privateUser: PrivateUser + privateUser: PrivateUser, + sendTime: string ) => { if (!privateUser || !privateUser.email) return @@ -192,7 +192,6 @@ Cofounder of Manifold Markets https://manifold.markets ` - const sendTime = dayjs().add(4, 'hours').toString() await sendTextEmail( privateUser.email, diff --git a/functions/src/index.ts b/functions/src/index.ts index ec1947f1..4d7cf42b 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -5,6 +5,7 @@ import { EndpointDefinition } from './api' admin.initializeApp() // v1 +export * from './on-create-user' export * from './on-create-bet' export * from './on-create-comment-on-contract' export * from './on-view' @@ -28,7 +29,6 @@ export * from './score-contracts' export * from './weekly-markets-emails' export * from './reset-betting-streaks' - // v2 export * from './health' export * from './transact' diff --git a/functions/src/on-create-user.ts b/functions/src/on-create-user.ts new file mode 100644 index 00000000..fd951ab4 --- /dev/null +++ b/functions/src/on-create-user.ts @@ -0,0 +1,41 @@ +import * as functions from 'firebase-functions' +import * as dayjs from 'dayjs' +import * as utc from 'dayjs/plugin/utc' +dayjs.extend(utc) + +import { getPrivateUser } from './utils' +import { User } from 'common/user' +import { + sendInterestingMarketsEmail, + sendPersonalFollowupEmail, + sendWelcomeEmail, +} from './emails' +import { getTrendingContracts } from './weekly-markets-emails' + +export const onCreateUser = functions + .runWith({ secrets: ['MAILGUN_KEY'] }) + .firestore.document('users/{userId}') + .onCreate(async (snapshot) => { + const user = snapshot.data() as User + const privateUser = await getPrivateUser(user.id) + if (!privateUser) return + + await sendWelcomeEmail(user, privateUser) + + const followupSendTime = dayjs().add(4, 'hours').toString() + await sendPersonalFollowupEmail(user, privateUser, followupSendTime) + + // skip email if weekly email is about to go out + const day = dayjs().utc().day() + if (day === 0 || (day === 1 && dayjs().utc().hour() <= 19)) return + + const contracts = await getTrendingContracts() + const marketsSendTime = dayjs().add(24, 'hours').toString() + + await sendInterestingMarketsEmail( + user, + privateUser, + contracts, + marketsSendTime + ) + })