Merge pull request #5306 from overleaf/jpa-clear-institution-notifications

[web] add a new script for clearing institution notifications

GitOrigin-RevId: d102b484c4fb9816832b48c2dfa66953bc996667
This commit is contained in:
Jakob Ackermann
2021-10-21 08:03:25 +00:00
committed by Copybot
parent fae4b96762
commit dc80bce41c
6 changed files with 157 additions and 1 deletions
+2
View File
@@ -35,6 +35,8 @@ app.delete(
)
app.delete('/user/:user_id', controller.removeNotificationKey)
app.delete('/key/:key', controller.removeNotificationByKeyOnly)
app.get('/key/:key/count', controller.countNotificationsByKeyOnly)
app.delete('/key/:key/bulk', controller.deleteUnreadNotificationsByKeyOnlyBulk)
app.get('/status', (req, res) => res.send('notifications sharelatex up'))
@@ -112,6 +112,22 @@ module.exports = Notifications = {
db.notifications.updateOne(searchOps, updateOperation, callback)
},
countNotificationsByKeyOnly(notificationKey, callback) {
const searchOps = { key: notificationKey, templateKey: { $exists: true } }
db.notifications.count(searchOps, callback)
},
deleteUnreadNotificationsByKeyOnlyBulk(notificationKey, callback) {
if (typeof notificationKey !== 'string') {
throw new Error('refusing to bulk delete arbitrary notifications')
}
const searchOps = { key: notificationKey, templateKey: { $exists: true } }
db.notifications.deleteMany(searchOps, (err, result) => {
if (err) return callback(err)
callback(null, result.deletedCount)
})
},
// hard delete of doc, rather than removing the templateKey
deleteNotificationByKeyOnly(notification_key, callback) {
const searchOps = { key: notification_key }
@@ -84,4 +84,29 @@ module.exports = {
(err, notifications) => res.sendStatus(200)
)
},
countNotificationsByKeyOnly(req, res) {
const notificationKey = req.params.key
Notifications.countNotificationsByKeyOnly(notificationKey, (err, count) => {
if (err) {
logger.err({ err, notificationKey }, 'cannot count by key')
return res.sendStatus(500)
}
res.json({ count })
})
},
deleteUnreadNotificationsByKeyOnlyBulk(req, res) {
const notificationKey = req.params.key
Notifications.deleteUnreadNotificationsByKeyOnlyBulk(
notificationKey,
(err, count) => {
if (err) {
logger.err({ err, notificationKey }, 'cannot bulk remove by key')
return res.sendStatus(500)
}
res.json({ count })
}
)
},
}