Skip to content

Commit

Permalink
Do not send email if there are no new notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Mephistic committed Jan 28, 2025
1 parent 76a6a50 commit 8def83c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions functions/src/notifications/deliverNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,33 @@ const deliverEmailNotifications = async () => {
const user = userDoc.data() as User
const digestData = await buildDigestData(user, userDoc.id, now)

const htmlString = renderToHtmlString(digestData)

// Create an email document in /notifications_mails to queue up the send
await db.collection("notifications_mails").add({
to: [user.email],
message: {
subject: "Your Notifications Digest",
text: "", // blank because we're sending HTML
html: htmlString
},
createdAt: Timestamp.now()
})

console.log(`Saved email message to user ${user.email}`)
// If there are no new notifications, don't send an email
if (
digestData.numBillsWithNewTestimony === 0 &&
digestData.numUsersWithNewTestimony === 0
) {
console.log(`No new notifications for ${userDoc.id} - not sending email`)
} else {
const htmlString = renderToHtmlString(digestData)

// Create an email document in /notifications_mails to queue up the send
await db.collection("notifications_mails").add({
to: [user.email],
message: {
subject: "Your Notifications Digest",
text: "", // blank because we're sending HTML
html: htmlString
},
createdAt: Timestamp.now()
})

console.log(`Saved email message to user ${userDoc.id}`)
}

const nextDigestAt = getNextDigestAt(user.notificationFrequency)
await userDoc.ref.update({ nextDigestAt })

console.log(`Updated nextDigestAt for ${user.email} to ${nextDigestAt}`)
console.log(`Updated nextDigestAt for ${userDoc.id} to ${nextDigestAt}`)
})

// Wait for all email documents to be created
Expand Down
2 changes: 1 addition & 1 deletion functions/src/notifications/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Timestamp } from "firebase-admin/firestore"
import { Frequency } from "../auth/types"
import { startOfDay } from "date-fns"

// TODO - Unit test this function
// TODO - Unit tests
export const getNextDigestAt = (notificationFrequency: Frequency) => {
const now = startOfDay(new Date())
let nextDigestAt = null
Expand Down

0 comments on commit 8def83c

Please sign in to comment.