in-app-notification.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {
  2. Types, Document, Schema, Model,
  3. } from 'mongoose';
  4. import mongoosePaginate from 'mongoose-paginate-v2';
  5. import { getOrCreateModel } from '@growi/core';
  6. import { ActivityDocument } from './activity';
  7. import ActivityDefine from '../util/activityDefine';
  8. import { InAppNotificationStatuses } from '~/interfaces/in-app-notification';
  9. const { STATUS_UNREAD, STATUS_UNOPENED, STATUS_OPENED } = InAppNotificationStatuses;
  10. export interface InAppNotificationDocument extends Document {
  11. _id: Types.ObjectId
  12. user: Types.ObjectId
  13. targetModel: string
  14. target: Types.ObjectId
  15. action: string
  16. activities: ActivityDocument[]
  17. status: string
  18. createdAt: Date
  19. snapshot: string
  20. }
  21. export interface InAppNotificationModel extends Model<InAppNotificationDocument> {
  22. findLatestInAppNotificationsByUser(user: Types.ObjectId, skip: number, offset: number)
  23. getUnreadCountByUser(user: Types.ObjectId): Promise<number | undefined>
  24. open(user, id: Types.ObjectId): Promise<InAppNotificationDocument | null>
  25. read(user) /* : Promise<Query<any>> */
  26. STATUS_UNREAD: string
  27. STATUS_UNOPENED: string
  28. STATUS_OPENED: string
  29. }
  30. const inAppNotificationSchema = new Schema<InAppNotificationDocument, InAppNotificationModel>({
  31. user: {
  32. type: Schema.Types.ObjectId,
  33. ref: 'User',
  34. index: true,
  35. require: true,
  36. },
  37. targetModel: {
  38. type: String,
  39. require: true,
  40. enum: ActivityDefine.getSupportTargetModelNames(),
  41. },
  42. target: {
  43. type: Schema.Types.ObjectId,
  44. refPath: 'targetModel',
  45. require: true,
  46. },
  47. action: {
  48. type: String,
  49. require: true,
  50. enum: ActivityDefine.getSupportActionNames(),
  51. },
  52. activities: [
  53. {
  54. type: Schema.Types.ObjectId,
  55. ref: 'Activity',
  56. },
  57. ],
  58. status: {
  59. type: String,
  60. default: STATUS_UNREAD,
  61. enum: InAppNotificationStatuses,
  62. index: true,
  63. require: true,
  64. },
  65. createdAt: {
  66. type: Date,
  67. default: new Date(),
  68. },
  69. snapshot: {
  70. type: String,
  71. require: true,
  72. },
  73. });
  74. inAppNotificationSchema.plugin(mongoosePaginate);
  75. const transform = (doc, ret) => {
  76. delete ret.activities;
  77. };
  78. inAppNotificationSchema.set('toObject', { virtuals: true, transform });
  79. inAppNotificationSchema.set('toJSON', { virtuals: true, transform });
  80. inAppNotificationSchema.index({
  81. user: 1, target: 1, action: 1, createdAt: 1,
  82. });
  83. inAppNotificationSchema.statics.STATUS_UNOPENED = function() {
  84. return STATUS_UNOPENED;
  85. };
  86. inAppNotificationSchema.statics.STATUS_UNREAD = function() {
  87. return STATUS_UNREAD;
  88. };
  89. inAppNotificationSchema.statics.STATUS_OPENED = function() {
  90. return STATUS_OPENED;
  91. };
  92. const InAppNotification = getOrCreateModel<InAppNotificationDocument, InAppNotificationModel>('InAppNotification', inAppNotificationSchema);
  93. export { InAppNotification };