Send none if a course variable is missing
[oweals/finalsclub.git] / models.js
index 87d5c72349d80e0d59491e5f1bd5ba636e51d0ba..15b812521b5b662a9a3dce6143b83408d7483629 100644 (file)
--- a/models.js
+++ b/models.js
@@ -36,6 +36,18 @@ var UserSchema = new Schema( {
        admin                                           : { 'type' : Boolean, 'default' : false }
 });
 
+UserSchema.virtual( 'sanitized' ).get(function() {
+  var user = {
+    email: this.email,
+    name: this.name,
+    affil: this.affil,
+    showName: this.showName,
+    admin: this.admin
+  }
+
+  return user;
+});
+
 UserSchema.virtual( 'displayName' )
        .get( function() {
                if( this.showName ) {
@@ -134,6 +146,7 @@ var SchoolSchema = new Schema( {
 
 SchoolSchema.virtual( 'sanitized' ).get(function() {
   var school = {
+    _id: this._id,
     name: this.name,
     description: this.description,
     url: this.url
@@ -169,6 +182,19 @@ var CourseSchema = new Schema( {
        users                           : Array
 });
 
+CourseSchema.virtual( 'sanitized' ).get(function() {
+  var course = {
+    _id: this._id,
+    name: this.name,
+    number: this.number || 'None',
+    description: this.description || 'None',
+    subject: this.subject || 'None',
+    department: this.department || 'None'
+  }
+
+  return course;
+});
+
 CourseSchema.virtual( 'displayName' )
        .get( function() {
                if( this.number ) {
@@ -239,6 +265,17 @@ var LectureSchema  = new Schema( {
        course                          : ObjectId
 });
 
+LectureSchema.virtual( 'sanitized' ).get(function() {
+  var lecture = {
+    _id: this._id,
+    name: this.name,
+    date: this.date,
+    live: this.live
+  }
+
+  return lecture;
+})
+
 LectureSchema.method( 'authorize', function( user, cb ) {
        Course.findById( this.course, function( err, course ) {
                if (course) {
@@ -288,6 +325,19 @@ var NoteSchema = new Schema( {
        collaborators : [String]
 });
 
+NoteSchema.virtual( 'sanitized').get(function() {
+  var note = {
+    _id: this._id,
+    name: this.name,
+    path: this.path,
+    public: this.public,
+    roID: this.roID,
+    visits: this.visits
+  }
+
+  return note;
+});
+
 NoteSchema.method( 'authorize', function( user, cb ) {
        Lecture.findById( this.lecture, function( err, lecture ) {
                if (lecture) {
@@ -362,6 +412,14 @@ var ArchivedNote = new Schema({
   text: String
 })
 
+ArchivedNote.virtual( 'sanitized' ).get(function() {
+  var note = {
+    _id: this._id,
+    topic: this.topic === '' ? (this.text.replace(/(<(.|\n)*?>)|[\r\n\t]*/g, '')).substr(0, 15) + '...' : this.topic
+  }
+  return note;
+})
+
 mongoose.model( 'ArchivedNote', ArchivedNote )
 
 var ArchivedSubject = new Schema({