Add weekly email sent flag, filter out manifold grouped markets

This commit is contained in:
Ian Philips 2022-08-22 16:36:39 -06:00
parent ec4d0f6b4a
commit e1775681aa
5 changed files with 42 additions and 6 deletions

View File

@ -54,6 +54,7 @@ export type PrivateUser = {
unsubscribedFromAnswerEmails?: boolean unsubscribedFromAnswerEmails?: boolean
unsubscribedFromGenericEmails?: boolean unsubscribedFromGenericEmails?: boolean
unsubscribedFromWeeklyTrendingEmails?: boolean unsubscribedFromWeeklyTrendingEmails?: boolean
weeklyTrendingEmailSent?: boolean
manaBonusEmailSent?: boolean manaBonusEmailSent?: boolean
initialDeviceToken?: string initialDeviceToken?: string
initialIpAddress?: string initialIpAddress?: string

View File

@ -29,6 +29,7 @@ export * from './on-delete-group'
export * from './score-contracts' export * from './score-contracts'
export * from './weekly-markets-emails' export * from './weekly-markets-emails'
export * from './reset-betting-streaks' export * from './reset-betting-streaks'
export * from './reset-weekly-emails-flag'
// v2 // v2
export * from './health' export * from './health'

View File

@ -9,7 +9,7 @@ const firestore = admin.firestore()
export const resetBettingStreaksForUsers = functions.pubsub export const resetBettingStreaksForUsers = functions.pubsub
.schedule(`0 ${BETTING_STREAK_RESET_HOUR} * * *`) .schedule(`0 ${BETTING_STREAK_RESET_HOUR} * * *`)
.timeZone('utc') .timeZone('Etc/UTC')
.onRun(async () => { .onRun(async () => {
await resetBettingStreaksInternal() await resetBettingStreaksInternal()
}) })

View File

@ -0,0 +1,24 @@
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { getAllPrivateUsers } from './utils'
export const resetWeeklyEmailsFlag = functions
.runWith({ secrets: ['MAILGUN_KEY'] })
// every Monday at 1 am PT (UTC -07:00) ( 12 hours before the emails will be sent)
.pubsub.schedule('0 7 * * 1')
.timeZone('Etc/UTC')
.onRun(async () => {
const privateUsers = await getAllPrivateUsers()
// get all users that haven't unsubscribed from weekly emails
const privateUsersToSendEmailsTo = privateUsers.filter((user) => {
return !user.unsubscribedFromWeeklyTrendingEmails
})
const firestore = admin.firestore()
await Promise.all(
privateUsersToSendEmailsTo.map(async (user) => {
return firestore.collection('private-users').doc(user.id).update({
weeklyTrendingEmailSent: false,
})
})
)
})

View File

@ -9,9 +9,9 @@ import { DAY_MS } from '../../common/util/time'
export const weeklyMarketsEmails = functions export const weeklyMarketsEmails = functions
.runWith({ secrets: ['MAILGUN_KEY'] }) .runWith({ secrets: ['MAILGUN_KEY'] })
// every Monday at 12pm PT (UTC -07:00) // every minute on Monday for an hour at 12pm PT (UTC -07:00)
.pubsub.schedule('0 19 * * 1') .pubsub.schedule('* 18 * * 1')
.timeZone('utc') .timeZone('Etc/UTC')
.onRun(async () => { .onRun(async () => {
await sendTrendingMarketsEmailsToAllUsers() await sendTrendingMarketsEmailsToAllUsers()
}) })
@ -37,7 +37,10 @@ async function sendTrendingMarketsEmailsToAllUsers() {
const privateUsers = await getAllPrivateUsers() const privateUsers = await getAllPrivateUsers()
// get all users that haven't unsubscribed from weekly emails // get all users that haven't unsubscribed from weekly emails
const privateUsersToSendEmailsTo = privateUsers.filter((user) => { const privateUsersToSendEmailsTo = privateUsers.filter((user) => {
return !user.unsubscribedFromWeeklyTrendingEmails return (
!user.unsubscribedFromWeeklyTrendingEmails &&
!user.weeklyTrendingEmailSent
)
}) })
log( log(
'Sending weekly trending emails to', 'Sending weekly trending emails to',
@ -50,13 +53,17 @@ async function sendTrendingMarketsEmailsToAllUsers() {
!( !(
contract.question.toLowerCase().includes('trump') && contract.question.toLowerCase().includes('trump') &&
contract.question.toLowerCase().includes('president') contract.question.toLowerCase().includes('president')
) && (contract?.closeTime ?? 0) > Date.now() + DAY_MS ) &&
(contract?.closeTime ?? 0) > Date.now() + DAY_MS &&
!contract.groupSlugs?.includes('manifold-features') &&
!contract.groupSlugs?.includes('manifold-6748e065087e')
) )
.slice(0, 20) .slice(0, 20)
log( log(
`Found ${trendingContracts.length} trending contracts:\n`, `Found ${trendingContracts.length} trending contracts:\n`,
trendingContracts.map((c) => c.question).join('\n ') trendingContracts.map((c) => c.question).join('\n ')
) )
for (const privateUser of privateUsersToSendEmailsTo) { for (const privateUser of privateUsersToSendEmailsTo) {
if (!privateUser.email) { if (!privateUser.email) {
log(`No email for ${privateUser.username}`) log(`No email for ${privateUser.username}`)
@ -79,6 +86,9 @@ async function sendTrendingMarketsEmailsToAllUsers() {
if (!user) continue if (!user) continue
await sendInterestingMarketsEmail(user, privateUser, contractsToSend) await sendInterestingMarketsEmail(user, privateUser, contractsToSend)
await firestore.collection('private-users').doc(user.id).update({
weeklyTrendingEmailSent: true,
})
} }
} }