ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anovi...@apache.org
Subject ignite git commit: IGNITE-843 Refactor serve to es6.
Date Mon, 15 Feb 2016 02:30:48 GMT
Repository: ignite
Updated Branches:
  refs/heads/ignite-843-rc2 3eb0be4c0 -> 42c70d8b8


IGNITE-843 Refactor serve to es6.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/42c70d8b
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/42c70d8b
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/42c70d8b

Branch: refs/heads/ignite-843-rc2
Commit: 42c70d8b80bed7be22942fc1f64eb357f117aac0
Parents: 3eb0be4
Author: Andrey <anovikov@gridgain.com>
Authored: Mon Feb 15 09:31:07 2016 +0700
Committer: Andrey <anovikov@gridgain.com>
Committed: Mon Feb 15 09:31:07 2016 +0700

----------------------------------------------------------------------
 .../src/main/js/serve/routes/profile.js         | 95 ++++++++++----------
 1 file changed, 45 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/42c70d8b/modules/control-center-web/src/main/js/serve/routes/profile.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/serve/routes/profile.js b/modules/control-center-web/src/main/js/serve/routes/profile.js
index 2f63f12..5ce9b81 100644
--- a/modules/control-center-web/src/main/js/serve/routes/profile.js
+++ b/modules/control-center-web/src/main/js/serve/routes/profile.js
@@ -23,71 +23,66 @@ module.exports = {
 };
 
 module.exports.factory = function(_, express, mongo) {
-    return new Promise((resolve) => {
+    return new Promise((resolveFactory) => {
         const router = express.Router();
 
-        function _updateUser(res, params) {
-            mongo.Account.update({_id: params._id}, params, {upsert: true}, function(err,
user) {
-                // TODO IGNITE-843 Send error to admin.
-                if (err)
-                    return res.status(500).send('Failed to update profile!');
-
-                if (params.email)
-                    user.email = params.email;
-
-                res.sendStatus(200);
-            });
-        }
-
-        function _checkEmail(res, user, params) {
-            if (params.email && user.email !== params.email) {
-                mongo.Account.findOne({email: params.email}, function(err, userForEmail)
{
-                    // TODO send error to admin
-                    if (err)
-                        return res.status(500).send('Failed to check e-mail!');
-
-                    if (userForEmail && userForEmail._id !== user._id)
-                        return res.status(500).send('User with this e-mail already registered!');
-
-                    _updateUser(res, params);
-                });
-            }
-            else
-                _updateUser(res, params);
-        }
-
         /**
          * Save user profile.
          */
         router.post('/save', function(req, res) {
-            var params = req.body;
+            const params = req.body;
 
-            mongo.Account.findById(params._id, function(err, user) {
-                // TODO IGNITE-843 Send error to admin
-                if (err)
-                    return res.status(500).send('Failed to find user!');
+            if (params.password && _.isEmpty(params.password))
+                return res.status(500).send('Wrong value for new password!');
 
-                if (params.password) {
-                    if (_.isEmpty(params.password))
-                        return res.status(500).send('Wrong value for new password!');
+            mongo.Account.findById(params._id).exec()
+                .then((user) => {
+                    if (!params.password)
+                        return Promise.resolve(user);
 
-                    user.setPassword(params.password, function(err, user) {
-                        if (err)
-                            return res.status(500).send(err.message);
+                    return new Promise((resolve, reject) => {
+                        user.setPassword(params.password, (err, _user) => {
+                            if (err)
+                                return reject(err.message);
+
+                            delete params.password;
 
-                        user.save(function(err) {
+                            resolve(_user);
+                        });
+                    });
+                })
+                .then((user) => {
+                    if (!params.email || user.email === params.email)
+                        return Promise.resolve(user);
+
+                    return new Promise((resolve, reject) => {
+                        mongo.Account.findOne({email: params.email}, function (err, _user)
{
+                            // TODO send error to admin
                             if (err)
-                                return res.status(500).send('Failed to change password!');
+                                return reject('Failed to check e-mail!');
+
+                            if (_user && _user._id !== user._id)
+                                return reject('User with this e-mail already registered!');
 
-                            _checkEmail(res, user, params);
+                            resolve(user);
                         });
                     });
-                }
-                else
-                    _checkEmail(res, user, params);
-            });
+                })
+                .then((user) => {
+                    for (var param in params) {
+                        if (params.hasOwnProperty(param))
+                            user[param] = params[param];
+                    }
+
+                    return user.save();
+                })
+                .then(() => res.sendStatus(200))
+                .catch((err) => {
+                    // TODO IGNITE-843 Send error to admin
+                    res.status(500).send(err);
+                });
         });
 
-        resolve(router);
+        resolveFactory(router);
     });
 };


Mime
View raw message