@kne/fastify-message 1.0.11 → 1.0.12
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 +4 -1
- package/libs/services/main.js +55 -70
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -20,7 +20,10 @@ module.exports = fp(async function(fastify, options) {
|
|
|
20
20
|
}, options);
|
|
21
21
|
|
|
22
22
|
fastify.register(require('@kne/fastify-statistics'), {
|
|
23
|
-
dbTableNamePrefix: options.dbTableNamePrefix,
|
|
23
|
+
dbTableNamePrefix: options.dbTableNamePrefix,
|
|
24
|
+
modelPrefix: `${options.name}Statistics`,
|
|
25
|
+
prefix: options.prefix,
|
|
26
|
+
name: `${options.name}Statistics`
|
|
24
27
|
});
|
|
25
28
|
|
|
26
29
|
fastify.register(require('@kne/fastify-namespace'), {
|
package/libs/services/main.js
CHANGED
|
@@ -14,20 +14,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
14
14
|
const { models, services } = fastify[options.name];
|
|
15
15
|
|
|
16
16
|
// 采集消息统计数据
|
|
17
|
-
const collectMessageStatistics = async ({ code, type, success =
|
|
17
|
+
const collectMessageStatistics = async ({ code, type, total = 0, success = 0, failed = 0 }) => {
|
|
18
18
|
try {
|
|
19
19
|
const statisticsServices = fastify[`${options.name}Statistics`]?.services;
|
|
20
20
|
if (!statisticsServices) return;
|
|
21
21
|
|
|
22
|
-
const data = { total
|
|
22
|
+
const data = Object.assign({}, { total }, success > 0 ? { success } : {}, failed > 0 ? { failed } : {});
|
|
23
23
|
const unit = { total: 'count', success: 'count', failed: 'count' };
|
|
24
24
|
const time = new Date();
|
|
25
25
|
|
|
26
|
-
// 1-segment channel: code
|
|
27
|
-
await statisticsServices.collect({
|
|
28
|
-
channel: code, data, unit, time
|
|
29
|
-
});
|
|
30
|
-
// 2-segment channel: code:type
|
|
31
26
|
await statisticsServices.collect({
|
|
32
27
|
channel: `${code}:${type}`, data, unit, time
|
|
33
28
|
});
|
|
@@ -104,71 +99,71 @@ module.exports = fp(async (fastify, options) => {
|
|
|
104
99
|
const sendMessage = async ({ type = 0, name, props, code, level = 0, client, options: targetOptions }) => {
|
|
105
100
|
targetOptions = Object.assign({}, targetOptions);
|
|
106
101
|
const { content, templateId } = await messageTemplate({ code, type, level, props });
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
102
|
+
await collectMessageStatistics({ code, type, total: 1 });
|
|
103
|
+
try {
|
|
104
|
+
const sendOptions = await (async () => {
|
|
105
|
+
const currentSender = options.senders?.[type];
|
|
106
|
+
if (type === 0) {
|
|
107
|
+
const mailOptions = {
|
|
108
|
+
...targetOptions,
|
|
109
|
+
from: `"${targetOptions.title || emailConfig.user}" <${emailConfig.user}>`,
|
|
110
|
+
to: name,
|
|
111
|
+
subject: content.subject || options.subject || emailConfig.defaultSubject || 'Message reminder',
|
|
112
|
+
text: content.text || convert(content.html),
|
|
113
|
+
html: content.html,
|
|
114
|
+
attachments: options.attachments || []
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
if (!isTest) {
|
|
118
|
+
if (typeof currentSender === 'function') {
|
|
119
|
+
await currentSender(mailOptions);
|
|
120
|
+
} else {
|
|
121
|
+
const currentClient = merge({}, {
|
|
122
|
+
host: emailConfig.host, port: emailConfig.port, secure: emailConfig.secure, auth: {
|
|
123
|
+
user: emailConfig.user, pass: emailConfig.pass
|
|
124
|
+
}
|
|
125
|
+
}, client);
|
|
126
|
+
const smtp = nodemailer.createTransport(currentClient);
|
|
127
|
+
try {
|
|
128
|
+
await smtp.sendMail(mailOptions);
|
|
129
|
+
} finally {
|
|
130
|
+
smtp.close();
|
|
127
131
|
}
|
|
128
|
-
}, client);
|
|
129
|
-
const smtp = nodemailer.createTransport(currentClient);
|
|
130
|
-
try {
|
|
131
|
-
await smtp.sendMail(mailOptions);
|
|
132
|
-
} finally {
|
|
133
|
-
smtp.close();
|
|
134
132
|
}
|
|
135
133
|
}
|
|
134
|
+
return mailOptions;
|
|
136
135
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return
|
|
136
|
+
if (typeof currentSender === 'function') {
|
|
137
|
+
if (!isTest) {
|
|
138
|
+
return await currentSender({ code, templateId, content, props, name, type, level, options: targetOptions });
|
|
139
|
+
}
|
|
140
|
+
return { content, props, level, options: targetOptions };
|
|
142
141
|
}
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
142
|
+
throw new Error(`未配置类型 ${type} 的消息发送器`);
|
|
143
|
+
})(type);
|
|
144
|
+
await models.record.create({ type, code, templateId, props, name, content: sendOptions });
|
|
145
|
+
await collectMessageStatistics({ code, type, success: 1 });
|
|
146
|
+
} catch (error) {
|
|
147
|
+
await collectMessageStatistics({ code, type, failed: 1 });
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
149
150
|
};
|
|
150
151
|
|
|
151
152
|
Object.assign(fastify[options.name].services, {
|
|
152
153
|
includeTemplate, messageTemplate, parseTemplate, sendMessage,
|
|
153
|
-
|
|
154
|
+
|
|
154
155
|
// 发送记录相关服务
|
|
155
156
|
record: {
|
|
156
157
|
list: async ({ filter = {}, perPage = 20, currentPage = 1 }) => {
|
|
157
158
|
const { count, rows } = await models.record.findAndCountAll({
|
|
158
|
-
where: filter,
|
|
159
|
-
limit: perPage,
|
|
160
|
-
offset: (currentPage - 1) * perPage,
|
|
161
|
-
order: [['createdAt', 'DESC']]
|
|
159
|
+
where: filter, limit: perPage, offset: (currentPage - 1) * perPage, order: [['createdAt', 'DESC']]
|
|
162
160
|
});
|
|
163
|
-
|
|
161
|
+
|
|
164
162
|
return {
|
|
165
|
-
pageData: rows,
|
|
166
|
-
totalCount: count,
|
|
167
|
-
perPage,
|
|
168
|
-
currentPage
|
|
163
|
+
pageData: rows, totalCount: count, perPage, currentPage
|
|
169
164
|
};
|
|
170
165
|
},
|
|
171
|
-
|
|
166
|
+
|
|
172
167
|
detail: async ({ id }) => {
|
|
173
168
|
const record = await models.record.findByPk(id);
|
|
174
169
|
if (!record) {
|
|
@@ -182,20 +177,14 @@ module.exports = fp(async (fastify, options) => {
|
|
|
182
177
|
template: {
|
|
183
178
|
list: async ({ filter = {}, perPage = 20, currentPage = 1 }) => {
|
|
184
179
|
const { count, rows } = await models.template.findAndCountAll({
|
|
185
|
-
where: filter,
|
|
186
|
-
limit: perPage,
|
|
187
|
-
offset: (currentPage - 1) * perPage,
|
|
188
|
-
order: [['createdAt', 'DESC']]
|
|
180
|
+
where: filter, limit: perPage, offset: (currentPage - 1) * perPage, order: [['createdAt', 'DESC']]
|
|
189
181
|
});
|
|
190
|
-
|
|
182
|
+
|
|
191
183
|
return {
|
|
192
|
-
pageData: rows,
|
|
193
|
-
totalCount: count,
|
|
194
|
-
perPage,
|
|
195
|
-
currentPage
|
|
184
|
+
pageData: rows, totalCount: count, perPage, currentPage
|
|
196
185
|
};
|
|
197
186
|
},
|
|
198
|
-
|
|
187
|
+
|
|
199
188
|
detail: async ({ id }) => {
|
|
200
189
|
const template = await models.template.findByPk(id);
|
|
201
190
|
if (!template) {
|
|
@@ -213,11 +202,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
213
202
|
throw new Error('模版已禁用,无法发送消息');
|
|
214
203
|
}
|
|
215
204
|
await sendMessage({
|
|
216
|
-
type: tpl.type,
|
|
217
|
-
name,
|
|
218
|
-
props,
|
|
219
|
-
code: tpl.code,
|
|
220
|
-
level: tpl.level
|
|
205
|
+
type: tpl.type, name, props, code: tpl.code, level: tpl.level
|
|
221
206
|
});
|
|
222
207
|
return { success: true };
|
|
223
208
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-message",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "管理消息发送,支持sms消息,email消息等.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"sqlite3": "^6.0.1"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@kne/fastify-statistics": "^0.1.0
|
|
63
|
+
"@kne/fastify-statistics": "^0.1.0",
|
|
64
64
|
"dayjs": "^1.11.20",
|
|
65
65
|
"fs-extra": "^11.3.0",
|
|
66
66
|
"html-to-text": "^9.0.5",
|