Просмотр исходного кода

move methods to service layer from model

kaori 4 лет назад
Родитель
Сommit
67bf5a1653

+ 0 - 29
packages/app/src/server/models/in-app-notification.ts

@@ -26,10 +26,6 @@ export interface InAppNotificationDocument extends Document {
 export interface InAppNotificationModel extends Model<InAppNotificationDocument> {
   findLatestInAppNotificationsByUser(user: Types.ObjectId, skip: number, offset: number): Promise<InAppNotificationDocument[]>
 
-  // commented out type 'Query' temporary to avoid ts error
-  removeEmpty()/* : Query<any> */
-  read(user) /* : Promise<Query<any>> */
-
   open(user, id: Types.ObjectId): Promise<InAppNotificationDocument | null>
   getUnreadCountByUser(user: Types.ObjectId): Promise<number | undefined>
 
@@ -101,31 +97,6 @@ inAppNotificationSchema.statics.findLatestInAppNotificationsByUser = function(us
     .exec();
 };
 
-inAppNotificationSchema.statics.removeEmpty = function() {
-  return InAppNotification.deleteMany({ activities: { $size: 0 } });
-};
-
-inAppNotificationSchema.statics.read = async function(user) {
-  const query = { user, status: STATUS_UNREAD };
-  const parameters = { status: STATUS_UNOPENED };
-
-  return InAppNotification.updateMany(query, parameters);
-};
-
-inAppNotificationSchema.statics.getUnreadCountByUser = async function(user) {
-  const query = { user, status: STATUS_UNREAD };
-
-  try {
-    const count = await InAppNotification.countDocuments(query);
-
-    return count;
-  }
-  catch (err) {
-    logger.error('Error on getUnreadCountByUser', err);
-    throw err;
-  }
-};
-
 inAppNotificationSchema.statics.STATUS_UNOPENED = function() {
   return STATUS_UNOPENED;
 };

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

@@ -1,7 +1,9 @@
 import { Types } from 'mongoose';
 import { subDays } from 'date-fns';
 import Crowi from '../crowi';
-import { InAppNotification, InAppNotificationDocument, STATUS_UNREAD } from '~/server/models/in-app-notification';
+import {
+  InAppNotification, InAppNotificationDocument, STATUS_UNREAD, STATUS_UNOPENED,
+} from '~/server/models/in-app-notification';
 import { ActivityDocument } from '~/server/models/activity';
 
 import loggerFactory from '~/utils/logger';
@@ -73,6 +75,29 @@ export default class InAppNotificationService {
   //   return Activity.getActionUsersFromActivities((this.activities as any) as ActivityDocument[]);
   // });
 
+  read = async function(user: Types.ObjectId): Promise<void> {
+    const query = { user, status: STATUS_UNREAD };
+    const parameters = { status: STATUS_UNOPENED };
+    await InAppNotification.updateMany(query, parameters);
+
+    return;
+  };
+
+  getUnreadCountByUser = async function(user: Types.ObjectId): Promise<number> {
+    const query = { user, status: STATUS_UNREAD };
+
+    try {
+      const count = await InAppNotification.countDocuments(query);
+
+      return count;
+    }
+    catch (err) {
+      logger.error('Error on getUnreadCountByUser', err);
+      throw err;
+    }
+  };
+
+
 }
 
 module.exports = InAppNotificationService;