in-app-notification.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { getOrCreateModel } from '@growi/core';
  2. import {
  3. Types, Document, Schema, Model,
  4. } from 'mongoose';
  5. import mongoosePaginate from 'mongoose-paginate-v2';
  6. import { AllSupportedTargetModelType, AllSupportedActionType } from '~/interfaces/activity';
  7. import { InAppNotificationStatuses } from '~/interfaces/in-app-notification';
  8. import { ActivityDocument } from './activity';
  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. required: true,
  36. },
  37. targetModel: {
  38. type: String,
  39. required: true,
  40. enum: AllSupportedTargetModelType,
  41. },
  42. target: {
  43. type: Schema.Types.ObjectId,
  44. refPath: 'targetModel',
  45. required: true,
  46. },
  47. action: {
  48. type: String,
  49. required: true,
  50. enum: AllSupportedActionType,
  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. required: true,
  64. },
  65. snapshot: {
  66. type: String,
  67. required: true,
  68. },
  69. }, {
  70. timestamps: { createdAt: true, updatedAt: false },
  71. });
  72. inAppNotificationSchema.plugin(mongoosePaginate);
  73. const transform = (doc, ret) => {
  74. delete ret.activities;
  75. };
  76. inAppNotificationSchema.set('toObject', { virtuals: true, transform });
  77. inAppNotificationSchema.set('toJSON', { virtuals: true, transform });
  78. inAppNotificationSchema.index({
  79. user: 1, target: 1, action: 1, createdAt: 1,
  80. });
  81. inAppNotificationSchema.statics.STATUS_UNOPENED = function() {
  82. return STATUS_UNOPENED;
  83. };
  84. inAppNotificationSchema.statics.STATUS_UNREAD = function() {
  85. return STATUS_UNREAD;
  86. };
  87. inAppNotificationSchema.statics.STATUS_OPENED = function() {
  88. return STATUS_OPENED;
  89. };
  90. const InAppNotification = getOrCreateModel<InAppNotificationDocument, InAppNotificationModel>('InAppNotification', inAppNotificationSchema);
  91. export { InAppNotification };