2d3ca47b52
* Create 500-mana.html * Update 500-mana.html Fixed typos and links not working * Added "create a good market" guide added page creating-market.html For Stephen to set up condition (email 3 days after signing up) * Update 500-mana.html updated 500 Mana email (still need to make changes to create market guide) * email changes * sendOneWeekBonusEmail logic * add dayjs as dependency * don't use mailgun scheduling Co-authored-by: mantikoros <sgrugett@gmail.com>
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import * as functions from 'firebase-functions'
|
|
import * as admin from 'firebase-admin'
|
|
import * as dayjs from 'dayjs'
|
|
|
|
import { getPrivateUser } from './utils'
|
|
import { sendOneWeekBonusEmail } from './emails'
|
|
import { User } from 'common/user'
|
|
|
|
export const manabonusemail = functions
|
|
.runWith({ secrets: ['MAILGUN_KEY'] })
|
|
.pubsub.schedule('0 9 * * 1-7')
|
|
.onRun(async () => {
|
|
await sendOneWeekEmails()
|
|
})
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
async function sendOneWeekEmails() {
|
|
const oneWeekAgo = dayjs().subtract(1, 'week').valueOf()
|
|
const twoWeekAgo = dayjs().subtract(2, 'weeks').valueOf()
|
|
|
|
const userDocs = await firestore
|
|
.collection('users')
|
|
.where('createdTime', '<=', oneWeekAgo)
|
|
.get()
|
|
|
|
for (const user of userDocs.docs.map((d) => d.data() as User)) {
|
|
if (user.createdTime < twoWeekAgo) continue
|
|
|
|
const privateUser = await getPrivateUser(user.id)
|
|
if (!privateUser || privateUser.manaBonusEmailSent) continue
|
|
|
|
await firestore
|
|
.collection('private-users')
|
|
.doc(user.id)
|
|
.update({ manaBonusEmailSent: true })
|
|
|
|
console.log('sending m$ bonus email to', user.username)
|
|
await sendOneWeekBonusEmail(user, privateUser)
|
|
return
|
|
}
|
|
}
|