NotificationsController.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import logger from '@overleaf/logger'
  2. import metrics from '@overleaf/metrics'
  3. import Notifications from './Notifications.js'
  4. import { expressify } from '@overleaf/promise-utils'
  5. async function getUserNotifications(req, res, next) {
  6. logger.debug(
  7. { userId: req.params.user_id },
  8. 'getting user unread notifications'
  9. )
  10. metrics.inc('getUserNotifications')
  11. const notifications = await Notifications.getUserNotifications(
  12. req.params.user_id
  13. )
  14. res.json(notifications)
  15. }
  16. async function addNotification(req, res) {
  17. logger.debug(
  18. { userId: req.params.user_id, notification: req.body },
  19. 'adding notification'
  20. )
  21. metrics.inc('addNotification')
  22. try {
  23. await Notifications.addNotification(req.params.user_id, req.body)
  24. res.sendStatus(200)
  25. } catch (err) {
  26. res.sendStatus(500)
  27. }
  28. }
  29. async function removeNotificationId(req, res) {
  30. logger.debug(
  31. {
  32. userId: req.params.user_id,
  33. notificationId: req.params.notification_id,
  34. },
  35. 'mark id notification as read'
  36. )
  37. metrics.inc('removeNotificationId')
  38. await Notifications.removeNotificationId(
  39. req.params.user_id,
  40. req.params.notification_id
  41. )
  42. res.sendStatus(200)
  43. }
  44. async function removeNotificationKey(req, res) {
  45. logger.debug(
  46. { userId: req.params.user_id, notificationKey: req.body.key },
  47. 'mark key notification as read'
  48. )
  49. metrics.inc('removeNotificationKey')
  50. await Notifications.removeNotificationKey(req.params.user_id, req.body.key)
  51. res.sendStatus(200)
  52. }
  53. async function removeNotificationByKeyOnly(req, res) {
  54. const notificationKey = req.params.key
  55. logger.debug({ notificationKey }, 'mark notification as read by key only')
  56. metrics.inc('removeNotificationKey')
  57. await Notifications.removeNotificationByKeyOnly(notificationKey)
  58. res.sendStatus(200)
  59. }
  60. async function countNotificationsByKeyOnly(req, res) {
  61. const notificationKey = req.params.key
  62. try {
  63. const count =
  64. await Notifications.countNotificationsByKeyOnly(notificationKey)
  65. res.json({ count })
  66. } catch (err) {
  67. logger.err({ err, notificationKey }, 'cannot count by key')
  68. res.sendStatus(500)
  69. }
  70. }
  71. async function deleteUnreadNotificationsByKeyOnlyBulk(req, res) {
  72. const notificationKey = req.params.key
  73. try {
  74. const count =
  75. await Notifications.deleteUnreadNotificationsByKeyOnlyBulk(
  76. notificationKey
  77. )
  78. res.json({ count })
  79. } catch (err) {
  80. logger.err({ err, notificationKey }, 'cannot bulk remove by key')
  81. res.sendStatus(500)
  82. }
  83. }
  84. export default {
  85. getUserNotifications: expressify(getUserNotifications),
  86. addNotification: expressify(addNotification),
  87. deleteUnreadNotificationsByKeyOnlyBulk: expressify(
  88. deleteUnreadNotificationsByKeyOnlyBulk
  89. ),
  90. removeNotificationByKeyOnly: expressify(removeNotificationByKeyOnly),
  91. removeNotificationId: expressify(removeNotificationId),
  92. removeNotificationKey: expressify(removeNotificationKey),
  93. countNotificationsByKeyOnly: expressify(countNotificationsByKeyOnly),
  94. }