2022-08-22 22:36:39 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import { getAllPrivateUsers } from './utils'
|
|
|
|
|
|
|
|
export const resetWeeklyEmailsFlag = functions
|
|
|
|
.runWith({ secrets: ['MAILGUN_KEY'] })
|
2022-08-22 22:56:28 +00:00
|
|
|
// every Monday at 12 am PT (UTC -07:00) ( 12 hours before the emails will be sent)
|
2022-08-22 22:36:39 +00:00
|
|
|
.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,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|