2015-11-02 20:07:23 -05:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
log = require 'winston'
|
|
|
|
config = require '../../server_config'
|
|
|
|
plugins = require '../plugins/plugins'
|
|
|
|
User = require '../users/User'
|
|
|
|
jsonSchema = require '../../app/schemas/models/classroom.schema'
|
2016-01-17 19:45:28 -05:00
|
|
|
utils = require '../lib/utils'
|
2015-11-02 20:07:23 -05:00
|
|
|
|
|
|
|
ClassroomSchema = new mongoose.Schema {}, {strict: false, minimize: false, read:config.mongo.readpref}
|
|
|
|
|
2015-12-03 14:15:34 -05:00
|
|
|
ClassroomSchema.index({ownerID: 1}, {name: 'ownerID index'})
|
|
|
|
ClassroomSchema.index({members: 1}, {name: 'members index'})
|
|
|
|
ClassroomSchema.index({code: 1}, {name: 'code index', unique: true})
|
|
|
|
|
2015-11-02 20:07:23 -05:00
|
|
|
ClassroomSchema.statics.privateProperties = []
|
|
|
|
ClassroomSchema.statics.editableProperties = [
|
|
|
|
'description'
|
|
|
|
'name'
|
2015-11-17 19:09:29 -05:00
|
|
|
'aceConfig'
|
2015-11-02 20:07:23 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
ClassroomSchema.statics.generateNewCode = (done) ->
|
|
|
|
tryCode = ->
|
2016-01-17 19:45:28 -05:00
|
|
|
# Use 4 code words once we get past 10M classrooms
|
|
|
|
codeCamel = utils.getCodeCamel(3)
|
2015-11-22 10:47:28 -05:00
|
|
|
code = codeCamel.toLowerCase()
|
2015-11-02 20:07:23 -05:00
|
|
|
Classroom.findOne code: code, (err, classroom) ->
|
|
|
|
return done() if err
|
2015-11-22 10:47:28 -05:00
|
|
|
return done(code, codeCamel) unless classroom
|
2015-11-02 20:07:23 -05:00
|
|
|
tryCode()
|
|
|
|
tryCode()
|
|
|
|
|
|
|
|
ClassroomSchema.pre('save', (next) ->
|
|
|
|
return next() if @get('code')
|
2015-11-22 10:47:28 -05:00
|
|
|
Classroom.generateNewCode (code, codeCamel) =>
|
2015-11-02 20:07:23 -05:00
|
|
|
@set 'code', code
|
2015-11-22 10:47:28 -05:00
|
|
|
@set 'codeCamel', codeCamel
|
2015-11-02 20:07:23 -05:00
|
|
|
next()
|
|
|
|
)
|
|
|
|
|
2015-11-09 16:29:49 -05:00
|
|
|
ClassroomSchema.methods.isOwner = (userID) ->
|
|
|
|
return userID.equals(@get('ownerID'))
|
2015-11-22 10:47:28 -05:00
|
|
|
|
2015-11-09 16:29:49 -05:00
|
|
|
ClassroomSchema.methods.isMember = (userID) ->
|
|
|
|
return _.any @get('members') or [], (memberID) -> userID.equals(memberID)
|
|
|
|
|
2015-11-02 20:07:23 -05:00
|
|
|
ClassroomSchema.statics.jsonSchema = jsonSchema
|
|
|
|
|
|
|
|
module.exports = Classroom = mongoose.model 'classroom', ClassroomSchema, 'classrooms'
|