@kne/fastify-message 1.0.3 → 1.0.5
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 +12 -2
- package/libs/controllers/main.js +239 -12
- package/libs/services/main.js +73 -1
- package/package.json +11 -3
package/index.js
CHANGED
|
@@ -7,13 +7,22 @@ module.exports = fp(async function (fastify, options) {
|
|
|
7
7
|
{
|
|
8
8
|
name: 'message',
|
|
9
9
|
dbTableNamePrefix: 't_message_',
|
|
10
|
+
prefix: '/api/v1/message',
|
|
10
11
|
getUserModel: () => {
|
|
11
12
|
if (!fastify.account) {
|
|
12
13
|
throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserModel');
|
|
13
14
|
}
|
|
14
15
|
return fastify.account.models.user;
|
|
15
16
|
},
|
|
16
|
-
senders: {}
|
|
17
|
+
senders: {},
|
|
18
|
+
getAuthenticate: (type) => {
|
|
19
|
+
switch (type) {
|
|
20
|
+
case 'record':
|
|
21
|
+
case 'template':
|
|
22
|
+
default:
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
17
26
|
},
|
|
18
27
|
options
|
|
19
28
|
);
|
|
@@ -29,7 +38,8 @@ module.exports = fp(async function (fastify, options) {
|
|
|
29
38
|
getUserModel: options.getUserModel
|
|
30
39
|
})
|
|
31
40
|
],
|
|
32
|
-
['services', path.resolve(__dirname, './libs/services')]
|
|
41
|
+
['services', path.resolve(__dirname, './libs/services')],
|
|
42
|
+
['controllers', path.resolve(__dirname, './libs/controllers')]
|
|
33
43
|
]
|
|
34
44
|
});
|
|
35
45
|
|
package/libs/controllers/main.js
CHANGED
|
@@ -1,24 +1,200 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
|
|
3
3
|
module.exports = fp(async (fastify, options) => {
|
|
4
|
-
const { services } = fastify.
|
|
4
|
+
const { services } = fastify[options.name];
|
|
5
|
+
|
|
6
|
+
// 查看发送记录列表
|
|
7
|
+
fastify.get(
|
|
8
|
+
`${options.prefix}/records`,
|
|
9
|
+
{
|
|
10
|
+
onRequest: options.getAuthenticate('record'),
|
|
11
|
+
schema: {
|
|
12
|
+
description: '获取消息发送记录列表',
|
|
13
|
+
summary: '查看发送记录',
|
|
14
|
+
querystring: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
currentPage: { type: 'integer', default: 1, description: '页码' },
|
|
18
|
+
perPage: { type: 'integer', default: 20, description: '每页数量' },
|
|
19
|
+
filter: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
default: {},
|
|
22
|
+
properties: {
|
|
23
|
+
type: { type: 'integer', description: '发送类型: 0=邮件, 1=短信' },
|
|
24
|
+
code: { type: 'string', description: '模板编码' },
|
|
25
|
+
name: { type: 'string', description: '发送对象(邮箱/手机号)' }
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
response: {
|
|
31
|
+
200: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
properties: {
|
|
34
|
+
totalCount: { type: 'integer', description: '总数量' },
|
|
35
|
+
currentPage: { type: 'integer', description: '当前页码' },
|
|
36
|
+
perPage: { type: 'integer', description: '每页数量' },
|
|
37
|
+
pageData: {
|
|
38
|
+
type: 'array',
|
|
39
|
+
items: {
|
|
40
|
+
type: 'object',
|
|
41
|
+
properties: {
|
|
42
|
+
id: { type: 'string', description: '记录ID' },
|
|
43
|
+
name: { type: 'string', description: '发送对象(邮箱/手机号)' },
|
|
44
|
+
type: { type: 'integer', description: '发送类型: 0=邮件, 1=短信' },
|
|
45
|
+
code: { type: 'string', description: '模板编码' },
|
|
46
|
+
props: { type: 'object', description: '模板变量', additionalProperties: true },
|
|
47
|
+
content: { type: 'object', description: '消息内容', additionalProperties: true },
|
|
48
|
+
templateId: { type: 'string', description: '模板ID' },
|
|
49
|
+
createdAt: { type: 'string', description: '创建时间' }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
async request => {
|
|
59
|
+
const { currentPage = 1, perPage = 20, filter = {} } = request.query;
|
|
60
|
+
const { type, code, name } = filter;
|
|
61
|
+
|
|
62
|
+
const where = {};
|
|
63
|
+
if (type !== undefined) where.type = type;
|
|
64
|
+
if (code) where.code = code;
|
|
65
|
+
if (name) where.name = name;
|
|
66
|
+
|
|
67
|
+
return await services.record.list({ filter: where, perPage, currentPage });
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// 发送消息
|
|
72
|
+
fastify.post(
|
|
73
|
+
`${options.prefix}/templates/send`,
|
|
74
|
+
{
|
|
75
|
+
onRequest: options.getAuthenticate('template:send'),
|
|
76
|
+
schema: {
|
|
77
|
+
description: '根据模版发送消息',
|
|
78
|
+
summary: '发送消息',
|
|
79
|
+
body: {
|
|
80
|
+
type: 'object',
|
|
81
|
+
required: ['templateId', 'name'],
|
|
82
|
+
properties: {
|
|
83
|
+
templateId: { type: 'string', description: '模版ID' },
|
|
84
|
+
name: { type: 'string', description: '发送对象(邮箱/手机号)' },
|
|
85
|
+
props: { type: 'object', description: '模版变量', additionalProperties: true }
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
response: {
|
|
89
|
+
200: {
|
|
90
|
+
type: 'object',
|
|
91
|
+
properties: {
|
|
92
|
+
success: { type: 'boolean', description: '是否发送成功' }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
async request => {
|
|
99
|
+
const { templateId, name, props = {} } = request.body;
|
|
100
|
+
try {
|
|
101
|
+
return await services.template.send({ templateId, name, props });
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error.message === '模版已禁用,无法发送消息') {
|
|
104
|
+
throw fastify.httpErrors.badRequest(error.message);
|
|
105
|
+
}
|
|
106
|
+
throw fastify.httpErrors.notFound(error.message);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// 查看单条发送记录
|
|
112
|
+
fastify.get(
|
|
113
|
+
`${options.prefix}/records/:id`,
|
|
114
|
+
{
|
|
115
|
+
onRequest: options.getAuthenticate('record'),
|
|
116
|
+
schema: {
|
|
117
|
+
description: '获取单条消息发送记录详情',
|
|
118
|
+
summary: '查看发送记录详情',
|
|
119
|
+
params: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
id: { type: 'string', description: '记录ID' }
|
|
123
|
+
},
|
|
124
|
+
required: ['id']
|
|
125
|
+
},
|
|
126
|
+
response: {
|
|
127
|
+
200: {
|
|
128
|
+
type: 'object',
|
|
129
|
+
properties: {
|
|
130
|
+
id: { type: 'string', description: '记录ID' },
|
|
131
|
+
name: { type: 'string', description: '发送对象(邮箱/手机号)' },
|
|
132
|
+
type: { type: 'integer', description: '发送类型: 0=邮件, 1=短信' },
|
|
133
|
+
code: { type: 'string', description: '模板编码' },
|
|
134
|
+
props: { type: 'object', description: '模板变量', additionalProperties: true },
|
|
135
|
+
content: { type: 'object', description: '消息内容', additionalProperties: true },
|
|
136
|
+
templateId: { type: 'string', description: '模板ID' },
|
|
137
|
+
createdAt: { type: 'string', description: '创建时间' }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
async request => {
|
|
144
|
+
const { id } = request.params;
|
|
145
|
+
try {
|
|
146
|
+
return await services.record.detail({ id });
|
|
147
|
+
} catch (error) {
|
|
148
|
+
throw fastify.httpErrors.notFound(error.message);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// 查看消息模版列表
|
|
5
154
|
fastify.get(
|
|
6
|
-
`${options.prefix}/
|
|
155
|
+
`${options.prefix}/templates`,
|
|
7
156
|
{
|
|
8
|
-
onRequest:
|
|
157
|
+
onRequest: options.getAuthenticate('template'),
|
|
9
158
|
schema: {
|
|
10
|
-
description: '
|
|
11
|
-
summary: '
|
|
12
|
-
|
|
159
|
+
description: '获取消息模版列表',
|
|
160
|
+
summary: '查看消息模版',
|
|
161
|
+
querystring: {
|
|
162
|
+
type: 'object',
|
|
163
|
+
properties: {
|
|
164
|
+
currentPage: { type: 'integer', default: 1, description: '页码' },
|
|
165
|
+
perPage: { type: 'integer', default: 20, description: '每页数量' },
|
|
166
|
+
filter: {
|
|
167
|
+
type: 'object',
|
|
168
|
+
default: {},
|
|
169
|
+
properties: {
|
|
170
|
+
type: { type: 'integer', description: '模版类型: 0=邮件, 1=短信' },
|
|
171
|
+
code: { type: 'string', description: '模版编码' },
|
|
172
|
+
level: { type: 'integer', description: '模版级别: 0=系统, 1=业务' },
|
|
173
|
+
status: { type: 'integer', description: '状态: 0=启用, 1=禁用' }
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
},
|
|
13
178
|
response: {
|
|
14
179
|
200: {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
'
|
|
18
|
-
|
|
180
|
+
type: 'object',
|
|
181
|
+
properties: {
|
|
182
|
+
totalCount: { type: 'integer', description: '总数量' },
|
|
183
|
+
currentPage: { type: 'integer', description: '当前页码' },
|
|
184
|
+
perPage: { type: 'integer', description: '每页数量' },
|
|
185
|
+
pageData: {
|
|
186
|
+
type: 'array',
|
|
187
|
+
items: {
|
|
19
188
|
type: 'object',
|
|
20
189
|
properties: {
|
|
21
|
-
|
|
190
|
+
id: { type: 'string', description: '模版ID' },
|
|
191
|
+
name: { type: 'string', description: '模版名称' },
|
|
192
|
+
code: { type: 'string', description: '模版编码' },
|
|
193
|
+
type: { type: 'integer', description: '模版类型: 0=邮件, 1=短信' },
|
|
194
|
+
level: { type: 'integer', description: '模版级别: 0=系统, 1=业务' },
|
|
195
|
+
status: { type: 'integer', description: '状态: 0=启用, 1=禁用' },
|
|
196
|
+
content: { type: 'string', description: '模版内容' },
|
|
197
|
+
createdAt: { type: 'string', description: '创建时间' }
|
|
22
198
|
}
|
|
23
199
|
}
|
|
24
200
|
}
|
|
@@ -28,7 +204,58 @@ module.exports = fp(async (fastify, options) => {
|
|
|
28
204
|
}
|
|
29
205
|
},
|
|
30
206
|
async request => {
|
|
31
|
-
|
|
207
|
+
const { currentPage = 1, perPage = 20, filter = {} } = request.query;
|
|
208
|
+
const { type, code, level, status } = filter;
|
|
209
|
+
|
|
210
|
+
const where = {};
|
|
211
|
+
if (type !== undefined) where.type = type;
|
|
212
|
+
if (code) where.code = code;
|
|
213
|
+
if (level !== undefined) where.level = level;
|
|
214
|
+
if (status !== undefined) where.status = status;
|
|
215
|
+
|
|
216
|
+
return await services.template.list({ filter: where, perPage, currentPage });
|
|
217
|
+
}
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// 查看单个消息模版
|
|
221
|
+
fastify.get(
|
|
222
|
+
`${options.prefix}/templates/:id`,
|
|
223
|
+
{
|
|
224
|
+
onRequest: options.getAuthenticate('template'),
|
|
225
|
+
schema: {
|
|
226
|
+
description: '获取单个消息模版详情',
|
|
227
|
+
summary: '查看消息模版详情',
|
|
228
|
+
params: {
|
|
229
|
+
type: 'object',
|
|
230
|
+
properties: {
|
|
231
|
+
id: { type: 'string', description: '模版ID' }
|
|
232
|
+
},
|
|
233
|
+
required: ['id']
|
|
234
|
+
},
|
|
235
|
+
response: {
|
|
236
|
+
200: {
|
|
237
|
+
type: 'object',
|
|
238
|
+
properties: {
|
|
239
|
+
id: { type: 'string', description: '模版ID' },
|
|
240
|
+
name: { type: 'string', description: '模版名称' },
|
|
241
|
+
code: { type: 'string', description: '模版编码' },
|
|
242
|
+
type: { type: 'integer', description: '模版类型: 0=邮件, 1=短信' },
|
|
243
|
+
level: { type: 'integer', description: '模版级别: 0=系统, 1=业务' },
|
|
244
|
+
status: { type: 'integer', description: '状态: 0=启用, 1=禁用' },
|
|
245
|
+
content: { type: 'string', description: '模版内容' },
|
|
246
|
+
createdAt: { type: 'string', description: '创建时间' }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
async request => {
|
|
253
|
+
const { id } = request.params;
|
|
254
|
+
try {
|
|
255
|
+
return await services.template.detail({ id });
|
|
256
|
+
} catch (error) {
|
|
257
|
+
throw fastify.httpErrors.notFound(error.message);
|
|
258
|
+
}
|
|
32
259
|
}
|
|
33
260
|
);
|
|
34
261
|
});
|
package/libs/services/main.js
CHANGED
|
@@ -121,6 +121,78 @@ module.exports = fp(async (fastify, options) => {
|
|
|
121
121
|
};
|
|
122
122
|
|
|
123
123
|
Object.assign(fastify[options.name].services, {
|
|
124
|
-
includeTemplate, messageTemplate, parseTemplate, sendMessage
|
|
124
|
+
includeTemplate, messageTemplate, parseTemplate, sendMessage,
|
|
125
|
+
|
|
126
|
+
// 发送记录相关服务
|
|
127
|
+
record: {
|
|
128
|
+
list: async ({ filter = {}, perPage = 20, currentPage = 1 }) => {
|
|
129
|
+
const { count, rows } = await models.record.findAndCountAll({
|
|
130
|
+
where: filter,
|
|
131
|
+
limit: perPage,
|
|
132
|
+
offset: (currentPage - 1) * perPage,
|
|
133
|
+
order: [['createdAt', 'DESC']]
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
pageData: rows,
|
|
138
|
+
totalCount: count,
|
|
139
|
+
perPage,
|
|
140
|
+
currentPage
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
detail: async ({ id }) => {
|
|
145
|
+
const record = await models.record.findByPk(id);
|
|
146
|
+
if (!record) {
|
|
147
|
+
throw new Error('记录不存在');
|
|
148
|
+
}
|
|
149
|
+
return record;
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
// 消息模版相关服务
|
|
154
|
+
template: {
|
|
155
|
+
list: async ({ filter = {}, perPage = 20, currentPage = 1 }) => {
|
|
156
|
+
const { count, rows } = await models.template.findAndCountAll({
|
|
157
|
+
where: filter,
|
|
158
|
+
limit: perPage,
|
|
159
|
+
offset: (currentPage - 1) * perPage,
|
|
160
|
+
order: [['createdAt', 'DESC']]
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
pageData: rows,
|
|
165
|
+
totalCount: count,
|
|
166
|
+
perPage,
|
|
167
|
+
currentPage
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
detail: async ({ id }) => {
|
|
172
|
+
const template = await models.template.findByPk(id);
|
|
173
|
+
if (!template) {
|
|
174
|
+
throw new Error('模版不存在');
|
|
175
|
+
}
|
|
176
|
+
return template;
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
send: async ({ templateId, name, props = {} }) => {
|
|
180
|
+
const tpl = await models.template.findByPk(templateId);
|
|
181
|
+
if (!tpl) {
|
|
182
|
+
throw new Error('模版不存在');
|
|
183
|
+
}
|
|
184
|
+
if (tpl.status !== 0) {
|
|
185
|
+
throw new Error('模版已禁用,无法发送消息');
|
|
186
|
+
}
|
|
187
|
+
await sendMessage({
|
|
188
|
+
type: tpl.type,
|
|
189
|
+
name,
|
|
190
|
+
props,
|
|
191
|
+
code: tpl.code,
|
|
192
|
+
level: tpl.level
|
|
193
|
+
});
|
|
194
|
+
return { success: true };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
125
197
|
});
|
|
126
198
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-message",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "管理消息发送,支持sms消息,email消息等.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
"build:md": "npx @kne/md-doc",
|
|
11
11
|
"start:md": "npx @kne/md-doc --watch",
|
|
12
12
|
"prettier": "prettier --config .prettierrc --write '{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}'",
|
|
13
|
-
"lint-staged": "npx lint-staged"
|
|
13
|
+
"lint-staged": "npx lint-staged",
|
|
14
|
+
"test": "mocha --exit test/**/*.test.js",
|
|
15
|
+
"test:coverage": "nyc --reporter=html --reporter=text mocha test/**/*.test.js --timeout 10000"
|
|
14
16
|
},
|
|
15
17
|
"lint-staged": {
|
|
16
18
|
"**/*.md": [
|
|
@@ -43,12 +45,18 @@
|
|
|
43
45
|
},
|
|
44
46
|
"devDependencies": {
|
|
45
47
|
"@fastify/env": "^4.4.0",
|
|
48
|
+
"@fastify/sensible": "^6.0.4",
|
|
46
49
|
"@kne/fastify-sequelize": "^2.0.1",
|
|
50
|
+
"chai": "^6.2.2",
|
|
47
51
|
"fastify": "^5.3.2",
|
|
48
52
|
"fastify-plugin": "^5.0.1",
|
|
49
53
|
"husky": "^9.0.11",
|
|
54
|
+
"mocha": "^11.7.5",
|
|
50
55
|
"nodemon": "^3.1.3",
|
|
51
|
-
"
|
|
56
|
+
"nyc": "^17.1.0",
|
|
57
|
+
"prettier": "^3.2.5",
|
|
58
|
+
"qs": "^6.15.1",
|
|
59
|
+
"sqlite3": "^6.0.1"
|
|
52
60
|
},
|
|
53
61
|
"dependencies": {
|
|
54
62
|
"fs-extra": "^11.3.0",
|