All files / lib/models comment.ts

86.36% Statements 19/22
100% Branches 0/0
58.33% Functions 7/12
86.36% Lines 19/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                            16x   16x                 16x             16x             16x 3x     16x       16x 9x           16x 3x 3x   3x   3x     3x       3x   3x         16x   16x    
import Crowi from 'server/crowi'
import { Types, Document, Model, Schema, model } from 'mongoose'
import Debug from 'debug'
 
export interface CommentDocument extends Document {
  _id: Types.ObjectId
  page: Types.ObjectId | any
  creator: Types.ObjectId
  revision: Types.ObjectId
  comment: string
  commentPosition: number
  createdAt: Date
}
export interface CommentModel extends Model<CommentDocument> {
  getCommentsByPageId(id: Types.ObjectId): Promise<CommentDocument[]>
  getCommentsByRevisionId(id: Types.ObjectId): Promise<CommentDocument[]>
  countCommentByPageId(page: any): Promise<number>
  removeCommentsByPageId(pageId: Types.ObjectId): Promise<void>
  findCreatorsByPage(page: any): Promise<any[]>
}
 
export default (crowi: Crowi) => {
  const debug = Debug('crowi:models:comment')
 
  const commentSchema = new Schema<CommentDocument, CommentModel>({
    page: { type: Schema.Types.ObjectId, ref: 'Page', index: true },
    creator: { type: Schema.Types.ObjectId, ref: 'User', index: true },
    revision: { type: Schema.Types.ObjectId, ref: 'Revision', index: true },
    comment: { type: String, required: true },
    commentPosition: { type: Number, default: -1 },
    createdAt: { type: Date, default: Date.now },
  })
 
  commentSchema.statics.getCommentsByPageId = function(id) {
    return Comment.find({ page: id })
      .sort({ createdAt: -1 })
      .populate('creator')
      .exec()
  }
 
  commentSchema.statics.getCommentsByRevisionId = function(id) {
    return Comment.find({ revision: id })
      .sort({ createdAt: -1 })
      .populate('creator')
      .exec()
  }
 
  commentSchema.statics.countCommentByPageId = function(page) {
    return Comment.countDocuments({ page }).exec()
  }
 
  commentSchema.statics.removeCommentsByPageId = async function(pageId) {
    await Comment.deleteMany({ page: pageId }).exec()
  }
 
  commentSchema.statics.findCreatorsByPage = function(page) {
    return Comment.distinct('creator', { page }).exec()
  }
 
  /**
   * post save hook
   */
  commentSchema.post('save', function(savedComment: CommentDocument) {
    var Page = crowi.model('Page')
    var Activity = crowi.model('Activity')
 
    Comment.countCommentByPageId(savedComment.page)
      .then(function(count) {
        return Page.updateCommentCount(savedComment.page, count)
      })
      .then(function(page) {
        debug('CommentCount Updated', page)
      })
      .catch(function() {})
 
    Activity.createByPageComment(savedComment)
      .then(function(activityLog) {
        debug('Activity created', activityLog)
      })
      .catch(function(err) {})
  })
 
  const Comment = model<CommentDocument, CommentModel>('Comment', commentSchema)
 
  return Comment
}