@kne/fastify-message 1.0.0 → 1.0.2
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/index.js +2 -1
- package/libs/services/main.js +41 -31
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -12,7 +12,8 @@ module.exports = fp(async function (fastify, options) {
|
|
|
12
12
|
throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserModel');
|
|
13
13
|
}
|
|
14
14
|
return fastify.account.models.user;
|
|
15
|
-
}
|
|
15
|
+
},
|
|
16
|
+
senders: {}
|
|
16
17
|
},
|
|
17
18
|
options
|
|
18
19
|
);
|
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');
|
|
9
9
|
|
|
10
10
|
module.exports = fp(async (fastify, options) => {
|
|
11
11
|
const emailConfig = Object.assign(
|
|
@@ -98,40 +98,50 @@ module.exports = fp(async (fastify, options) => {
|
|
|
98
98
|
|
|
99
99
|
const sendMessage = async ({ type = 0, name, props, code, level = 0, client, options: targetOptions }) => {
|
|
100
100
|
targetOptions = Object.assign({}, targetOptions);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
101
|
+
const { content, templateId } = await messageTemplate({ code, type, level, props });
|
|
102
|
+
const sendOptions = await (async () => {
|
|
103
|
+
if (type === 0) {
|
|
104
|
+
const currentClient = merge(
|
|
105
|
+
{},
|
|
106
|
+
{
|
|
107
|
+
host: emailConfig.host,
|
|
108
|
+
port: emailConfig.port,
|
|
109
|
+
secure: emailConfig.secure,
|
|
110
|
+
auth: {
|
|
111
|
+
user: emailConfig.user,
|
|
112
|
+
pass: emailConfig.pass
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
client
|
|
116
|
+
);
|
|
117
|
+
const smtp = nodemailer.createTransport(currentClient);
|
|
116
118
|
|
|
117
|
-
|
|
119
|
+
const mailOptions = {
|
|
120
|
+
...targetOptions,
|
|
121
|
+
from: `"${targetOptions.title || emailConfig.user}" <${emailConfig.user}>`,
|
|
122
|
+
to: name,
|
|
123
|
+
subject: content.subject || options.subject || emailConfig.defaultSubject || 'Message reminder',
|
|
124
|
+
text: content.text || convert(content.html),
|
|
125
|
+
html: content.html,
|
|
126
|
+
attachments: options.attachments || []
|
|
127
|
+
};
|
|
118
128
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
subject: content.subject || options.subject || emailConfig.defaultSubject || 'Message reminder',
|
|
124
|
-
text: content.text || convert(content.html),
|
|
125
|
-
html: content.html,
|
|
126
|
-
attachments: options.attachments || []
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
if (!isTest) {
|
|
130
|
-
await pify(smtp.sendMail.bind(smtp))(mailOptions);
|
|
129
|
+
if (!isTest) {
|
|
130
|
+
await pify(smtp.sendMail.bind(smtp))(mailOptions);
|
|
131
|
+
}
|
|
132
|
+
return mailOptions;
|
|
131
133
|
}
|
|
134
|
+
const currentSender = options.senders?.[type];
|
|
135
|
+
if (typeof currentSender === 'function') {
|
|
136
|
+
const { content, templateId } = await messageTemplate({ code, type, level, props });
|
|
137
|
+
if (!isTest) {
|
|
138
|
+
return await currentSender({ code, templateId, content, props, name, type, level, options: targetOptions });
|
|
139
|
+
}
|
|
132
140
|
|
|
133
|
-
|
|
134
|
-
|
|
141
|
+
return { content, props, level, options: targetOptions };
|
|
142
|
+
}
|
|
143
|
+
})(type);
|
|
144
|
+
await models.record.create({ type, code, templateId, props, name, content: sendOptions });
|
|
135
145
|
};
|
|
136
146
|
|
|
137
147
|
Object.assign(fastify[options.name].services, {
|