NotificationsController.js 3.2 KB

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