|
| 1 | +import * as handlebars from "handlebars" |
| 2 | +import * as fs from "fs" |
| 3 | +import { Script } from "./types" |
| 4 | +import * as helpers from "../../functions/src/email/helpers" |
| 5 | + |
| 6 | +import { NotificationEmailDigest } from "functions/src/email/types" |
| 7 | +import { Record, String } from "runtypes" |
| 8 | +import { Timestamp } from "functions/src/firebase" |
| 9 | + |
| 10 | +const path = require("path") |
| 11 | + |
| 12 | +const PARTIALS_DIR = "./functions/lib/email/partials/" |
| 13 | +const EMAIL_TEMPLATE_PATH = "./functions/lib/email/digestEmail.handlebars" |
| 14 | + |
| 15 | +// Define Handlebars helper functions |
| 16 | +handlebars.registerHelper("toLowerCase", helpers.toLowerCase) |
| 17 | +handlebars.registerHelper("noUpdatesFormat", helpers.noUpdatesFormat) |
| 18 | +handlebars.registerHelper("isDefined", helpers.isDefined) |
| 19 | +function registerPartials(directoryPath: string) { |
| 20 | + console.log("REGISTERING PARTIALS") |
| 21 | + |
| 22 | + const filenames = fs.readdirSync(directoryPath) |
| 23 | + |
| 24 | + filenames.forEach(filename => { |
| 25 | + const partialPath = path.join(directoryPath, filename) |
| 26 | + const stats = fs.statSync(partialPath) |
| 27 | + |
| 28 | + if (stats.isDirectory()) { |
| 29 | + // Recursive call for directories |
| 30 | + registerPartials(partialPath) |
| 31 | + } else if (stats.isFile() && path.extname(filename) === ".handlebars") { |
| 32 | + // Register partials for .handlebars files |
| 33 | + const partialName = path.basename(filename, ".handlebars") |
| 34 | + const partialContent = fs.readFileSync(partialPath, "utf8") |
| 35 | + handlebars.registerPartial(partialName, partialContent) |
| 36 | + } |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +const renderToHtmlString = (digestData: NotificationEmailDigest) => { |
| 41 | + // TODO: Can we register these earlier since they're shared across all notifs - maybe at startup? |
| 42 | + registerPartials(PARTIALS_DIR) |
| 43 | + |
| 44 | + console.log("DEBUG: Working directory: ", process.cwd()) |
| 45 | + console.log( |
| 46 | + "DEBUG: Digest template path: ", |
| 47 | + path.resolve(EMAIL_TEMPLATE_PATH) |
| 48 | + ) |
| 49 | + |
| 50 | + const templateSource = fs.readFileSync( |
| 51 | + path.resolve(EMAIL_TEMPLATE_PATH), |
| 52 | + "utf8" |
| 53 | + ) |
| 54 | + const compiledTemplate = handlebars.compile(templateSource) |
| 55 | + return compiledTemplate({ digestData }) |
| 56 | +} |
| 57 | + |
| 58 | +const Args = Record({ email: String }) |
| 59 | + |
| 60 | +export const script: Script = async ({ db, args }) => { |
| 61 | + const { email } = Args.check(args) |
| 62 | + const digestData: NotificationEmailDigest = { |
| 63 | + notificationFrequency: "Monthly", |
| 64 | + startDate: new Date(), |
| 65 | + endDate: new Date(), |
| 66 | + bills: [], |
| 67 | + users: [], |
| 68 | + numBillsWithNewTestimony: 0, |
| 69 | + numUsersWithNewTestimony: 0 |
| 70 | + } |
| 71 | + |
| 72 | + const htmlString = renderToHtmlString(digestData) |
| 73 | + |
| 74 | + // Create an email document in /notifications_mails to queue up the send |
| 75 | + await db.collection("notifications_mails").add({ |
| 76 | + to: [email], |
| 77 | + message: { |
| 78 | + subject: "Test Notifications Digest", |
| 79 | + text: "", // blank because we're sending HTML |
| 80 | + html: htmlString |
| 81 | + }, |
| 82 | + createdAt: Timestamp.now() |
| 83 | + }) |
| 84 | +} |
0 commit comments