NotificationsController.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import NotificationsHandler from './NotificationsHandler.js'
  2. import SessionManager from '../Authentication/SessionManager.js'
  3. import _ from 'lodash'
  4. export default {
  5. getAllUnreadNotifications(req, res, next) {
  6. const userId = SessionManager.getLoggedInUserId(req.session)
  7. NotificationsHandler.getUserNotifications(
  8. userId,
  9. function (err, unreadNotifications) {
  10. if (err) {
  11. return next(err)
  12. }
  13. unreadNotifications = _.map(
  14. unreadNotifications,
  15. function (notification) {
  16. notification.html = req.i18n.translate(
  17. notification.templateKey,
  18. notification.messageOpts
  19. )
  20. return notification
  21. }
  22. )
  23. res.json(unreadNotifications)
  24. }
  25. )
  26. },
  27. markNotificationAsRead(req, res) {
  28. const userId = SessionManager.getLoggedInUserId(req.session)
  29. const { notificationId } = req.params
  30. NotificationsHandler.markAsRead(userId, notificationId, () =>
  31. res.sendStatus(200)
  32. )
  33. },
  34. getNotification(req, res, next) {
  35. const userId = SessionManager.getLoggedInUserId(req.session)
  36. const { notificationId } = req.params
  37. NotificationsHandler.getUserNotifications(
  38. userId,
  39. function (err, unreadNotifications) {
  40. if (err) {
  41. return next(err)
  42. }
  43. const notification = unreadNotifications.find(
  44. n => n._id === notificationId
  45. )
  46. if (!notification) {
  47. return res.status(404).end()
  48. }
  49. res.json(notification)
  50. }
  51. )
  52. },
  53. }