all repos — caroster @ 23027a122b6cf9f8018dc50a51bb031d35f1598b

[Octree] Group carpool to your event https://caroster.io

backend/src/api/email/services/email.ts (view raw)

 1import fs from "node:fs/promises";
 2import _ from "lodash";
 3import { marked } from "marked";
 4import { getHTMLMeta } from "../utils/layout";
 5
 6const langs = ["en", "fr"];
 7
 8let locales: Record<
 9  string,
10  {
11    template: Record<"header" | "footer", string>;
12    notifications: Record<string, { title: string; content: string }>;
13  }
14> = null;
15
16export default () => ({
17  async loadContentFiles() {
18    strapi.log.info(
19      `🌐 Load localized content files for email notifications...`
20    );
21    const content = await Promise.all(
22      langs.map(async (lang) => {
23        const langFile = await fs.readFile(
24          `${__dirname}/../locales/${lang}.json`,
25          "utf-8"
26        );
27        return [lang, JSON.parse(langFile)];
28      })
29    );
30    locales = Object.fromEntries(content);
31    return locales;
32  },
33
34  async sendEmailNotif(
35    to: string,
36    notifType: string,
37    lang: string,
38    variables?: object
39  ) {
40    try {
41      const emailTemplate = await this.getEmailTemplate(
42        notifType,
43        lang,
44        variables
45      );
46      if (!emailTemplate)
47        throw new Error(`No locale found for ${notifType} in ${lang}`);
48
49      await strapi.plugins["email"].services.email.send({
50        to,
51        ...emailTemplate,
52      });
53    } catch (error) {
54      strapi.log.error(`Can't send email notification to ${to}`);
55      console.error(error);
56    }
57  },
58
59  async getEmailTemplate(notifType: string, lang: string, variables = {}) {
60    let notif = locales?.[lang]?.notifications?.[notifType];
61
62    if (!notif) {
63      strapi.log.warn(
64        `No email notification locale found for type '${notifType}' and lang '${lang}'`
65      );
66      notif = locales?.["en"]?.notifications?.[notifType];
67      if (!notif) return null;
68    }
69
70    try {
71      const subject = _.template(notif.title)({
72        ...variables,
73        host: strapi.config.server.url,
74      });
75      const mdContent = _.template(notif.content)({
76        ...variables,
77        host: strapi.config.server.url,
78      });
79      const mdHeader = locales?.[lang]?.template.header;
80      const mdFooter = locales?.[lang]?.template.footer;
81      const htmlContent = await marked.parse(mdContent, { breaks: true });
82      const htmlHeader = await marked.parse(mdHeader, { breaks: true });
83      const htmlFooter = await marked.parse(mdFooter, { breaks: true });
84      const html = `${getHTMLMeta()}<main>${htmlHeader}${htmlContent}${htmlFooter}</main>`;
85
86      return {
87        subject,
88        html,
89        text: notif.content,
90      };
91    } catch (error) {
92      strapi.log.error(
93        `Can't parse email notification locale for type '${notifType}' and lang '${lang}'`
94      );
95      console.error(error);
96      return null;
97    }
98  },
99});