All files / models revision.js

55.38% Statements 36/65
27.78% Branches 5/18
36.36% Functions 8/22
55.38% Lines 36/65

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 14415x   15x 15x     15x               15x                 15x                               15x                                         15x             15x                                 15x 3x   3x 3x 3x       3x         15x 6x   6x     6x   6x       6x 6x 6x 6x 6x 6x   6x     15x         15x   15x 9x   9x 9x 9x       9x         15x    
module.exports = function(crowi) {
  // var debug = require('debug')('crowi:models:revision')
  var mongoose = require('mongoose')
  var ObjectId = mongoose.Schema.Types.ObjectId
  var revisionSchema
 
  revisionSchema = new mongoose.Schema({
    path: { type: String, required: true, index: true },
    body: { type: String, required: true },
    format: { type: String, default: 'markdown' },
    author: { type: ObjectId, ref: 'User' },
    createdAt: { type: Date, default: Date.now },
  })
 
  revisionSchema.statics.findLatestRevision = function(path, cb) {
    this.find({ path: path })
      .sort({ createdAt: -1 })
      .limit(1)
      .exec(function(err, data) {
        cb(err, data.shift())
      })
  }
 
  revisionSchema.statics.findRevision = function(id) {
    var Revision = this
 
    return new Promise(function(resolve, reject) {
      Revision.findById(id)
        .populate('author')
        .exec(function(err, data) {
          if (err) {
            return reject(err)
          }
 
          return resolve(data)
        })
    })
  }
 
  revisionSchema.statics.findRevisions = function(ids) {
    var Revision = this
 
    if (!Array.isArray(ids)) {
      return Promise.reject(new Error('The argument was not Array.'))
    }
 
    return new Promise(function(resolve, reject) {
      Revision.find({ _id: { $in: ids } })
        .sort({ createdAt: -1 })
        .populate('author')
        .exec(function(err, revisions) {
          if (err) {
            return reject(err)
          }
 
          return resolve(revisions)
        })
    })
  }
 
  revisionSchema.statics.findRevisionIdList = function(path) {
    return this.find({ path: path })
      .select('_id author createdAt')
      .sort({ createdAt: -1 })
      .exec()
  }
 
  revisionSchema.statics.findRevisionList = function(path, options) {
    var Revision = this
 
    return new Promise(function(resolve, reject) {
      Revision.find({ path: path })
        .sort({ createdAt: -1 })
        .populate('author')
        .exec(function(err, data) {
          if (err) {
            return reject(err)
          }
 
          return resolve(data)
        })
    })
  }
 
  revisionSchema.statics.updateRevisionListByPath = function(path, updateData, options) {
    var Revision = this
 
    return new Promise(function(resolve, reject) {
      Revision.update({ path: path }, { $set: updateData }, { multi: true }, function(err, data) {
        Iif (err) {
          return reject(err)
        }
 
        return resolve(data)
      })
    })
  }
 
  revisionSchema.statics.prepareRevision = function(pageData, body, user, options) {
    var Revision = this
 
    Iif (!options) {
      options = {}
    }
    var format = options.format || 'markdown'
 
    Iif (!user._id) {
      throw new Error('Error: user should have _id')
    }
 
    var newRevision = new Revision()
    newRevision.path = pageData.path
    newRevision.body = body
    newRevision.format = format
    newRevision.author = user._id
    newRevision.createdAt = Date.now()
 
    return newRevision
  }
 
  revisionSchema.statics.removeRevisionsByPath = function(path) {
    const Revision = this
    return Revision.remove({ path })
  }
 
  revisionSchema.statics.updatePath = function(pathName) {}
 
  revisionSchema.statics.findAuthorsByPage = function(page) {
    var Revision = this
 
    return new Promise(function(resolve, reject) {
      Revision.distinct('author', { path: page.path }).exec(function(err, authors) {
        Iif (err) {
          reject(err)
        }
 
        resolve(authors)
      })
    })
  }
 
  return mongoose.model('Revision', revisionSchema)
}