-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
76 lines (63 loc) · 1.82 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { conf } = require('mono-core')
const { getFindOptions, FindStream } = require('mono-mongodb')
const notificationModule = require('./index')
const validation = require('./routes.validation')
const { sessionKey, routeName } = conf.mono.notifications
module.exports = [
{
method: 'get',
path: `/${routeName}`,
session: true,
validation,
async handler(req, res) {
const options = getFindOptions(req.query)
const userId = req.session[sessionKey]
const total = await notificationModule.count(userId)
const stream = notificationModule.list(userId, options)
const listStream = new FindStream({
total,
res,
...options
})
stream.pipe(listStream).pipe(res)
// Set notifications as read if markRead is set as true
if (req.query.markRead === 'true') {
const notifications = await notificationModule.list(userId, options).toArray()
const unreadNotifications = notifications.filter((notification) => !notification.read)
await notificationModule.read(userId, unreadNotifications.map((notification) => notification._id))
}
}
},
{
method: 'get',
path: `/${routeName}/count`,
session: true,
async handler(req, res) {
const userId = req.session[sessionKey]
const count = await notificationModule.count(userId, false)
res.json(count)
}
},
{
method: 'put',
path: `/${routeName}/read`,
session: true,
async handler(req, res) {
const userId = req.session[sessionKey]
const notificationsId = req.body
await notificationModule.read(userId, notificationsId)
res.sendStatus(200)
}
},
{
method: 'put',
path: `/${routeName}/:id/read`,
session: true,
async handler(req, res) {
const userId = req.session[sessionKey]
const notificationId = req.params.id
await notificationModule.read(userId, notificationId)
res.sendStatus(200)
}
}
]