Skip to content

Commit caa2c16

Browse files
committed
add email notify of api created or deleted #21
1 parent c7abe07 commit caa2c16

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

server/app/controller/api.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ class ApiController extends AbstractController {
110110
const users = await this.service.user.getByIds(follower)
111111
this.service.email.notifyApiChange(api, users)
112112
}
113+
async notifyApi (act, group, api) {
114+
const selfIdx = group.follower.findIndex(f => f.toString() === this.ctx.authUser._id)
115+
// 如果修改者也在关注列表中,不推送自己
116+
if (selfIdx >= 0) {
117+
group.follower.splice(selfIdx, 1)
118+
}
119+
const users = await this.service.user.getByIds(group.follower)
120+
this.service.email[`notifyApi${act}`](group, api, users)
121+
}
113122
async getApi () {
114123
const { groupId, apiId } = this.ctx.params
115124

@@ -171,7 +180,7 @@ class ApiController extends AbstractController {
171180
}))
172181

173182
this.service.group.updateTime(groupId)
174-
183+
this.notifyApi('Create', group, resources)
175184
this.ctx.body = { resources }
176185
this.ctx.status = 200
177186
}
@@ -196,7 +205,8 @@ class ApiController extends AbstractController {
196205
msg: '无权删除'
197206
})
198207
}
199-
await this.ctx.model.Group.update({ _id: groupId }, { modifiedTime: Date.now() }, { new: true }).exec()
208+
const group = await this.service.group.updateTime(groupId)
209+
this.notifyApi('Delete', group, rs)
200210
this.ctx.logger.info('deleteApi')
201211
this.ctx.status = 204
202212
}

server/app/service/email.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,34 @@ class Email extends Service {
3535
`
3636
return this.sent(user.email, 'Api Mocker 找回密码', html)
3737
}
38+
getApiDocUrl (api) {
39+
return `${this.config.clientRoot}/#/doc/${api.group}/${api._id}`
40+
}
41+
notifyApiCreate (group, api, users) {
42+
const html = `
43+
<strong>API:${api.name}</strong>
44+
<p>分组:${group.name} </p>
45+
<p>创建者:${this.ctx.authUser.name}</p>
46+
<p>链接地址:${this.getApiDocUrl(api)}</p>
47+
`
48+
users.forEach(user => this.sent(user.email, 'Api Mocker 接口新增提醒', html))
49+
}
50+
notifyApiDelete (group, api, users) {
51+
const html = `
52+
<strong>API:${api.name}</strong>
53+
<p>分组:${group.name} </p>
54+
<p>删除者:${this.ctx.authUser.name}</p>
55+
<p>链接地址:${this.getApiDocUrl(api)}</p>
56+
`
57+
users.forEach(user => this.sent(user.email, 'Api Mocker 接口删除提醒', html))
58+
}
3859
notifyApiChange (api, users) {
3960
const html = `
4061
<strong>API:${api.name}</strong>
4162
<p>修改者:${this.ctx.authUser.name}</p>
42-
<p>链接地址:${this.config.clientRoot}/#/doc/${api.group}/${api._id}</p>
63+
<p>链接地址:${this.getApiDocUrl(api)}</p>
4364
`
44-
users.map(user => {
45-
this.sent(user.email, 'Api Mocker 接口变动提醒', html)
46-
})
65+
users.forEach(user => this.sent(user.email, 'Api Mocker 接口变动提醒', html))
4766
}
4867
}
4968

0 commit comments

Comments
 (0)