-
Notifications
You must be signed in to change notification settings - Fork 3
/
mail-body-generator.ts
137 lines (121 loc) · 4.26 KB
/
mail-body-generator.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the mail-body-generator.
*
* @module internal/mailer
*/
import { Language, MailLanguageMap } from './mail-message';
import User from '../entity/user/user';
import fs from 'fs';
import path from 'path';
interface TemplateFields {
subject: string;
htmlSubject: string;
shortTitle: string;
body: string;
weekDay: string;
date: string;
serviceEmail: string;
reasonForEmail: string;
}
export const templateFieldDefault: Record<
keyof Pick<TemplateFields, 'serviceEmail' | 'reasonForEmail'>
, { [key in Language]: string }
> = {
serviceEmail: {
'en-US': process.env.SMTP_FROM?.split('<')[1].split('>')[0] || '',
'nl-NL': process.env.SMTP_FROM?.split('<')[1].split('>')[0] || '',
},
reasonForEmail: {
'en-US': 'You are receiving this email because you are registered as a SudoSOS user. Learn more about how we treat your personal data on <a href="https://gew.is/privacy">https://gew.is/privacy</a>.',
'nl-NL': 'Je ontvangt deze email omdat je bent geregistreerd als een SudoSOS gebruiker. Lees hoe wij je persoonlijke informatie verwerken op <a href="https://gew.is/privacy">https://gew.is/privacy</a>.',
},
};
export default class MailBodyGenerator<T> {
private readonly template: string;
constructor(private language: Language) {
this.template = fs.readFileSync(path.join(__dirname, '../../static/mailer/template.html')).toString();
}
/**
* Get a localized salutation (including the comma afterward)
* @private
*/
private getLocalizedSalutation(to: User) {
switch (this.language) {
case Language.DUTCH:
return `Beste ${to.firstName}`;
case Language.ENGLISH:
return `Dear ${to.firstName}`;
default:
throw new Error(`Unknown language: "${this.language}"`);
}
}
private getLocalizedClosing() {
switch (this.language) {
case Language.DUTCH:
return `Met vriendelijke groet,
SudoSOS`;
case Language.ENGLISH:
return `Kind regards,
SudoSOS`;
default:
throw new Error(`Unknown language: "${this.language}"`);
}
}
/**
* Add a salutation to the given html in the given language for the given user
* @param html
* @param to
* @private
*/
public getHtmlWithSalutation(html: string, to: User) {
return `<p>${this.getLocalizedSalutation(to)},</p>
${html}`;
}
public getTextWithSalutation(text: string, to: User) {
return `${this.getLocalizedSalutation(to)},
${text}
${this.getLocalizedClosing()}`;
}
public getContents(
contents: MailLanguageMap<T>,
options: T,
to: User,
) {
const { text, html, subject, title, reason } = contents[this.language].getContent(options);
let styledHtml = this.template;
const styledHtmlTemplateFields: TemplateFields = {
subject,
htmlSubject: subject.replaceAll(' ', ' '),
body: this.getHtmlWithSalutation(html, to),
shortTitle: title,
weekDay: new Date().toLocaleString(this.language, { weekday: 'long' }),
date: new Date().toLocaleDateString(this.language, { day: 'numeric', month: 'long', year: 'numeric' }),
serviceEmail: templateFieldDefault.serviceEmail[this.language],
reasonForEmail: reason ?? templateFieldDefault.reasonForEmail[this.language],
};
Object.entries(styledHtmlTemplateFields).forEach(([key, value]) => {
styledHtml = styledHtml.replaceAll(`{{ ${key} }}`, value);
});
const styledText = this.getTextWithSalutation(text, to);
return { text: styledText, html: styledHtml, subject };
}
}