@@ -14,6 +14,7 @@ import {
14
14
} from "../email/types"
15
15
import { prepareHandlebars } from "../email/handlebarsHelpers"
16
16
import { getAuth } from "firebase-admin/auth"
17
+ import { Frequency } from "../auth/types"
17
18
18
19
const NUM_BILLS_TO_DISPLAY = 4
19
20
const NUM_USERS_TO_DISPLAY = 4
@@ -25,9 +26,13 @@ const db = admin.firestore()
25
26
const auth = getAuth ( )
26
27
const path = require ( "path" )
27
28
28
- const isEmailVerified = async ( uid : string ) => {
29
+ const getVerifiedUserEmail = async ( uid : string ) => {
29
30
const userRecord = await auth . getUser ( uid )
30
- return userRecord && userRecord . emailVerified
31
+ if ( userRecord && userRecord . email && userRecord . emailVerified ) {
32
+ return userRecord . email
33
+ } else {
34
+ return null
35
+ }
31
36
}
32
37
33
38
// TODO: Batching (at both user + email level)?
@@ -39,21 +44,32 @@ const deliverEmailNotifications = async () => {
39
44
prepareHandlebars ( )
40
45
console . log ( "Handlebars helpers and partials prepared" )
41
46
47
+ // TODO: Add index
42
48
const usersSnapshot = await db
43
49
. collection ( "users" )
44
50
. where ( "nextDigestAt" , "<=" , now )
45
51
. get ( )
46
52
47
53
const emailPromises = usersSnapshot . docs . map ( async userDoc => {
48
54
const user = userDoc . data ( ) as User
55
+ if ( ! user || ! user . notificationFrequency ) {
56
+ console . log ( `User ${ userDoc . id } has no notificationFrequency - skipping` )
57
+ return
58
+ }
49
59
50
- const emailVerified = await isEmailVerified ( userDoc . id )
51
- if ( ! emailVerified ) {
52
- console . log ( `Skipping user ${ userDoc . id } because email is not verified` )
60
+ const verifiedEmail = await getVerifiedUserEmail ( userDoc . id )
61
+ if ( ! verifiedEmail ) {
62
+ console . log (
63
+ `Skipping user ${ userDoc . id } because they have no verified email address`
64
+ )
53
65
return
54
66
}
55
67
56
- const digestData = await buildDigestData ( user , userDoc . id , now )
68
+ const digestData = await buildDigestData (
69
+ userDoc . id ,
70
+ now ,
71
+ user . notificationFrequency
72
+ )
57
73
58
74
// If there are no new notifications, don't send an email
59
75
if (
@@ -66,7 +82,7 @@ const deliverEmailNotifications = async () => {
66
82
67
83
// Create an email document in /emails to queue up the send
68
84
await db . collection ( "emails" ) . add ( {
69
- to : [ user . email ] ,
85
+ to : [ verifiedEmail ] ,
70
86
message : {
71
87
subject : "Your Notifications Digest" ,
72
88
text : "" , // blank because we're sending HTML
@@ -89,8 +105,12 @@ const deliverEmailNotifications = async () => {
89
105
}
90
106
91
107
// TODO: Unit tests
92
- const buildDigestData = async ( user : User , userId : string , now : Timestamp ) => {
93
- const startDate = getNotificationStartDate ( user . notificationFrequency , now )
108
+ const buildDigestData = async (
109
+ userId : string ,
110
+ now : Timestamp ,
111
+ notificationFrequency : Frequency
112
+ ) => {
113
+ const startDate = getNotificationStartDate ( notificationFrequency , now )
94
114
95
115
const notificationsSnapshot = await db
96
116
. collection ( `users/${ userId } /userNotificationFeed` )
@@ -176,7 +196,7 @@ const buildDigestData = async (user: User, userId: string, now: Timestamp) => {
176
196
. sort ( ( a , b ) => b . newTestimonyCount - a . newTestimonyCount )
177
197
178
198
const digestData = {
179
- notificationFrequency : user . notificationFrequency ,
199
+ notificationFrequency,
180
200
startDate : startDate . toDate ( ) ,
181
201
endDate : now . toDate ( ) ,
182
202
bills : bills . slice ( 0 , NUM_BILLS_TO_DISPLAY ) ,
0 commit comments