From aace2c7e6ccb6e74df83faac74c427d43bfaf79b Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Wed, 23 Apr 2025 15:06:54 -0700 Subject: Fix ReferenceError: nodemailerTransporter is not defined Part of https://github.com/lowercasename/gathio/pull/200 was migrating more code to use the shared init email function, but all the local usages of nodemailerTransporter were missed --- src/lib/handlebars.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/lib/handlebars.ts') diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts index d5a8b6e..42f8010 100644 --- a/src/lib/handlebars.ts +++ b/src/lib/handlebars.ts @@ -1,4 +1,5 @@ import { Request } from "express"; +import { ExpressHandlebars } from "express-handlebars"; export const renderTemplate = async ( req: Request, @@ -21,3 +22,29 @@ export const renderTemplate = async ( ); }); }; + +export const renderEmail = async ( + hbsInstance: ExpressHandlebars, + templateName: string, + data: Record, +): Promise<{ html: string, text: string }> => { + const [html, text] = await Promise.all([ + hbsInstance.renderView( + `./views/emails/${templateName}Html.handlebars`, + { + cache: true, + layout: "email.handlebars", + ...data, + } + ), + hbsInstance.renderView( + `./views/emails/${templateName}Text.handlebars`, + { + cache: true, + layout: "email.handlebars", + ...data, + } + ), + ]); + return { html, text } +} -- cgit v1.2.3 From a8a17443c2d070d2d23920ffff7e4a43c905698c Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Wed, 23 Apr 2025 17:27:55 -0700 Subject: Refactor for everywhere to use sendEmailFromTemplate everywhere * Created a singleton to house handlebars so req doesn't need to be passed everywhere (should make unit testing easier later) * Subjectline for sendgrid and nodemailer are both always prefixed in sendEmail() * removed prefix subjectline from all other email places * added a couple if (!event) { return 404 } to help make typescript happy * some minor eslint auto fixes (looks like let => const where it can) --- src/lib/handlebars.ts | 97 +++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 46 deletions(-) (limited to 'src/lib/handlebars.ts') diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts index 42f8010..6d4f796 100644 --- a/src/lib/handlebars.ts +++ b/src/lib/handlebars.ts @@ -1,50 +1,55 @@ -import { Request } from "express"; -import { ExpressHandlebars } from "express-handlebars"; +import hbs, { ExpressHandlebars } from "express-handlebars"; +import { RenderViewOptions } from "express-handlebars/types/index.js"; -export const renderTemplate = async ( - req: Request, - templateName: string, - data: Record, -): Promise => { - return new Promise((resolve, reject) => { - req.app - .get("hbsInstance") - .renderView( - `./views/emails/${templateName}.handlebars`, - data, - (err: any, html: string) => { - if (err) { - console.error(err); - reject(err); - } - resolve(html); +export class HandlebarsSingleton { + static #instance: HandlebarsSingleton; + hbsInstance: hbs.ExpressHandlebars; + + private constructor() { + this.hbsInstance = hbs.create({ + defaultLayout: "main", + partialsDir: ["views/partials/"], + layoutsDir: "views/layouts/", + helpers: { + plural: function (number: number, text: string) { + const singular = number === 1; + // If no text parameter was given, just return a conditional s. + if (typeof text !== "string") return singular ? "" : "s"; + // Split with regex into group1/group2 or group1(group3) + const match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); + // If no match, just append a conditional s. + if (!match) return text + (singular ? "" : "s"); + // We have a good match, so fire away + return ( + (singular && match[1]) || // Singular case + match[2] || // Plural case: 'bagel/bagels' --> bagels + match[1] + (match[3] || "s") + ); // Plural case: 'bagel(s)' or 'bagel' --> bagels + }, + json: function (context: object) { + return JSON.stringify(context); }, - ); - }); -}; + }, + }); + } + + public static get instance(): HandlebarsSingleton { + if (!HandlebarsSingleton.#instance) { + HandlebarsSingleton.#instance = new HandlebarsSingleton(); + } + + return HandlebarsSingleton.#instance; + } + + public get engine(): ExpressHandlebars["engine"] { + return this.hbsInstance.engine; + } -export const renderEmail = async ( - hbsInstance: ExpressHandlebars, - templateName: string, - data: Record, -): Promise<{ html: string, text: string }> => { - const [html, text] = await Promise.all([ - hbsInstance.renderView( - `./views/emails/${templateName}Html.handlebars`, - { - cache: true, - layout: "email.handlebars", - ...data, - } - ), - hbsInstance.renderView( - `./views/emails/${templateName}Text.handlebars`, - { - cache: true, - layout: "email.handlebars", - ...data, - } - ), - ]); - return { html, text } + /** + * Finally, any singleton can define some business logic, which can be + * executed on its instance. + */ + public renderView(viewPath: string, options: RenderViewOptions): Promise { + return this.hbsInstance.renderView(viewPath, options); + } } -- cgit v1.2.3 From 14041a319cace03cfc23c0a919ed81fb141f88ce Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Fri, 25 Apr 2025 21:43:39 -0700 Subject: Refactor to have email service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move hbsInstance back to app * Add email and hbs to req so typescript 🎉🎉🎉 * Init Email and config once --- src/lib/handlebars.ts | 55 --------------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 src/lib/handlebars.ts (limited to 'src/lib/handlebars.ts') diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts deleted file mode 100644 index 6d4f796..0000000 --- a/src/lib/handlebars.ts +++ /dev/null @@ -1,55 +0,0 @@ -import hbs, { ExpressHandlebars } from "express-handlebars"; -import { RenderViewOptions } from "express-handlebars/types/index.js"; - -export class HandlebarsSingleton { - static #instance: HandlebarsSingleton; - hbsInstance: hbs.ExpressHandlebars; - - private constructor() { - this.hbsInstance = hbs.create({ - defaultLayout: "main", - partialsDir: ["views/partials/"], - layoutsDir: "views/layouts/", - helpers: { - plural: function (number: number, text: string) { - const singular = number === 1; - // If no text parameter was given, just return a conditional s. - if (typeof text !== "string") return singular ? "" : "s"; - // Split with regex into group1/group2 or group1(group3) - const match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); - // If no match, just append a conditional s. - if (!match) return text + (singular ? "" : "s"); - // We have a good match, so fire away - return ( - (singular && match[1]) || // Singular case - match[2] || // Plural case: 'bagel/bagels' --> bagels - match[1] + (match[3] || "s") - ); // Plural case: 'bagel(s)' or 'bagel' --> bagels - }, - json: function (context: object) { - return JSON.stringify(context); - }, - }, - }); - } - - public static get instance(): HandlebarsSingleton { - if (!HandlebarsSingleton.#instance) { - HandlebarsSingleton.#instance = new HandlebarsSingleton(); - } - - return HandlebarsSingleton.#instance; - } - - public get engine(): ExpressHandlebars["engine"] { - return this.hbsInstance.engine; - } - - /** - * Finally, any singleton can define some business logic, which can be - * executed on its instance. - */ - public renderView(viewPath: string, options: RenderViewOptions): Promise { - return this.hbsInstance.renderView(viewPath, options); - } -} -- cgit v1.2.3