-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (104 loc) · 3.9 KB
/
index.js
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
require('dotenv').config();
require('module-alias/register')
const port = process.env.PORT || 80;
//This timeout is used to delay accepting connections until the server is fully loaded.
//It could come to a crash if a request comes in before the settings cache was fully laoded.
const { log } = require('@lib/logger');
const path = require('path');
const fs = require('fs');
process.log = {};
process.log = log;
const { Worker } = require('bullmq');
const nodemailer = require("nodemailer");
const i18next = require('i18next');
const { GetUserData } = require('@lib/postgres');
const { replacePlaceholders } = require('@lib/util');
const { addConfirmationToken, addResetPasswordToken } = require('@lib/redis');
const emailTemplateFolder = path.join(__dirname, 'templates');
const mailTemplateStore = {};
fs.readdirSync(emailTemplateFolder).forEach((file) => {
if (path.extname(file) === '.js') {
const filename = path.basename(file, '.js');
mailTemplateStore[filename] = require(path.join(emailTemplateFolder, file));
}
});
const translationStore = {
de: require(path.join(emailTemplateFolder, 'lang', 'de.json')),
en: require(path.join(emailTemplateFolder, 'lang', 'en.json')),
};
const connection = {
port: parseInt(process.env.REDIS_PORT) || 6379,
host: process.env.REDIS_HOST || "127.0.0.1",
username: process.env.REDIS_USER || "default",
password: process.env.REDIS_PASSWORD || "default",
db: parseInt(process.env.REDIS_DB) + 1 || 1,
};
const emailtransporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: true,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
}
});
(async () => {
await i18next.init({
lng: 'de',
fallbackLng: 'de',
resources: translationStore,
});
const emailWorker = new Worker('q:mail', async (job) => {
try {
const userData = await GetUserData(job.data.userId);
let emailText;
process.log.debug(`Sending email to ${userData.email} with type: ${job.name}`);
const lang = userData.language || process.env.FALLBACKLANG;
const t = i18next.getFixedT(lang);
switch (job.name) {
case 'user:email_verification':
emailText = mailTemplateStore.email_verification_text.generate(t, {
username: userData.username,
regUrl: `${job.data.appDomain}/api/v1/register/${job.data.urlPath}`
});
await emailtransporter.sendMail({
from: `${process.env.COMPANYNAME} - Webpanel <${process.env.SMTP_USER}>`,
to: userData.email,
subject: t('subject.registerMail', { companyName: process.env.COMPANYNAME }),
text: emailText,
});
await addConfirmationToken(job.data.urlPath, job.data.userId);
break;
case 'user:login':
// Send login email
break;
case 'user:reset_password':
const emailText = mailTemplateStore.email_passwordReset_text.generate(t, {
username: userData.username,
regUrl: `${job.data.appDomain}/api/v1/register/${job.data.urlPath}`
});
// Send email verification
await emailtransporter.sendMail({
from: `${process.env.COMPANYNAME} - Webpanel <${process.env.SMTP_USER}>`,
to: userData.email,
subject: t('subject.passwordReset', { companyName: process.env.COMPANYNAME }),
text: emailText,
});
// Add password reset token to Redis
await addResetPasswordToken(job.data.urlPath, job.data.userId);
break;
default:
throw new Error(`Invalid email type: ${job.name}`);
}
} catch (error) {
process.log.error(error);
process.log.error(JSON.stringify(job.data));
throw error;
}
return;
}, {
connection: connection,
removeOnComplete: { count: 1 },
removeOnFail: { count: 50 },
});
})();