'use strict';

var express = require('express');
var validator = require('validator');
var email = require('../../email');
var models = require('../../models');
var auth = require('../../auth');
var router = express.Router();
var User = models.User;
var Project = models.Project;

router.use(auth.requireUserOr401);

router.route('/:provider/:id')

/**
 * @api {put} /:provider/:id Update Provider Account
 * @apiDescription Updates a provider account for the _active_ user (the API user).
 * @apiName UpdateAccount
 * @apiGroup Account
 * @apiVersion 1.0.0
 *
 * @apiParam {String} provider Type of provider, e.g. github
 * @apiParam {Number} id Unique provider identification
 */
  .put(function (req, res) {
    var accounts = req.user.accounts;
    var provider = req.params.provider;
    var id = req.params.id;

    for (var i = 0; i < accounts.length; i++) {
      if (accounts[i].provider === provider &&
        accounts[i].id === id) {
        // TODO validate these accounts
        accounts[i] = req.body;

        return User.update({_id: req.user._id}, {$set: {accounts: accounts}}, function (err, effected) {
          if (effected !== 1) {
            return res.status(500).send('Failed to save one user');
          }

          res.sendStatus(200);
        });
      }
    }

    accounts.push(req.body);

    User.update({_id: req.user._id}, {$set: {accounts: accounts}}, function (err, effected) {
      if (effected !== 1) {
        return res.status(500).send('Failed to save one user');
      }

      res.sendStatus(200);
    });
  })

  /**
   * @api {delete} /:provider/:id Delete Provider Account
   * @apiDescription Deletes a provider account for the _active_ user (the API user).
   * @apiName DeleteAccount
   * @apiGroup Account
   * @apiVersion 1.0.0
   *
   * @apiParam {String} provider Type of provider, e.g. github
   * @apiParam {Number} id Unique provider identification
   */
  .delete(function (req, res) {
    var accounts = req.user.accounts;
    var provider = req.params.provider;
    var id = req.params.id;
    var accountRemoved = false;

    Project.find({'provider.id': provider})
      .lean()
      .exec(function (err, projects) {
        if (err) {
          return res.status(400).send('Failed do to bad data');
        }

        if (projects.length) {
          var projectNames = projects.map(function (project) {
            return project.name;
          });
          return res.status(403)
            .send('Cannot delete provider since projects are using this provider: ' +
              projectNames.join(', '));
        } else {
          accounts.forEach(function (account, index) {
            if (account.provider === provider && account.id === id) {
              accounts.splice(index, 1);
              accountRemoved = true;
            }
          });

          if (accountRemoved) {
            return req.user.save(function (err) {
              if (err) {
                return res.status(500).send('Failed to save user');
              }

              res.sendStatus(204);
            });
          }

          res.status(404).send('Account not found');
        }
      });
  });

router.route('/password')

/**
 * @api {post} /password Change Password
 * @apiDescription Changes the password for the _active_ user (the API user).
 * @apiName ChangePassword
 * @apiGroup Account
 * @apiVersion 1.0.0
 *
 * @apiParam (RequestBody) {String{6..}} password The new password, which must be at least 6 characters long.
 */
  .post(function (req, res) {
    if (req.user !== undefined) {
      console.log('password change by ' + req.user.email);
    }

    var password = req.body.password;

    if (password.length < 6) {
      return res.status(400).json({
        status: 'error',
        errors: [{message: 'password must be at least 6 characters long'}]
      });
    }

    req.user.password = password;

    req.user.save(function (err) {
      if (err) {
        throw err;
      }

      email.notify_password_change(req.user);

      res.json({
        status: 'ok',
        errors: []
      });
    });
  });


router.route('/email')

/**
 * @api {post} /email Change Email
 * @apiDescription Changes the email address for the _active_ user (the API user).
 * @apiName ChangeEmail
 * @apiGroup Account
 * @apiVersion 1.0.0
 *
 * @apiParam (RequestBody) {String} email The new email address. This must be a VALID email address.
 */
  .post(function (req, res) {
    var newEmail = req.body.email;

    if (!validator.isEmail(newEmail)) {
      return res.status(400).json({

        status: 'error',
        errors: [{message: 'email is invalid'}]
      });
    }

    console.log('email change from ' + req.user.email + ' to ' + newEmail);

    var oldEmail = req.user.email;
    req.user.email = newEmail;

    req.user.save(function (err) {
      if (err) {
        return res.status(400).json({
          status: 'error',
          errors: [{message: 'email already in use'}]
        });
      }

      email.notify_email_change(req.user, oldEmail);
      res.json({status: 'ok', errors: []});
    });
  });

module.exports = router;

@@ -12,23 +12,20 @@
 ';%0A%0Avar 
-express
+auth
  = requi
@@ -32,53 +32,71 @@
 re('
-express');%0Avar validator = require('validator
+../../auth');%0Avar debug = require('debug')('strider:api:account
 ');%0A
@@ -127,24 +127,58 @@
 ../email');%0A
+var express = require('express');%0A
 var models =
@@ -211,36 +211,32 @@
 var 
-auth = require('../../auth')
+Project = models.Project
 ;%0Ava
@@ -296,32 +296,40 @@
 var 
-Project = models.Project
+validator = require('validator')
 ;%0A%0Ar
@@ -1152,34 +1152,24 @@
 unction (err
-, effected
 ) %7B%0A        
@@ -1175,32 +1175,45 @@
    if (e
-ffected !== 1) %7B
+rr) %7B%0A            debug(err);
 %0A       
@@ -1458,18 +1458,8 @@
 (err
-, effected
 ) %7B%0A
@@ -1473,24 +1473,33 @@
 f (e
-ffected !== 1) %7B
+rr) %7B%0A        debug(err);
 %0A   

Fix mongoose usage