Преглед изворни кода

Merge pull request #4729 from weseek/imprv/81907-add-endpoint-for-mark-as-read

Imprv/81907 update notification statuses by clicking on the "mark as read" button
Yuki Takei пре 4 година
родитељ
комит
097c735233

+ 11 - 3
packages/app/src/components/InAppNotification/InAppNotificationPage.tsx

@@ -9,6 +9,7 @@ import { useSWRxInAppNotifications } from '../../stores/in-app-notification';
 import PaginationWrapper from '../PaginationWrapper';
 import CustomNavAndContents from '../CustomNavigation/CustomNavAndContents';
 import { InAppNotificationStatuses } from '~/interfaces/in-app-notification';
+import { apiv3Put } from '~/client/util/apiv3-client';
 
 
 type Props = {
@@ -63,10 +64,18 @@ const InAppNotificationPageBody: FC<Props> = (props) => {
     );
   };
 
+
   // commonize notification lists by 81953
   const UnopenedInAppNotificationList = () => {
     const offsetOfUnopenedNotificationCat = (activePageOfUnopenedNotificationCat - 1) * limit;
-    const { data: unopendNotificationData } = useSWRxInAppNotifications(limit, offsetOfUnopenedNotificationCat, InAppNotificationStatuses.STATUS_UNOPENED);
+    const {
+      data: unopendNotificationData, mutate,
+    } = useSWRxInAppNotifications(limit, offsetOfUnopenedNotificationCat, InAppNotificationStatuses.STATUS_UNOPENED);
+
+    const updateUnopendNotificationStatusesToOpened = async() => {
+      await apiv3Put('/in-app-notification/all-statuses-open');
+      mutate();
+    };
 
     if (unopendNotificationData == null) {
       return (
@@ -84,8 +93,7 @@ const InAppNotificationPageBody: FC<Props> = (props) => {
           <button
             type="button"
             className="btn btn-outline-primary"
-            // TODO: set "UNOPENED" notification status "OPEND" by 81951
-            // onClick={}
+            onClick={updateUnopendNotificationStatusesToOpened}
           >
             {t('in_app_notification.mark_all_as_read')}
           </button>

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

@@ -103,5 +103,17 @@ module.exports = (crowi) => {
     }
   });
 
+  router.put('/all-statuses-open', accessTokenParser, loginRequiredStrictly, csrf, async(req, res) => {
+    const user = req.user;
+
+    try {
+      await inAppNotificationService.updateAllNotificationsAsOpened(user);
+      return res.apiv3();
+    }
+    catch (err) {
+      return res.apiv3Err(err);
+    }
+  });
+
   return router;
 };

+ 10 - 1
packages/app/src/server/service/in-app-notification.ts

@@ -1,6 +1,6 @@
 import { Types } from 'mongoose';
 import { subDays } from 'date-fns';
-import { InAppNotificationStatuses, PaginateResult } from '~/interfaces/in-app-notification';
+import { InAppNotificationStatuses, PaginateResult, IInAppNotification } from '~/interfaces/in-app-notification';
 import Crowi from '../crowi';
 import {
   InAppNotification,
@@ -12,6 +12,7 @@ import InAppNotificationSettings from '~/server/models/in-app-notification-setti
 import Subscription, { STATUS_SUBSCRIBE } from '~/server/models/subscription';
 
 import { IUser } from '~/interfaces/user';
+
 import { HasObjectId } from '~/interfaces/has-object-id';
 import loggerFactory from '~/utils/logger';
 import { RoomPrefix, getRoomNameWithId } from '../util/socket-io-helpers';
@@ -138,6 +139,14 @@ export default class InAppNotificationService {
     return;
   }
 
+  updateAllNotificationsAsOpened = async function(user: IUser & HasObjectId): Promise<void> {
+    const filter = { user: user._id, status: STATUS_UNOPENED };
+    const options = { status: STATUS_OPENED };
+
+    await InAppNotification.updateMany(filter, options);
+    return;
+  }
+
   getUnreadCountByUser = async function(user: Types.ObjectId): Promise<number| undefined> {
     const query = { user, status: STATUS_UNREAD };