var mongoose = require('mongoose')
var passportLocalMongoose = require('passport-local-mongoose')
var Notification = require('./notification')
var Friends = require('./friend')

var User = mongoose.Schema({
  email: {
    type: String,
    required: true,
    index: {
      unique: true
    }
  },
  username: {
    type: String,
    required: false
  },
  displayName: {
    type: String,
    required: false
  },
  resetToken: String,
  resetExpires: Date
})

User.methods.addFriend = function (email, callback) {
  var user = this
  var query = Friends.find({
    email: email
  })
  var getUser = this.model('user').findOne()
    .where('email').equals(email)
    .select('_id email displayName')
  getUser.exec(function (err, foundUser) {
    if (err) {
      throw err
    }
    if (foundUser) {
      console.log('[user.js] From user:', user)
      console.log('[user.js] User found:', foundUser)
      console.log('[user.js] Checking for existing friend request')
      query.where('added').equals(true).sort('-id').exec(function (err, friendReq) {
        if (err) {
          return err
        }
        if (friendReq.length < 1) {
          console.log('[user.js] No existing friend request')
          console.log('[user.js] creaing notification for user:', foundUser)
          var notif = new Notification({
            user_id: foundUser._id,
            type: 'request',
            title: 'New Friend Request',
            content: {
              created_by: user._id,
              message: user.displayName + ' added you as a friend!'
            }
          })
          console.log('[user.js] notification created:', notif)
          return notif.save(callback)
        } else {
          console.log('[user.js] Friend request already exists:', friendReq)
          return callback
        }
      })
    } else {
      console.log('[user.js] No user exists, need to implement email sending.')
      return callback
    }
  })
}

User.methods.findNotifications = function (callback) {
  var query = Notification.find({
    user_id: this._id
  })
  if (callback) {
    return query.where('viewed').equals(false).sort('-created').exec(callback)
  } else {
    return query
  }
}

User.methods.findFriends = function (callback) {
  var query = Friends.find({
    user_id: this._id
  })
  if (callback) {
    return query.where('added').equals(true).sort('-id').exec(callback)
  } else {
    return query
  }
}

User.plugin(passportLocalMongoose, {
  usernameField: 'email',
  usernameLowerCase: true,
  usernameQueryFields: ['email']
})
module.exports = mongoose.model('user', User)

@@ -779,31 +779,8 @@
   %7D%0A
-    if (foundUser) %7B%0A  
     
@@ -817,26 +817,24 @@
 er:', user)%0A
-  
     console.
@@ -869,26 +869,24 @@
  foundUser)%0A
-  
     console.
@@ -943,18 +943,16 @@
 t')%0A    
-  
 query.wh
@@ -1024,26 +1024,24 @@
 eq) %7B%0A      
-  
 if (err) %7B%0A 
@@ -1035,26 +1035,24 @@
  if (err) %7B%0A
-  
         retu
@@ -1060,30 +1060,26 @@
 n err%0A      
- 
- 
 %7D%0A
-  
       if (fr
@@ -1096,26 +1096,24 @@
 ngth %3C 1) %7B%0A
-  
         cons
@@ -1160,18 +1160,16 @@
 quest')%0A
-  
         
@@ -1243,18 +1243,16 @@
         
-  
 var noti
@@ -1284,25 +1284,26 @@
         
-  
 user_
-id
+email
 : foundU
@@ -1302,31 +1302,40 @@
 : foundUser.
-_id,%0A  
+email %7C%7C email,%0A
           ty
@@ -1359,18 +1359,16 @@
         
- 
- 
 title: '
@@ -1398,18 +1398,16 @@
         
-  
 content:
@@ -1421,18 +1421,16 @@
         
- 
- 
 created_
@@ -1443,18 +1443,16 @@
 er._id,%0A
-  
         
@@ -1515,30 +1515,26 @@
 '%0A          
-  %7D%0A  
+%7D%0A
         %7D)%0A 
@@ -1532,18 +1532,16 @@
      %7D)%0A
-  
         
@@ -1598,26 +1598,24 @@
 if)%0A        
-  
 return notif
@@ -1636,18 +1636,16 @@
 )%0A      
-  
 %7D else %7B
@@ -1641,26 +1641,24 @@
    %7D else %7B%0A
-  
         cons
@@ -1724,26 +1724,24 @@
 eq)%0A        
- 
- 
 return callb
@@ -1754,142 +1754,17 @@
     
-  
 %7D%0A    
- 
- 
 %7D)%0A
-    %7D else %7B%0A      console.log('%5Buser.js%5D No user exists, need to implement email sending.')%0A      return callback%0A    %7D%0A
   %7D)

Work on fixing friend requests