فهرست منبع

Merge pull request #4336 from weseek/imprv/gw7511-create-in-app-notification-file-under-apiv3

Imprv/gw7511 create in app notification file under apiv3
Yuki Takei 4 سال پیش
والد
کامیت
e78a8bb366
2فایلهای تغییر یافته به همراه103 افزوده شده و 0 حذف شده
  1. 100 0
      packages/app/src/server/routes/apiv3/in-app-notification.ts
  2. 3 0
      packages/app/src/server/routes/apiv3/index.js

+ 100 - 0
packages/app/src/server/routes/apiv3/in-app-notification.ts

@@ -0,0 +1,100 @@
+import { InAppNotification } from '../../models/in-app-notification';
+
+const express = require('express');
+
+const router = express.Router();
+
+
+module.exports = (crowi) => {
+  const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
+  const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
+  const csrf = require('../../middlewares/csrf')(crowi);
+
+  router.get('/list', accessTokenParser, loginRequiredStrictly, (req, res) => {
+    const user = req.user;
+
+    let limit = 10;
+    if (req.query.limit) {
+      limit = parseInt(req.query.limit, 10);
+    }
+
+    let offset = 0;
+    if (req.query.offset) {
+      offset = parseInt(req.query.offset, 10);
+    }
+
+    const requestLimit = limit + 1;
+
+
+    /**
+     * TODO: GW-7482
+     *   -  Replace then/catch to async/awai
+     *   -  Use mongoose-paginate-v2 for paging
+     */
+    InAppNotification.findLatestInAppNotificationsByUser(user._id, requestLimit, offset)
+      .then((notifications) => {
+        let hasPrev = false;
+        if (offset > 0) {
+          hasPrev = true;
+        }
+
+        let hasNext = false;
+        if (notifications.length > limit) {
+          hasNext = true;
+        }
+
+        const result = {
+          notifications: notifications.slice(0, limit),
+          hasPrev,
+          hasNext,
+        };
+
+        return res.apiv3(result);
+      })
+      .catch((err) => {
+        return res.apiv3Err(err);
+      });
+  });
+
+  router.get('/status', accessTokenParser, loginRequiredStrictly, async(req, res) => {
+    const user = req.user;
+
+    try {
+      const count = await InAppNotification.getUnreadCountByUser(user._id);
+      const result = { count };
+      return res.apiv3(result);
+    }
+    catch (err) {
+      return res.apiv3Err(err);
+    }
+  });
+
+  router.post('/read', accessTokenParser, loginRequiredStrictly, csrf, (req, res) => {
+    const user = req.user;
+
+    try {
+      const notification = InAppNotification.read(user);
+      const result = { notification };
+      return res.apiv3(result);
+    }
+    catch (err) {
+      return res.apiv3Err(err);
+    }
+  });
+
+  router.post('/open', accessTokenParser, loginRequiredStrictly, csrf, async(req, res) => {
+    const user = req.user;
+    const id = req.body.id;
+
+    try {
+      const notification = await InAppNotification.open(user, id);
+      const result = { notification };
+      return res.apiv3(result);
+    }
+    catch (err) {
+      return res.apiv3Err(err);
+    }
+  });
+
+  return router;
+};

+ 3 - 0
packages/app/src/server/routes/apiv3/index.js

@@ -25,6 +25,9 @@ module.exports = (crowi) => {
   router.use('/import', require('./import')(crowi));
   router.use('/search', require('./search')(crowi));
 
+
+  router.use('/in-app-notification', require('./in-app-notification')(crowi));
+
   router.use('/personal-setting', require('./personal-setting')(crowi));
 
   router.use('/user-group-relations', require('./user-group-relation')(crowi));