All files / lib/models share.ts

55.93% Statements 33/59
8.7% Branches 2/23
46.15% Functions 6/13
55.93% Lines 33/59

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195              16x 16x                                                                         16x                 16x         16x 16x 16x   16x       16x       16x               16x 4x 4x     16x                                                               16x                                                       16x         16x           16x 4x       4x 1x     3x                   16x 2x 2x     16x 1x     16x 1x     16x 16x   16x   16x    
import Crowi from 'server/crowi'
import { Types, Document, Model, Schema, model } from 'mongoose'
// import Debug from 'debug'
import uuidv4 from 'uuid/v4'
import mongoosePaginate from 'mongoose-paginate'
import { UserDocument } from './user'
 
const STATUS_ACTIVE = 'active'
const STATUS_INACTIVE = 'inactive'
 
export interface ShareDocument extends Document {
  _id: Types.ObjectId
  uuid: string
  page: Types.ObjectId
  status: string
  creator: Types.ObjectId
  secretKeyword: string
  createdAt: Date
  updatedAt: Date
 
  isActive(): boolean
  isInactive(): boolean
  isCreator(userData): boolean
}
 
export interface ShareModel extends Model<ShareDocument> {
  paginate: any
 
  isExists(query): Promise<any>
  findShares(query, options: { page?: number; limit?: number; sort?: object; populateAccesses?: boolean }): Promise<any>
  findShare(query, options?: { populateAccesses?: boolean }): Promise<ShareDocument>
  findShareByUuid(uuid, query?, options?): Promise<ShareDocument>
  findShareByPageId(pageId, query?, options?): Promise<ShareDocument>
  createShare(pageId: Types.ObjectId, user: Types.ObjectId): Promise<ShareDocument>
  deleteShare(query: object): Promise<ShareDocument | null>
  deleteById(id): Promise<ShareDocument | null>
  deleteByPageId(pageId): Promise<ShareDocument | null>
 
  STATUS_ACTIVE: string
  STATUS_INACTIVE: string
}
 
export default (crowi: Crowi) => {
  // const debug = Debug('crowi:models:share')
 
  const shareSchema = new Schema<ShareDocument, ShareModel>({
    uuid: { type: String, required: true, index: true, unique: true },
    page: { type: Schema.Types.ObjectId, ref: 'Page', required: true, index: true },
    status: { type: String, default: STATUS_ACTIVE, index: true },
    creator: { type: Schema.Types.ObjectId, ref: 'User', required: true, index: true },
    secretKeyword: String,
    createdAt: { type: Date, default: Date.now },
    updatedAt: Date,
  })
  shareSchema.virtual('accesses', {
    ref: 'ShareAccess',
    localField: '_id',
    foreignField: 'share',
  })
  shareSchema.set('toObject', { virtuals: true })
  shareSchema.set('toJSON', { virtuals: true })
  shareSchema.plugin(mongoosePaginate)
 
  shareSchema.methods.isActive = function() {
    return this.status === STATUS_ACTIVE
  }
 
  shareSchema.methods.isInactive = function() {
    return this.status === STATUS_INACTIVE
  }
 
  shareSchema.methods.isCreator = function(userData) {
    this.populate('creator')
    const creatorId = ((this.creator as any) as UserDocument)._id.toString()
    const userId = userData._id.toString()
 
    return creatorId === userId
  }
 
  shareSchema.statics.isExists = async function(query) {
    const count = await this.countDocuments(query)
    return count > 0
  }
 
  shareSchema.statics.findShares = async function(query, options = {}) {
    const page = options.page || 1
    const limit = options.limit || 50
    const sort = options.sort || { createdAt: -1 }
 
    const { populateAccesses = false } = options
    const optionalDocs = populateAccesses
      ? [
          {
            path: 'accesses',
            populate: { path: 'tracking' },
            options: { sort: { lastAccessedAt: -1 } },
          },
        ]
      : []
 
    return this.paginate(query, {
      page,
      limit,
      sort,
      populate: [
        ...optionalDocs,
        {
          path: 'page',
        },
        {
          path: 'creator',
        },
      ],
    })
  }
 
  shareSchema.statics.findShare = async function(query, options = {}) {
    const Page = crowi.model('Page')
 
    const { populateAccesses = false } = options
    const optionalDocs = populateAccesses
      ? [
          {
            path: 'accesses',
            populate: { path: 'tracking' },
          },
        ]
      : []
 
    const shareData = await Share.findOne(query)
      .findOne(query)
      .populate([...optionalDocs, { path: 'page' }, { path: 'creator' }])
      .exec()
 
    if (shareData === null) {
      const shareNotFoundError = new Error('Share Not Found')
      shareNotFoundError.name = 'Crowi:Share:NotFound'
      throw shareNotFoundError
    }
 
    shareData.page = (await Page.populatePageData(shareData.page)) as any
    return shareData
  }
 
  shareSchema.statics.findShareByUuid = async function(uuid, query, options) {
    query = Object.assign({ uuid }, query !== undefined ? query : {})
    return Share.findShare(query, options)
  }
 
  shareSchema.statics.findShareByPageId = async function(pageId, query, options) {
    query = Object.assign({ page: pageId }, query !== undefined ? query : {})
 
    return Share.findShare(query, options)
  }
 
  shareSchema.statics.createShare = async function(pageId, user) {
    const isExists = await Share.isExists({
      page: pageId,
      status: STATUS_ACTIVE,
    })
    if (isExists) {
      throw new Error('Cannot create new share.')
    }
 
    return Share.create({
      uuid: uuidv4(),
      page: pageId,
      creator: user,
      createdAt: Date.now(),
      updatedAt: Date.now(),
      status: STATUS_ACTIVE,
    })
  }
 
  shareSchema.statics.deleteShare = async function(query = {}) {
    const defaultQuery = { status: STATUS_ACTIVE }
    return Share.findOneAndUpdate({ ...query, ...defaultQuery }, { status: STATUS_INACTIVE }, { new: true }).exec()
  }
 
  shareSchema.statics.deleteById = async function(id) {
    return Share.deleteShare({ _id: id })
  }
 
  shareSchema.statics.deleteByPageId = async function(pageId) {
    return Share.deleteShare({ page: pageId })
  }
 
  shareSchema.statics.STATUS_ACTIVE = STATUS_ACTIVE
  shareSchema.statics.STATUS_INACTIVE = STATUS_INACTIVE
 
  const Share = model<ShareDocument, ShareModel>('Share', shareSchema)
 
  return Share
}