NotificationsController.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import { parseReq, z, zz } from '@overleaf/validation-tools'
  6. import type { Request, Response } from 'express'
  7. const getUserNotificationsSchema = z.object({
  8. params: z.object({
  9. user_id: zz.objectId(),
  10. }),
  11. })
  12. async function getUserNotifications(req: Request, res: Response) {
  13. const { params } = parseReq(req, getUserNotificationsSchema)
  14. logger.debug({ userId: params.user_id }, 'getting user unread notifications')
  15. metrics.inc('getUserNotifications')
  16. const notifications = await Notifications.getUserNotifications(params.user_id)
  17. res.json(notifications)
  18. }
  19. const addNotificationSchema = z.object({
  20. params: z.object({
  21. user_id: zz.objectId(),
  22. }),
  23. body: z.looseObject({}),
  24. })
  25. async function addNotification(req: Request, res: Response) {
  26. const { params, body } = parseReq(req, addNotificationSchema)
  27. logger.debug(
  28. { userId: params.user_id, notification: body },
  29. 'adding notification'
  30. )
  31. metrics.inc('addNotification')
  32. try {
  33. await Notifications.addNotification(params.user_id, body)
  34. res.sendStatus(200)
  35. } catch (err) {
  36. res.sendStatus(500)
  37. }
  38. }
  39. const removeNotificationIdSchema = z.object({
  40. params: z.object({
  41. user_id: zz.objectId(),
  42. notification_id: zz.objectId(),
  43. }),
  44. })
  45. async function removeNotificationId(req: Request, res: Response) {
  46. const { params } = parseReq(req, removeNotificationIdSchema)
  47. logger.debug(
  48. {
  49. userId: req.params.user_id,
  50. notificationId: req.params.notification_id,
  51. },
  52. 'mark id notification as read'
  53. )
  54. metrics.inc('removeNotificationId')
  55. await Notifications.removeNotificationId(
  56. params.user_id,
  57. params.notification_id
  58. )
  59. res.sendStatus(200)
  60. }
  61. const removeNotificationKeySchema = z.object({
  62. params: z.object({
  63. user_id: zz.objectId(),
  64. }),
  65. body: z.object({
  66. key: z.string(),
  67. }),
  68. })
  69. async function removeNotificationKey(req: Request, res: Response) {
  70. const { params, body } = parseReq(req, removeNotificationKeySchema)
  71. logger.debug(
  72. { userId: req.params.user_id, notificationKey: body.key },
  73. 'mark key notification as read'
  74. )
  75. metrics.inc('removeNotificationKey')
  76. await Notifications.removeNotificationKey(params.user_id, body.key)
  77. res.sendStatus(200)
  78. }
  79. const removeNotificationByKeyOnlySchema = z.object({
  80. params: z.object({
  81. key: z.string(),
  82. }),
  83. })
  84. async function removeNotificationByKeyOnly(req: Request, res: Response) {
  85. const { params } = parseReq(req, removeNotificationByKeyOnlySchema)
  86. const notificationKey = params.key
  87. logger.debug({ notificationKey }, 'mark notification as read by key only')
  88. metrics.inc('removeNotificationKey')
  89. await Notifications.removeNotificationByKeyOnly(notificationKey)
  90. res.sendStatus(200)
  91. }
  92. const countNotificationsByKeyOnlySchema = z.object({
  93. params: z.object({
  94. key: z.string(),
  95. }),
  96. })
  97. async function countNotificationsByKeyOnly(req: Request, res: Response) {
  98. const { params } = parseReq(req, countNotificationsByKeyOnlySchema)
  99. const notificationKey = params.key
  100. try {
  101. const count =
  102. await Notifications.countNotificationsByKeyOnly(notificationKey)
  103. res.json({ count })
  104. } catch (err) {
  105. logger.err({ err, notificationKey }, 'cannot count by key')
  106. res.sendStatus(500)
  107. }
  108. }
  109. const deleteUnreadNotificationsByKeyOnlyBulkSchema = z.object({
  110. params: z.object({
  111. key: z.string(),
  112. }),
  113. })
  114. async function deleteUnreadNotificationsByKeyOnlyBulk(
  115. req: Request,
  116. res: Response
  117. ) {
  118. const { params } = parseReq(req, deleteUnreadNotificationsByKeyOnlyBulkSchema)
  119. const notificationKey = params.key
  120. try {
  121. const count =
  122. await Notifications.deleteUnreadNotificationsByKeyOnlyBulk(
  123. notificationKey
  124. )
  125. res.json({ count })
  126. } catch (err) {
  127. logger.err({ err, notificationKey }, 'cannot bulk remove by key')
  128. res.sendStatus(500)
  129. }
  130. }
  131. export default {
  132. getUserNotifications: expressify(getUserNotifications),
  133. addNotification: expressify(addNotification),
  134. deleteUnreadNotificationsByKeyOnlyBulk: expressify(
  135. deleteUnreadNotificationsByKeyOnlyBulk
  136. ),
  137. removeNotificationByKeyOnly: expressify(removeNotificationByKeyOnly),
  138. removeNotificationId: expressify(removeNotificationId),
  139. removeNotificationKey: expressify(removeNotificationKey),
  140. countNotificationsByKeyOnly: expressify(countNotificationsByKeyOnly),
  141. }