All files / models shareAccess.js

58.82% Statements 10/17
0% Branches 0/7
33.33% Functions 1/3
58.82% Lines 10/17

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 4715x   15x 15x 15x   15x           15x 15x   15x                                             15x           15x    
module.exports = function(crowi) {
  // const debug = require('debug')('crowi:models:shareAccess')
  const mongoose = require('mongoose')
  const mongoosePaginate = require('mongoose-paginate')
  const ObjectId = mongoose.Schema.Types.ObjectId
 
  const shareAccessSchema = new mongoose.Schema({
    share: { type: ObjectId, ref: 'Share', index: true },
    tracking: { type: ObjectId, ref: 'Tracking', index: true },
    createdAt: { type: Date, default: Date.now },
    lastAccessedAt: { type: Date, default: Date.now },
  })
  shareAccessSchema.index({ share: 1, tracking: 1 }, { unique: true })
  shareAccessSchema.plugin(mongoosePaginate)
 
  shareAccessSchema.statics.findAccesses = async function(query, options = {}) {
    const page = options.page || 1
    const limit = options.limit || 50
    const sort = options.sort || { lastAccessedAt: -1 }
 
    return this.paginate(query, {
      page,
      limit,
      sort,
      populate: [
        {
          path: 'tracking',
          model: 'Tracking',
        },
        {
          path: 'share',
          model: 'Share',
          populate: ['page', 'creator'],
        },
      ],
    })
  }
 
  shareAccessSchema.statics.access = async function(shareId, trackingId) {
    const query = { share: shareId, tracking: trackingId }
    const update = { lastAccessedAt: Date.now() }
    return this.findOneAndUpdate(query, update, { upsert: true, new: true }).exec()
  }
 
  return mongoose.model('ShareAccess', shareAccessSchema)
}