summaryrefslogtreecommitdiff
path: root/src/lib/handlebars.ts
blob: 42f801030406d1f37d61adbb95f7849d9013297c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Request } from "express";
import { ExpressHandlebars } from "express-handlebars";

export const renderTemplate = async (
    req: Request,
    templateName: string,
    data: Record<string, unknown>,
): Promise<string> => {
    return new Promise<string>((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 const renderEmail = async (
    hbsInstance: ExpressHandlebars,
    templateName: string,
    data: Record<string, unknown>,
): 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 }
}