NotificationsController.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* eslint-disable
  2. camelcase,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const Notifications = require('./Notifications')
  13. const logger = require('@overleaf/logger')
  14. const metrics = require('@overleaf/metrics')
  15. module.exports = {
  16. getUserNotifications(req, res, next) {
  17. logger.log(
  18. { user_id: req.params.user_id },
  19. 'getting user unread notifications'
  20. )
  21. metrics.inc('getUserNotifications')
  22. return Notifications.getUserNotifications(
  23. req.params.user_id,
  24. (err, notifications) => {
  25. if (err) return next(err)
  26. res.json(notifications)
  27. }
  28. )
  29. },
  30. addNotification(req, res) {
  31. logger.log(
  32. { user_id: req.params.user_id, notification: req.body },
  33. 'adding notification'
  34. )
  35. metrics.inc('addNotification')
  36. return Notifications.addNotification(
  37. req.params.user_id,
  38. req.body,
  39. function (err, notifications) {
  40. if (err != null) {
  41. return res.sendStatus(500)
  42. } else {
  43. return res.sendStatus(200)
  44. }
  45. }
  46. )
  47. },
  48. removeNotificationId(req, res, next) {
  49. logger.log(
  50. {
  51. user_id: req.params.user_id,
  52. notification_id: req.params.notification_id,
  53. },
  54. 'mark id notification as read'
  55. )
  56. metrics.inc('removeNotificationId')
  57. return Notifications.removeNotificationId(
  58. req.params.user_id,
  59. req.params.notification_id,
  60. err => {
  61. if (err) return next(err)
  62. res.sendStatus(200)
  63. }
  64. )
  65. },
  66. removeNotificationKey(req, res, next) {
  67. logger.log(
  68. { user_id: req.params.user_id, notification_key: req.body.key },
  69. 'mark key notification as read'
  70. )
  71. metrics.inc('removeNotificationKey')
  72. return Notifications.removeNotificationKey(
  73. req.params.user_id,
  74. req.body.key,
  75. (err, notifications) => {
  76. if (err) return next(err)
  77. res.sendStatus(200)
  78. }
  79. )
  80. },
  81. removeNotificationByKeyOnly(req, res, next) {
  82. const notification_key = req.params.key
  83. logger.log({ notification_key }, 'mark notification as read by key only')
  84. metrics.inc('removeNotificationKey')
  85. return Notifications.removeNotificationByKeyOnly(notification_key, err => {
  86. if (err) return next(err)
  87. res.sendStatus(200)
  88. })
  89. },
  90. countNotificationsByKeyOnly(req, res) {
  91. const notificationKey = req.params.key
  92. Notifications.countNotificationsByKeyOnly(notificationKey, (err, count) => {
  93. if (err) {
  94. logger.err({ err, notificationKey }, 'cannot count by key')
  95. return res.sendStatus(500)
  96. }
  97. res.json({ count })
  98. })
  99. },
  100. deleteUnreadNotificationsByKeyOnlyBulk(req, res) {
  101. const notificationKey = req.params.key
  102. Notifications.deleteUnreadNotificationsByKeyOnlyBulk(
  103. notificationKey,
  104. (err, count) => {
  105. if (err) {
  106. logger.err({ err, notificationKey }, 'cannot bulk remove by key')
  107. return res.sendStatus(500)
  108. }
  109. res.json({ count })
  110. }
  111. )
  112. },
  113. }