|
|
@@ -1,7 +1,9 @@
|
|
|
import {
|
|
|
- Types, Document, Model, Schema /* , Query */,
|
|
|
+ Types, Document, PaginateModel, Schema, /* , Query */
|
|
|
} from 'mongoose';
|
|
|
+import mongoosePaginate from 'mongoose-paginate-v2';
|
|
|
import ActivityDefine from '../util/activityDefine';
|
|
|
+import { ActivityDocument } from './activity';
|
|
|
import { getOrCreateModel } from '../util/mongoose-utils';
|
|
|
import loggerFactory from '../../utils/logger';
|
|
|
|
|
|
@@ -18,13 +20,14 @@ export interface InAppNotificationDocument extends Document {
|
|
|
targetModel: string
|
|
|
target: Types.ObjectId
|
|
|
action: string
|
|
|
- activities: Types.ObjectId[]
|
|
|
+ activities: ActivityDocument[]
|
|
|
status: string
|
|
|
createdAt: Date
|
|
|
}
|
|
|
|
|
|
-export interface InAppNotificationModel extends Model<InAppNotificationDocument> {
|
|
|
- findLatestInAppNotificationsByUser(user: Types.ObjectId, skip: number, offset: number): Promise<InAppNotificationDocument[]>
|
|
|
+
|
|
|
+export interface InAppNotificationModel extends PaginateModel<InAppNotificationDocument> {
|
|
|
+ findLatestInAppNotificationsByUser(user: Types.ObjectId, skip: number, offset: number)
|
|
|
getUnreadCountByUser(user: Types.ObjectId): Promise<number | undefined>
|
|
|
open(user, id: Types.ObjectId): Promise<InAppNotificationDocument | null>
|
|
|
read(user) /* : Promise<Query<any>> */
|
|
|
@@ -74,6 +77,7 @@ const inAppNotificationSchema = new Schema<InAppNotificationDocument, InAppNotif
|
|
|
default: Date.now,
|
|
|
},
|
|
|
});
|
|
|
+inAppNotificationSchema.plugin(mongoosePaginate);
|
|
|
|
|
|
const transform = (doc, ret) => {
|
|
|
// delete ret.activities
|
|
|
@@ -84,20 +88,6 @@ inAppNotificationSchema.index({
|
|
|
user: 1, target: 1, action: 1, createdAt: 1,
|
|
|
});
|
|
|
|
|
|
-inAppNotificationSchema.statics.findLatestInAppNotificationsByUser = async function(user, limitNum, offset) {
|
|
|
- const limit = limitNum || 10;
|
|
|
-
|
|
|
- // TODO: improve populate refer to GROWI way by #78756
|
|
|
- const notificatins = await InAppNotification.find({ user });
|
|
|
- // .sort({ createdAt: -1 })
|
|
|
- // .skip(offset)
|
|
|
- // .limit(limit)
|
|
|
- // .populate(['user', 'target'])
|
|
|
- // .populate({ path: 'activities', populate: { path: 'user' } })
|
|
|
- // .exec();
|
|
|
- return notificatins;
|
|
|
-};
|
|
|
-
|
|
|
inAppNotificationSchema.statics.STATUS_UNOPENED = function() {
|
|
|
return STATUS_UNOPENED;
|
|
|
};
|