@kne/fastify-message 1.0.0-alpha.0 → 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/libs/models/record.js +2 -2
- package/libs/services/main.js +12 -10
- package/package.json +1 -1
package/libs/models/record.js
CHANGED
package/libs/services/main.js
CHANGED
|
@@ -5,7 +5,7 @@ const template = require('lodash/template');
|
|
|
5
5
|
const merge = require('lodash/merge');
|
|
6
6
|
const nodemailer = require('nodemailer');
|
|
7
7
|
const { convert } = require('html-to-text');
|
|
8
|
-
const pify = require('pify');
|
|
8
|
+
const pify = require('pify').default;
|
|
9
9
|
|
|
10
10
|
module.exports = fp(async (fastify, options) => {
|
|
11
11
|
const emailConfig = Object.assign(
|
|
@@ -25,7 +25,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
25
25
|
const list = await fs.readdir(dir);
|
|
26
26
|
for (const file of list) {
|
|
27
27
|
const filePath = path.join(dir, file);
|
|
28
|
-
const content = fs.readFile(filePath, 'utf8');
|
|
28
|
+
const content = await fs.readFile(filePath, 'utf8');
|
|
29
29
|
const filename = file.replace(path.extname(file), '');
|
|
30
30
|
const tempArray = filename.split('_');
|
|
31
31
|
const code = tempArray[0],
|
|
@@ -38,12 +38,14 @@ module.exports = fp(async (fastify, options) => {
|
|
|
38
38
|
level: 0
|
|
39
39
|
}
|
|
40
40
|
});
|
|
41
|
+
|
|
41
42
|
if (codeTemplate) {
|
|
42
43
|
codeTemplate.content = content;
|
|
43
44
|
codeTemplate.name = name;
|
|
44
45
|
await codeTemplate.save();
|
|
45
46
|
continue;
|
|
46
47
|
}
|
|
48
|
+
|
|
47
49
|
await models.template.create({
|
|
48
50
|
code,
|
|
49
51
|
type,
|
|
@@ -61,8 +63,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
61
63
|
|
|
62
64
|
while ((match = regex.exec(text)) !== null) {
|
|
63
65
|
const fieldName = match[1].trim();
|
|
64
|
-
|
|
65
|
-
result[fieldName] = fieldValue.replace(/\n/g, '\\n');
|
|
66
|
+
result[fieldName] = match[2].trim();
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
return result;
|
|
@@ -91,7 +92,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
91
92
|
};
|
|
92
93
|
};
|
|
93
94
|
|
|
94
|
-
const sendMessage = async ({ type = 0, name, props, code, level = 0, client, options }) => {
|
|
95
|
+
const sendMessage = async ({ type = 0, name, props, code, level = 0, client, options: targetOptions }) => {
|
|
96
|
+
targetOptions = Object.assign({}, targetOptions);
|
|
95
97
|
if (type === 0) {
|
|
96
98
|
const currentClient = merge(
|
|
97
99
|
{},
|
|
@@ -108,19 +110,19 @@ module.exports = fp(async (fastify, options) => {
|
|
|
108
110
|
);
|
|
109
111
|
const smtp = nodemailer.createTransport(currentClient);
|
|
110
112
|
|
|
111
|
-
const { content,
|
|
113
|
+
const { content, templateId } = await messageTemplate({ code, type, level, props });
|
|
112
114
|
|
|
113
115
|
const mailOptions = {
|
|
114
|
-
...
|
|
115
|
-
from: `"${
|
|
116
|
+
...targetOptions,
|
|
117
|
+
from: `"${targetOptions.title || emailConfig.user}" <${emailConfig.user}>`,
|
|
116
118
|
to: name,
|
|
117
119
|
subject: content.subject || options.subject || emailConfig.defaultSubject || 'Message reminder',
|
|
118
|
-
html: content.html,
|
|
119
120
|
text: content.text || convert(content.html),
|
|
121
|
+
html: content.html,
|
|
120
122
|
attachments: options.attachments || []
|
|
121
123
|
};
|
|
122
124
|
|
|
123
|
-
if (isTest) {
|
|
125
|
+
if (!isTest) {
|
|
124
126
|
await pify(smtp.sendMail.bind(smtp))(mailOptions);
|
|
125
127
|
}
|
|
126
128
|
|