Repository: ignite Updated Branches: refs/heads/ignite-843-rc2 2cb3d9caf -> 389c66256
IGNITE-843 WIP rework server side to promises. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/389c6625 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/389c6625 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/389c6625 Branch: refs/heads/ignite-843-rc2 Commit: 389c66256c827df16cf5c9ac3d28e40779e0b8eb Parents: 2cb3d9c Author: AKuznetsov <[email protected]> Authored: Mon Feb 15 23:37:51 2016 +0700 Committer: AKuznetsov <[email protected]> Committed: Mon Feb 15 23:37:51 2016 +0700 ---------------------------------------------------------------------- .../control-center-web/src/main/js/package.json | 1 - .../src/main/js/serve/mongo.js | 31 +- .../src/main/js/serve/routes/admin.js | 140 +++---- .../src/main/js/serve/routes/agent.js | 32 +- .../src/main/js/serve/routes/caches.js | 197 ++++------ .../src/main/js/serve/routes/clusters.js | 178 ++++----- .../src/main/js/serve/routes/domains.js | 365 ++++++++----------- .../src/main/js/serve/routes/igfs.js | 55 +-- .../src/main/js/serve/routes/notebooks.js | 104 ++---- .../src/main/js/serve/routes/profile.js | 7 +- .../src/main/js/serve/routes/public.js | 20 +- 11 files changed, 451 insertions(+), 679 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/package.json ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/package.json b/modules/control-center-web/src/main/js/package.json index 7910f35..9eef666 100644 --- a/modules/control-center-web/src/main/js/package.json +++ b/modules/control-center-web/src/main/js/package.json @@ -21,7 +21,6 @@ "node": ">=0.12.4" }, "dependencies": { - "async": "1.5.0", "babel-eslint": "^4.1.6", "body-parser": "~1.14.1", "bootstrap-sass": "^3.3.6", http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/mongo.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/mongo.js b/modules/control-center-web/src/main/js/serve/mongo.js index f6ff790..83d3b67 100644 --- a/modules/control-center-web/src/main/js/serve/mongo.js +++ b/modules/control-center-web/src/main/js/serve/mongo.js @@ -39,6 +39,8 @@ module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, p const ObjectId = mongoose.Schema.Types.ObjectId; const result = { connection: mongoose.connection }; + result.ObjectId = ObjectId; + // Define Account schema. const AccountSchema = new Schema({ username: String, @@ -527,26 +529,19 @@ module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, p // Define Notebook model. result.Notebook = mongoose.model('Notebook', NotebookSchema); - result.upsert = function(Model, data, cb) { - if (data._id) { - const id = data._id; - - delete data._id; - - Model.findOneAndUpdate({_id: id}, data, cb); - } - else - new Model(data).save(cb); + result.handleError = function(res, err) { + // TODO IGNITE-843 Send error to admin + res.status(err.code || 500).send(err.message); }; - result.processed = function(err, res) { - if (err) { - res.status(500).send(err); - - return false; - } - - return true; + /** + * Query for user spaces. + * + * @param userId User ID. + * @returns {Promise} + */ + result.spaces = function(userId) { + return result.Space.find({owner: userId}).exec(); }; // Registering the routes of all plugin modules http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/admin.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/admin.js b/modules/control-center-web/src/main/js/serve/routes/admin.js index 2f26f7a..0736277 100644 --- a/modules/control-center-web/src/main/js/serve/routes/admin.js +++ b/modules/control-center-web/src/main/js/serve/routes/admin.js @@ -25,104 +25,106 @@ module.exports = { }; module.exports.factory = function(_, express, nodemailer, settings, mongo) { - return new Promise((resolve) => { + return new Promise((factoryResolve) => { const router = new express.Router(); /** * Get list of user accounts. */ - router.post('/list', function(req, res) { - mongo.Account.find({}).sort('username').exec(function(err, users) { - if (err) - return res.status(500).send(err.message); - - res.json(users); - }); + router.post('/list', (req, res) => { + mongo.Account.find({}).sort('username').exec() + .then((users) => res.json(users)) + .catch((err) => mongo.handleError(res, err)); }); // Remove user. - router.post('/remove', function(req, res) { + router.post('/remove', (req, res) => { const userId = req.body.userId; - mongo.Account.findByIdAndRemove(userId, function(errAccount, user) { - if (errAccount) - return res.status(500).send(errAccount.message); + mongo.Account.findByIdAndRemove(userId).exec() + .then(() => mongo.spaces(userId)) + .then((spaces) => { + const promises = []; - mongo.Space.find({owner: userId}, function(errSpace, spaces) { _.forEach(spaces, (space) => { - mongo.Cluster.remove({space: space._id}).exec(); - mongo.Cache.remove({space: space._id}).exec(); - mongo.DomainModel.remove({space: space._id}).exec(); - mongo.Notebook.remove({space: space._id}).exec(); - mongo.Space.remove({owner: space._id}).exec(); + promises.push(mongo.Cluster.remove({space: space._id}).exec()); + promises.push(mongo.Cache.remove({space: space._id}).exec()); + promises.push(mongo.DomainModel.remove({space: space._id}).exec()); + promises.push(mongo.Notebook.remove({space: space._id}).exec()); + promises.push(mongo.Space.remove({owner: space._id}).exec()); }); - }); - - const transporter = { - service: settings.smtp.service, - auth: { - user: settings.smtp.email, - pass: settings.smtp.password - } - }; - - if (transporter.service !== '' || transporter.auth.user !== '' || transporter.auth.pass !== '') { - const mailer = nodemailer.createTransport(transporter); - - const mailOptions = { - from: settings.smtp.address(settings.smtp.username, settings.smtp.email), - to: settings.smtp.address(user.username, user.email), - subject: 'Your account was deleted', - text: 'You are receiving this e-mail because admin remove your account.\n\n' + - '--------------\n' + - 'Apache Ignite Web Console http://' + req.headers.host + '\n' - }; - - mailer.sendMail(mailOptions, function(errMailer) { - if (errMailer) - return res.status(503).send('Account was removed, but failed to send e-mail notification to user!<br />' + errMailer); - - res.sendStatus(200); + + return Promise.all(promises); + }) + .then(() => { + return new Promise((resolveMail, rejectMail) => { + const transporter = { + service: settings.smtp.service, + auth: { + user: settings.smtp.email, + pass: settings.smtp.password + } + }; + + if (transporter.service !== '' || transporter.auth.user !== '' || transporter.auth.pass !== '') { + const mailer = nodemailer.createTransport(transporter); + + const mailOptions = { + from: settings.smtp.address(settings.smtp.username, settings.smtp.email), + to: settings.smtp.address(user.username, user.email), + subject: 'Your account was deleted', + text: 'You are receiving this e-mail because admin remove your account.\n\n' + + '--------------\n' + + settings.smtp.username + ' http://' + req.headers.host + '\n' + }; + + mailer.sendMail(mailOptions, (errMailer) => { + if (errMailer) { + rejectMail({ + code: 503, + message: 'Account was removed, but failed to send e-mail notification to user!<br />' + errMailer + }); + } + else + resolveMail(); + }); + } + else + rejectMail({code: 503, message: 'Account was removed, but failed to send e-mail notification to user, because mailer is not configured!'}); }); - } - else - res.sendStatus(200); - }); + }) + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); // Save user. - router.post('/save', function(req, res) { - const userId = req.body.userId; - const adminFlag = req.body.adminFlag; + router.post('/save', (req, res) => { + const params = req.body; - mongo.Account.findByIdAndUpdate(userId, {admin: adminFlag}, function(err) { - if (err) - return res.status(500).send(err.message); - - res.sendStatus(200); - }); + mongo.Account.findByIdAndUpdate(params.userId, {admin: params.adminFlag}).exec() + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); // Become user. - router.get('/become', function(req, res) { - mongo.Account.findById(req.query.viewedUserId).exec(function(err, viewedUser) { - if (err) - return res.sendStatus(404); - - req.session.viewedUser = viewedUser; + router.get('/become', (req, res) => { + mongo.Account.findById(req.query.viewedUserId).exec() + .then((viewedUser) => { + req.session.viewedUser = viewedUser; - return res.sendStatus(200); - }); + res.sendStatus(200); + }) + .catch(() => res.sendStatus(404)); }); - // Become user. - router.get('/revert/identity', function(req, res) { + // Revert to your identity. + router.get('/revert/identity', (req, res) => { req.session.viewedUser = null; return res.sendStatus(200); }); - resolve(router); + factoryResolve(router); }); }; http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/agent.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/agent.js b/modules/control-center-web/src/main/js/serve/routes/agent.js index 40e0c82..0451e52 100644 --- a/modules/control-center-web/src/main/js/serve/routes/agent.js +++ b/modules/control-center-web/src/main/js/serve/routes/agent.js @@ -57,7 +57,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }; const _handleException = (res) => { - return function(error) { + return (error) => { if (_.isObject(error)) return res.status(error.code).send(error.message); @@ -66,17 +66,17 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }; /* Get grid topology. */ - router.get('/download/zip', function(req, res) { + router.get('/download/zip', (req, res) => { const agentFld = settings.agent.file; const agentZip = agentFld + '.zip'; const agentPathZip = 'public/agent/' + agentFld + '.zip'; - fs.stat(agentPathZip, function(err, stats) { + fs.stat(agentPathZip, (err, stats) => { if (err) return res.download(agentPathZip, agentZip); // Read a zip file. - fs.readFile(agentPathZip, function(errFs, data) { + fs.readFile(agentPathZip, (errFs, data) => { if (errFs) return res.download(agentPathZip, agentZip); @@ -108,7 +108,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Get grid topology. */ - router.post('/topology', function(req, res) { + router.post('/topology', (req, res) => { _client(req.currentUserId()) .then((client) => client.ignite(req.body.demo).cluster(req.body.attr, req.body.mtr)) .then((clusters) => res.json(clusters)) @@ -116,7 +116,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Execute query. */ - router.post('/query', function(req, res) { + router.post('/query', (req, res) => { _client(req.currentUserId()) .then((client) => { // Create sql query. @@ -136,7 +136,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Execute query getAll. */ - router.post('/query/getAll', function(req, res) { + router.post('/query/getAll', (req, res) => { _client(req.currentUserId()) .then((client) => { // Create sql query. @@ -148,7 +148,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, // Get query cursor. const cursor = client.ignite(req.body.demo).cache(req.body.cacheName).query(qry); - return new Promise(function(resolve) { + return new Promise((resolve) => { cursor.getAll().then((rows) => resolve({meta: cursor.fieldsMetadata(), rows})); }); }) @@ -157,7 +157,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Execute query. */ - router.post('/scan', function(req, res) { + router.post('/scan', (req, res) => { _client(req.currentUserId()) .then((client) => { // Create sql query. @@ -178,7 +178,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Get next query page. */ - router.post('/query/fetch', function(req, res) { + router.post('/query/fetch', (req, res) => { _client(req.currentUserId()) .then((client) => { const cache = client.ignite(req.body.demo).cache(req.body.cacheName); @@ -194,7 +194,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Close query cursor by id. */ - router.post('/query/close', function(req, res) { + router.post('/query/close', (req, res) => { _client(req.currentUserId()) .then((client) => { const cache = client.ignite(req.body.demo).cache(req.body.cacheName); @@ -206,7 +206,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Get metadata for cache. */ - router.post('/cache/metadata', function(req, res) { + router.post('/cache/metadata', (req, res) => { _client(req.currentUserId()) .then((client) => client.ignite(req.body.demo).cache(req.body.cacheName).metadata()) .then((caches) => { @@ -292,14 +292,14 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /* Ping client. */ - router.post('/ping', function(req, res) { + router.post('/ping', (req, res) => { _client(req.currentUserId()) .then(() => res.sendStatus(200)) .catch(_handleException(res)); }); /* Get JDBC drivers list. */ - router.post('/drivers', function(req, res) { + router.post('/drivers', (req, res) => { _client(req.currentUserId()) .then((client) => client.availableDrivers()) .then((arr) => res.json(arr)) @@ -307,7 +307,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /** Get database schemas. */ - router.post('/schemas', function(req, res) { + router.post('/schemas', (req, res) => { _client(req.currentUserId()) .then((client) => { const args = req.body; @@ -321,7 +321,7 @@ module.exports.factory = function(_, express, apacheIgnite, fs, JSZip, settings, }); /** Get database tables. */ - router.post('/tables', function(req, res) { + router.post('/tables', (req, res) => { _client(req.currentUserId()) .then((client) => { const args = req.body; http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/caches.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/caches.js b/modules/control-center-web/src/main/js/serve/routes/caches.js index 1f5a01c..77c3943 100644 --- a/modules/control-center-web/src/main/js/serve/routes/caches.js +++ b/modules/control-center-web/src/main/js/serve/routes/caches.js @@ -25,7 +25,7 @@ module.exports = { }; module.exports.factory = function(_, express, mongo) { - return new Promise((resolve) => { + return new Promise((factoryResolve) => { const router = new express.Router(); /** @@ -35,70 +35,33 @@ module.exports.factory = function(_, express, mongo) { * @param res Response. */ router.post('/list', (req, res) => { - const user_id = req.currentUserId(); + const result = {}; + let spacesIds = []; // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (mongo.processed(errSpace, res)) { - const space_ids = spaces.map((value) => value._id); - - // Get all clusters for spaces. - mongo.Cluster.find({space: {$in: space_ids}}, '_id name caches').sort('name').exec((errCluster, clusters) => { - if (mongo.processed(errCluster, res)) { - // Get all domain models for spaces. - mongo.DomainModel.find({space: {$in: space_ids}}).sort('name').exec((errDomainModel, domains) => { - if (mongo.processed(errDomainModel, res)) { - // Get all caches for spaces. - mongo.Cache.find({space: {$in: space_ids}}).sort('name').exec((err, caches) => { - if (mongo.processed(err, res)) { - _.forEach(clusters, (cluster) => { - cluster.caches = _.filter(cluster.caches, (cacheId) => { - return _.find(caches, {_id: cacheId}); - }); - }); - - _.forEach(domains, (domain) => { - domain.caches = _.filter(domain.caches, (cacheId) => { - return _.find(caches, {_id: cacheId}); - }); - }); - - _.forEach(caches, (cache) => { - // Remove deleted clusters. - cache.clusters = _.filter(cache.clusters, (clusterId) => { - return _.findIndex(clusters, (cluster) => { - return cluster._id.equals(clusterId); - }) >= 0; - }); - - // Remove deleted domain models. - cache.domains = _.filter(cache.domains, (metaId) => { - return _.findIndex(domains, (domain) => { - return domain._id.equals(metaId); - }) >= 0; - }); - }); - - res.json({ - spaces, - clusters: clusters.map((cluster) => { - return { - value: cluster._id, - label: cluster.name, - caches: cluster.caches - }; - }), - domains, - caches - }); - } - }); - } - }); - } - }); - } - }); + mongo.spaces(req.currentUserId()) + .then((spaces) => { + result.spaces = spaces; + spacesIds = spaces.map((value) => value._id); + + return mongo.Cluster.find({space: {$in: result.spacesIds}}).sort('name').exec(); + }) + .then((clusters) => { + result.clusters = clusters; + + return mongo.DomainModel.find({space: {$in: spacesIds}}).sort('name').exec(); + }) + .then((domains) => { + result.domains = domains; + + return mongo.Cache.find({space: {$in: spacesIds}}).sort('name').exec(); + }) + .then((caches) => { + result.caches = caches; + + res.json(result); + }) + .catch((err) => mongo.handleError(res, err)); }); /** @@ -111,49 +74,30 @@ module.exports.factory = function(_, express, mongo) { let cacheId = params._id; if (params._id) { - mongo.Cache.update({_id: cacheId}, params, {upsert: true}, (errCache) => { - if (mongo.processed(errCache, res)) { - mongo.Cluster.update({_id: {$in: clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}, (errClusterAdd) => { - if (mongo.processed(errClusterAdd, res)) { - mongo.Cluster.update({_id: {$nin: clusters}}, {$pull: {caches: cacheId}}, {multi: true}, (errClusterPull) => { - if (mongo.processed(errClusterPull, res)) { - mongo.DomainModel.update({_id: {$in: domains}}, {$addToSet: {caches: cacheId}}, {multi: true}, (errDomainModelAdd) => { - if (mongo.processed(errDomainModelAdd, res)) { - mongo.DomainModel.update({_id: {$nin: domains}}, {$pull: {caches: cacheId}}, {multi: true}, (errDomainModelPull) => { - if (mongo.processed(errDomainModelPull, res)) - res.send(params._id); - }); - } - }); - } - }); - } - }); - } - }); + mongo.Cache.update({_id: cacheId}, params, {upsert: true}).exec() + .then(() => mongo.Cluster.update({_id: {$in: clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec()) + .then(() => mongo.Cluster.update({_id: {$nin: clusters}}, {$pull: {caches: cacheId}}, {multi: true}).exec()) + .then(() => mongo.DomainModel.update({_id: {$in: domains}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec()) + .then(() => mongo.DomainModel.update({_id: {$nin: domains}}, {$pull: {caches: cacheId}}, {multi: true}).exec()) + .then(() => res.send(cacheId)) + .catch((err) => mongo.handleError(res, err)); } else { - mongo.Cache.findOne({space: params.space, name: params.name}, (errCacheFind, cacheFound) => { - if (mongo.processed(errCacheFind, res)) { - if (cacheFound) - return res.status(500).send('Cache with name: "' + cacheFound.name + '" already exist.'); - - (new mongo.Cache(params)).save((errCacheSave, cache) => { - if (mongo.processed(errCacheSave, res)) { - cacheId = cache._id; - - mongo.Cluster.update({_id: {$in: clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}, (errCluster) => { - if (mongo.processed(errCluster, res)) { - mongo.DomainModel.update({_id: {$in: domains}}, {$addToSet: {caches: cacheId}}, {multi: true}, (errDomainModel) => { - if (mongo.processed(errDomainModel, res)) - res.send(cacheId); - }); - } - }); - } - }); - } - }); + mongo.Cache.findOne({space: params.space, name: params.name}).exec() + .then((cache) => { + if (cache) + throw new Error('Cache with name: "' + cache.name + '" already exist.'); + + return (new mongo.Cache(params)).save(); + }) + .then((cache) => { + cacheId = cache._id; + + return mongo.Cluster.update({_id: {$in: clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec(); + }) + .then(() => mongo.DomainModel.update({_id: {$in: domains}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec()) + .then(() => res.send(cacheId)) + .catch((err) => mongo.handleError(res, err)); } }); @@ -161,41 +105,34 @@ module.exports.factory = function(_, express, mongo) { * Remove cache by ._id. */ router.post('/remove', (req, res) => { - mongo.Cache.remove(req.body, (err) => { - if (mongo.processed(err, res)) - res.sendStatus(200); - }); + const params = req.body; + const cacheId = params._id; + + mongo.Cluster.update({caches: {$in: [cacheId]}}, {$pull: {caches: cacheId}}, {multi: true}).exec() + .then(() => mongo.DomainModel.update({caches: {$in: [cacheId]}}, {$pull: {caches: cacheId}}, {multi: true}).exec()) + .then(() => mongo.Cache.remove(params)) + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); /** * Remove all caches. */ router.post('/remove/all', (req, res) => { - const user_id = req.currentUserId(); + let spacesIds = []; - // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (mongo.processed(errSpace, res)) { - const space_ids = spaces.map((value) => value._id); - - mongo.Cache.remove({space: {$in: space_ids}}, (errCache) => { - if (errCache) - return res.status(500).send(errCache.message); - - mongo.Cluster.update({space: {$in: space_ids}}, {caches: []}, {multi: true}, (errCluster) => { - if (mongo.processed(errCluster, res)) { - mongo.DomainModel.update({space: {$in: space_ids}}, {caches: []}, {multi: true}, (errDomainModel) => { - if (mongo.processed(errDomainModel, res)) - res.sendStatus(200); - }); - } - }); - }); - } - }); + mongo.spaces(req.currentUserId()) + .then((spaces) => { + spacesIds = spaces.map((value) => value._id); + + return mongo.Cluster.update({space: {$in: spacesIds}}, {caches: []}, {multi: true}); + }) + .then(() => mongo.DomainModel.update({space: {$in: spacesIds}}, {caches: []}, {multi: true})) + .then(() => mongo.Cache.remove({space: {$in: spacesIds}})) + .catch((err) => mongo.handleError(res, err)); }); - resolve(router); + factoryResolve(router); }); }; http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/clusters.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/clusters.js b/modules/control-center-web/src/main/js/serve/routes/clusters.js index 256de5d..468e912 100644 --- a/modules/control-center-web/src/main/js/serve/routes/clusters.js +++ b/modules/control-center-web/src/main/js/serve/routes/clusters.js @@ -25,7 +25,7 @@ module.exports = { }; module.exports.factory = function(_, express, mongo) { - return new Promise((resolve) => { + return new Promise((factoryResolve) => { const router = new express.Router(); /** @@ -35,59 +35,32 @@ module.exports.factory = function(_, express, mongo) { * @param res Response. */ router.post('/list', (req, res) => { - const user_id = req.currentUserId(); - - // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (mongo.processed(errSpace, res)) { - const space_ids = spaces.map((value) => value._id); - - // Get all caches for spaces. - mongo.Cache.find({space: {$in: space_ids}}).sort('name').deepPopulate('domains').exec((errCache, caches) => { - if (mongo.processed(errCache, res)) { - // Get all IGFSs for spaces. - mongo.Igfs.find({space: {$in: space_ids}}).sort('name').exec((errIgfs, igfss) => { - if (mongo.processed(errIgfs, res)) { - // Get all clusters for spaces. - mongo.Cluster.find({space: {$in: space_ids}}).sort('name').deepPopulate(mongo.ClusterDefaultPopulate).exec((errCluster, clusters) => { - if (mongo.processed(errCluster, res)) { - _.forEach(caches, (cache) => { - // Remove deleted caches. - cache.clusters = _.filter(cache.clusters, (clusterId) => { - return _.find(clusters, {_id: clusterId}); - }); - }); - - _.forEach(igfss, (igfs) => { - // Remove deleted caches. - igfs.clusters = _.filter(igfs.clusters, (clusterId) => { - return _.find(clusters, {_id: clusterId}); - }); - }); - - _.forEach(clusters, (cluster) => { - // Remove deleted caches. - cluster.caches = _.filter(cluster.caches, (cacheId) => { - return _.find(caches, {_id: cacheId}); - }); - - // Remove deleted IGFS. - cluster.igfss = _.filter(cluster.igfss, (igfsId) => { - return _.findIndex(igfss, (igfs) => { - return igfs._id.equals(igfsId); - }) >= 0; - }); - }); - - res.json({ spaces, caches, igfss, clusters }); - } - }); - } - }); - } - }); - } - }); + const result = {}; + let spacesIds = []; + + mongo.spaces(req.currentUserId()) + .then((spaces) => { + result.spaces = spaces; + spacesIds = spaces.map((value) => value._id); + + return mongo.Cache.find({space: {$in: spacesIds}}).sort('name').exec(); + }) + .then((caches) => { + result.caches = caches; + + return mongo.Igfs.find({space: {$in: spacesIds}}).sort('name').exec(); + }) + .then((igfss) => { + result.igfss = igfss; + + return mongo.Cluster.find({space: {$in: spacesIds}}).sort('name').deepPopulate(mongo.ClusterDefaultPopulate).exec(); + }) + .then((clusters) => { + result.clusters = clusters; + + res.json(result); + }) + .catch((err) => mongo.handleError(res, err)); }); /** @@ -96,40 +69,36 @@ module.exports.factory = function(_, express, mongo) { router.post('/save', (req, res) => { const params = req.body; const caches = params.caches; + const igfss = params.igfss; let clusterId = params._id; if (params._id) { - mongo.Cluster.update({_id: params._id}, params, {upsert: true}, (errCluster) => { - if (mongo.processed(errCluster, res)) { - mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {clusters: clusterId}}, {multi: true}, (errCacheAdd) => { - if (mongo.processed(errCacheAdd, res)) { - mongo.Cache.update({_id: {$nin: caches}}, {$pull: {clusters: clusterId}}, {multi: true}, (errCachePull) => { - if (mongo.processed(errCachePull, res)) - res.send(params._id); - }); - } - }); - } - }); + mongo.Cluster.update({_id: params._id}, params, {upsert: true}).exec() + .then(() => mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {clusters: clusterId}}, {multi: true}).exec()) + .then(() => mongo.Cache.update({_id: {$nin: caches}}, {$pull: {clusters: clusterId}}, {multi: true}).exec()) + .then(() => mongo.Igfs.update({_id: {$in: igfss}}, {$addToSet: {clusters: clusterId}}, {multi: true}).exec()) + .then(() => mongo.Igfs.update({_id: {$nin: igfss}}, {$pull: {clusters: clusterId}}, {multi: true}).exec()) + .then(() => res.send(clusterId)) + .catch((err) => mongo.handleError(res, err)); } else { - mongo.Cluster.findOne({space: params.space, name: params.name}, (errClusterFound, clusterFound) => { - if (mongo.processed(errClusterFound, res)) { - if (clusterFound) - return res.status(500).send('Cluster with name: "' + clusterFound.name + '" already exist.'); - - (new mongo.Cluster(params)).save((errCluster, cluster) => { - if (mongo.processed(errCluster, res)) { - clusterId = cluster._id; - - mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {clusters: clusterId}}, {multi: true}, (errCacheAdd) => { - if (mongo.processed(errCacheAdd, res)) - res.send(clusterId); - }); - } - }); - } - }); + mongo.Cluster.findOne({space: params.space, name: params.name}).exec() + .then((cluster) => { + if (cluster) + return res.status(500).send('Cluster with name: "' + cluster.name + '" already exist.'); + + return (new mongo.Cluster(params)).save(); + }) + .then((cluster) => { + clusterId = cluster._id; + + return mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {clusters: clusterId}}, {multi: true}).exec(); + }) + .then(() => mongo.Cache.update({_id: {$nin: caches}}, {$pull: {clusters: clusterId}}, {multi: true}).exec()) + .then(() => mongo.Igfs.update({_id: {$in: igfss}}, {$addToSet: {clusters: clusterId}}, {multi: true}).exec()) + .then(() => mongo.Igfs.update({_id: {$nin: igfss}}, {$pull: {clusters: clusterId}}, {multi: true}).exec()) + .then(() => res.send(clusterId)) + .catch((err) => mongo.handleError(res, err)); } }); @@ -137,46 +106,35 @@ module.exports.factory = function(_, express, mongo) { * Remove cluster by ._id. */ router.post('/remove', (req, res) => { - const clusterId = req.body; + const params = req.body; + const clusterId = params._id; mongo.Cache.update({clusters: {$in: [clusterId]}}, {$pull: {clusters: clusterId}}, {multi: true}).exec() .then(() => mongo.Igfs.update({clusters: {$in: [clusterId]}}, {$pull: {clusters: clusterId}}, {multi: true}).exec()) - .then(() => mongo.Cluster.remove(clusterId)) + .then(() => mongo.Cluster.remove(params)) .then(() => res.sendStatus(200)) - .catch((err) => { - // TODO IGNITE-843 Send error to admin - res.status(500).send(err.message); - }); + .catch((err) => mongo.handleError(res, err)); }); /** * Remove all clusters. */ router.post('/remove/all', (req, res) => { - const user_id = req.currentUserId(); + let spacesIds = []; // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (mongo.processed(errSpace, res)) { - const space_ids = spaces.map((value) => value._id); - - mongo.Cluster.remove({space: {$in: space_ids}}, (errCluster) => { - if (errCluster) - return res.status(500).send(errCluster.message); - - mongo.Cache.update({space: {$in: space_ids}}, {clusters: []}, {multi: true}, (errCache) => { - if (mongo.processed(errCache, res)) { - mongo.Igfs.update({space: {$in: space_ids}}, {clusters: []}, {multi: true}, (errIgfs) => { - if (mongo.processed(errIgfs, res)) - res.sendStatus(200); - }); - } - }); - }); - } - }); + mongo.spaces(req.currentUserId()) + .then((spaces) => { + spacesIds = spaces.map((value) => value._id); + + return mongo.Cluster.remove({space: {$in: spacesIds}}); + }) + .then(() => mongo.Cache.update({space: {$in: spacesIds}}, {clusters: []}, {multi: true}).exec()) + .then(() => mongo.Igfs.update({space: {$in: spacesIds}}, {clusters: []}, {multi: true}).exec()) + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); - resolve(router); + factoryResolve(router); }); }; http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/domains.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/domains.js b/modules/control-center-web/src/main/js/serve/routes/domains.js index 368258e..922fff2 100644 --- a/modules/control-center-web/src/main/js/serve/routes/domains.js +++ b/modules/control-center-web/src/main/js/serve/routes/domains.js @@ -21,11 +21,11 @@ module.exports = { implements: 'domains-routes', - inject: ['require(lodash)', 'require(express)', 'require(async)', 'mongo'] + inject: ['require(lodash)', 'require(express)', 'mongo'] }; module.exports.factory = function(_, express, async, mongo) { - return new Promise((resolve) => { + return new Promise((factoryResolve) => { const router = new express.Router(); /** @@ -35,251 +35,198 @@ module.exports.factory = function(_, express, async, mongo) { * @param res Response. */ router.post('/list', (req, res) => { - const user_id = req.currentUserId(); - - // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (mongo.processed(errSpace, res)) { - const space_ids = spaces.map((value) => value._id); - - // Get all clusters for spaces. - mongo.Cluster.find({space: {$in: space_ids}}, '_id name').sort('name').exec((errCluster, clusters) => { - if (mongo.processed(errCluster, res)) { - // Get all caches for spaces. - mongo.Cache.find({space: {$in: space_ids}}).sort('name').exec((errCache, caches) => { - if (mongo.processed(errCache, res)) { - // Get all domain models for spaces. - mongo.DomainModel.find({space: {$in: space_ids}}).sort('valueType').exec((errDomainModel, domains) => { - if (mongo.processed(errDomainModel, res)) { - _.forEach(caches, (cache) => { - cache.domains = _.filter(cache.domains, (metaId) => { - return _.find(domains, {_id: metaId}); - }); - }); - - // Remove deleted caches. - _.forEach(domains, (domain) => { - domain.caches = _.filter(domain.caches, (cacheId) => { - return _.find(caches, {_id: cacheId}); - }); - }); - - res.json({ - spaces, - clusters: clusters.map((cluster) => ({value: cluster._id, label: cluster.name})), - caches, - domains - }); - } - }); - } - }); - } - }); - } - }); + const result = {}; + let spacesIds = []; + + mongo.spaces(req.currentUserId()) + .then((spaces) => { + result.spaces = spaces; + spacesIds = spaces.map((value) => value._id); + + return mongo.Cluster.find({space: {$in: spacesIds}}, '_id name').sort('name').exec(); + }) + .then((clusters) => { + result.clusters = clusters; + + return mongo.Cache.find({space: {$in: spacesIds}}).sort('name').exec(); + }) + .then((caches) => { + result.caches = caches; + + return mongo.DomainModel.find({space: {$in: spacesIds}}).sort('valueType').exec(); + }) + .then((domains) => { + result.domains = domains; + + res.json(result); + }) + .catch((err) => mongo.handleError(res, err)); }); - function _saveDomainModel(domain, savedDomains, callback) { - const caches = domain.caches; - let domainId = domain._id; - - const cacheStoreChanges = domain.cacheStoreChanges; - - if (domainId) { - mongo.DomainModel.update({_id: domain._id}, domain, {upsert: true}, (errDomainModel) => { - if (errDomainModel) - callback(errDomainModel); - else { - mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: domainId}}, {multi: true}, (errCacheAdd) => { - if (errCacheAdd) - callback(errCacheAdd); - else { - mongo.Cache.update({_id: {$nin: caches}}, {$pull: {domains: domainId}}, {multi: true}, (errCachePull) => { - if (errCachePull) - callback(errCachePull); - else { - savedDomains.push(domain); - - _updateCacheStore(cacheStoreChanges, callback); - } - }); - } - }); - } - }); - } - else { - mongo.DomainModel.findOne({space: domain.space, valueType: domain.valueType}, (errDomainModelFind, found) => { - if (errDomainModelFind) - callback(errDomainModelFind); - else if (found) - return callback('Domain model with value type: "' + found.valueType + '" already exist.'); - - (new mongo.DomainModel(domain)).save((errDomainModel, domainSaved) => { - if (errDomainModel) - callback(errDomainModel); - else { - domainId = domainSaved._id; - - mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: domainId}}, {multi: true}, (errCache) => { - if (errCache) - callback(errCache); - else { - savedDomains.push(domainSaved); - - _updateCacheStore(cacheStoreChanges, callback); - } - }); - } - }); - }); - } - } - - function _updateCacheStore(cacheStoreChanges, callback) { - if (cacheStoreChanges && cacheStoreChanges.length > 0) { - async.forEachOf(cacheStoreChanges, (change, idx, cb) => { - mongo.Cache.update({_id: {$eq: change.cacheId}}, change.change, {}, (err) => { - if (err) - cb(err); - else - cb(); - }); - }, callback); - } - else - callback(); - } - - function _save(domains, res) { - const savedDomains = []; - const generatedCaches = []; - - if (domains && domains.length > 0) { - async.forEachOf(domains, (domain, idx, callback) => { - if (domain.newCache) { - mongo.Cache.findOne({space: domain.space, name: domain.newCache.name}, (errCacheFind, cacheFound) => { - if (mongo.processed(errCacheFind, res)) { - if (cacheFound) { - // Cache already exists, just save domain model. - domain.caches = [cacheFound._id]; - - _saveDomainModel(domain, savedDomains, callback); - } - else { - // If cache not found, then create it and associate with domain model. - const newCache = domain.newCache; - newCache.space = domain.space; - - (new mongo.Cache(newCache)).save((errCache, cache) => { - const cacheId = cache._id; - - if (mongo.processed(errCache, res)) { - mongo.Cluster.update({_id: {$in: cache.clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}, (errCluster) => { - if (mongo.processed(errCluster, res)) { - domain.caches = [cacheId]; - generatedCaches.push(cache); - - _saveDomainModel(domain, savedDomains, callback); - } - }); - } - }); - } - } - }); - } - else - _saveDomainModel(domain, savedDomains, callback); - }, (err) => { - if (err) - res.status(500).send(err.message); - else - res.send({savedDomains, generatedCaches}); - }); - } - else - res.status(500).send('Nothing to save!'); - } + // function _saveDomainModel(domain, savedDomains, callback) { + // const caches = domain.caches; + // const cacheStoreChanges = domain.cacheStoreChanges; + // let domainId = domain._id; + // + // if (domainId) { + // Promise.all([ + // mongo.DomainModel.update({_id: domain._id}, domain, {upsert: true}).exec(), + // mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: domainId}}, {multi: true}).exec(), + // mongo.Cache.update({_id: {$nin: caches}}, {$pull: {domains: domainId}}, {multi: true}).exec() + // ]).then(() => { + // savedDomains.push(domain); + // + // _updateCacheStore(cacheStoreChanges, callback); + // }); + // } + // else { + // mongo.DomainModel.findOne({space: domain.space, valueType: domain.valueType}).exec() + // .then((found) => { + // if (found) + // reject(new Error('Domain model with value type: "' + found.valueType + '" already exist.')); + // + // return (new mongo.DomainModel(domain)).save(); + // }) + // .then((domainSaved) => { + // savedDomains.push(domainSaved); + // + // return mongo.Cache.update({_id: {$in: caches}}, {$addToSet: {domains: domainSaved._id}}, {multi: true}).exec(); + // }).then(() => _updateCacheStore(cacheStoreChanges)); + // } + // } + + // function _updateCacheStore(cacheStoreChanges, callback) { + // if (cacheStoreChanges && cacheStoreChanges.length > 0) { + // async.forEachOf(cacheStoreChanges, (change, idx, cb) => { + // mongo.Cache.update({_id: {$eq: change.cacheId}}, change.change, {}, (err) => { + // if (err) + // cb(err); + // else + // cb(); + // }); + // }, callback); + // } + // else + // callback(); + // } + + // function _save(domains, res) { + // if (domains && domains.length > 0) { + // const savedDomains = []; + // const generatedCaches = []; + // const promises = []; + // + // _.forEach(domains, (domain) => { + // promises.push(); + // }); + // + // Promise.all(promises) + // .then(() => res.send({savedDomains, generatedCaches})) + // .catch((err) => mongo.handleError(res, err)); + // + // //async.forEachOf(domains, (domain, idx, callback) => { + // // if (domain.newCache) { + // // mongo.Cache.findOne({space: domain.space, name: domain.newCache.name}, (errCacheFind, cacheFound) => { + // // if (mongo.processed(errCacheFind, res)) { + // // if (cacheFound) { + // // // Cache already exists, just save domain model. + // // domain.caches = [cacheFound._id]; + // // + // // _saveDomainModel(domain, savedDomains, callback); + // // } + // // else { + // // // If cache not found, then create it and associate with domain model. + // // const newCache = domain.newCache; + // // newCache.space = domain.space; + // // + // // (new mongo.Cache(newCache)).save((errCache, cache) => { + // // const cacheId = cache._id; + // // + // // if (mongo.processed(errCache, res)) { + // // mongo.Cluster.update({_id: {$in: cache.clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}, (errCluster) => { + // // if (mongo.processed(errCluster, res)) { + // // domain.caches = [cacheId]; + // // generatedCaches.push(cache); + // // + // // _saveDomainModel(domain, savedDomains, callback); + // // } + // // }); + // // } + // // }); + // // } + // // } + // // }); + // // } + // // else + // // _saveDomainModel(domain, savedDomains, callback); + // //} + // } + // else + // res.status(500).send('Nothing to save!'); + // } /** * Save domain model. */ router.post('/save', (req, res) => { - _save([req.body], res); + res.status(500).send('Not ready!'); + // _save([req.body], res); }); /** * Batch save domain models. */ router.post('/save/batch', (req, res) => { - _save(req.body, res); + res.status(500).send('Not ready!'); + // _save(req.body, res); }); /** * Remove domain model by ._id. */ router.post('/remove', (req, res) => { - mongo.DomainModel.remove(req.body, (err) => { - if (mongo.processed(err, res)) - res.sendStatus(200); - }); + mongo.DomainModel.remove(req.body) + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); /** * Remove all domain models. */ router.post('/remove/all', (req, res) => { - const user_id = req.currentUserId(); + let spacesIds = []; - // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (mongo.processed(errSpace, res)) { - const space_ids = spaces.map((value) => value._id); + mongo.spaces(req.currentUserId()) + .then((spaces) => { + spacesIds = spaces.map((value) => value._id); - mongo.DomainModel.remove({space: {$in: space_ids}}, (errDomainModel) => { - if (errDomainModel) - return res.status(500).send(errDomainModel.message); - - mongo.Cache.update({space: {$in: space_ids}}, {domains: []}, {multi: true}, (errCache) => { - if (mongo.processed(errCache, res)) - res.sendStatus(200); - }); - }); - } - }); + return mongo.Cache.update({space: {$in: spacesIds}}, {domains: []}, {multi: true}).exec(); + }) + .then(() => mongo.DomainModel.remove({space: {$in: spacesIds}}).exec()) + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); /** * Remove all generated demo domain models and caches. */ router.post('/remove/demo', (req, res) => { - const user_id = req.currentUserId(); - - // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (mongo.processed(errSpace, res)) { - const space_ids = spaces.map((value) => value._id); + let spacesIds = []; - // Remove all demo domain models. - mongo.DomainModel.remove({$and: [{space: {$in: space_ids}}, {demo: true}]}, (errDomainModel) => { - if (errDomainModel) - return res.status(500).send(errDomainModel.message); + // TODO IGNITE-843 also remove from links: Cache -> DomainModel ; DomainModel -> Cache; Cluster -> Cache. - // Remove all demo caches. - mongo.Cache.remove({$and: [{space: {$in: space_ids}}, {demo: true}]}, (errCache) => { - if (errCache) - return res.status(500).send(errCache.message); + mongo.spaces(req.currentUserId()) + .then((spaces) => { + spacesIds = spaces.map((value) => value._id); - res.sendStatus(200); - }); - }); - } - }); + return mongo.DomainModel.remove({$and: [{space: {$in: spacesIds}}, {demo: true}]}); + }) + .then(() => mongo.Cache.remove({$and: [{space: {$in: spacesIds}}, {demo: true}]})) + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); - resolve(router); + factoryResolve(router); }); }; http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/igfs.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/igfs.js b/modules/control-center-web/src/main/js/serve/routes/igfs.js index 7508f9a..1df089e 100644 --- a/modules/control-center-web/src/main/js/serve/routes/igfs.js +++ b/modules/control-center-web/src/main/js/serve/routes/igfs.js @@ -25,7 +25,7 @@ module.exports = { }; module.exports.factory = function(_, express, mongo) { - return new Promise((resolve) => { + return new Promise((factoryResolve) => { const router = new express.Router(); /** @@ -36,31 +36,27 @@ module.exports.factory = function(_, express, mongo) { */ router.post('/list', (req, res) => { const result = {}; + let spacesIds = []; // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: req.currentUserId()}, {usedBy: {$elemMatch: {account: req.currentUserId()}}}]}).exec() + mongo.spaces(req.currentUserId()) .then((spaces) => { result.spaces = spaces; - result.spacesIds = spaces.map((value) => value._id); + spacesIds = spaces.map((value) => value._id); - return mongo.Cluster.find({space: {$in: result.spacesIds}}, '_id name').sort('name').exec(); + return mongo.Cluster.find({space: {$in: spacesIds}}, '_id name').sort('name').exec(); }) .then((clusters) => { result.clusters = clusters; - return mongo.Igfs.find({space: {$in: result.spacesIds}}).sort('name').exec(); + return mongo.Igfs.find({space: {$in: spacesIds}}).sort('name').exec(); }) .then((igfss) => { - res.json({ - spaces: result.spaces, - clusters: result.clusters.map((cluster) => ({value: cluster._id, label: cluster.name})), - igfss - }); + result.igfss = igfss; + + res.json(result); }) - .catch((err) => { - // TODO IGNITE-843 Send error to admin - res.status(500).send(err.message); - }); + .catch((err) => mongo.handleError(res, err)); }); /** @@ -69,7 +65,6 @@ module.exports.factory = function(_, express, mongo) { router.post('/save', (req, res) => { const params = req.body; const clusters = params.clusters; - let igfsId = params._id; if (params._id) { @@ -77,10 +72,7 @@ module.exports.factory = function(_, express, mongo) { .then(() => mongo.Cluster.update({_id: {$in: clusters}}, {$addToSet: {igfss: igfsId}}, {multi: true}).exec()) .then(() => mongo.Cluster.update({_id: {$nin: clusters}}, {$pull: {igfss: igfsId}}, {multi: true}).exec()) .then(() => res.send(igfsId)) - .catch((err) => { - // TODO IGNITE-843 Send error to admin - res.status(500).send(err.message); - }); + .catch((err) => mongo.handleError(res, err)); } else { mongo.Igfs.findOne({space: params.space, name: params.name}).exec() @@ -96,10 +88,7 @@ module.exports.factory = function(_, express, mongo) { return mongo.Cluster.update({_id: {$in: clusters}}, {$addToSet: {igfss: igfsId}}, {multi: true}); }) .then(() => res.send(igfsId)) - .catch((err) => { - // TODO IGNITE-843 Send error to admin - res.status(500).send(err.message); - }); + .catch((err) => mongo.handleError(res, err)); } }); @@ -107,26 +96,23 @@ module.exports.factory = function(_, express, mongo) { * Remove IGFS by ._id. */ router.post('/remove', (req, res) => { - const igfsId = req.body; + const params = req.body; + const igfsId = params._id; mongo.Cluster.update({igfss: {$in: [igfsId]}}, {$pull: {igfss: igfsId}}, {multi: true}).exec() - .then(mongo.Igfs.remove(igfsId)) + .then(mongo.Igfs.remove(params)) .then(() => res.sendStatus(200)) - .catch((err) => { - // TODO IGNITE-843 Send error to admin - res.status(500).send(err.message); - }); + .catch((err) => mongo.handleError(res, err)); }); /** * Remove all IGFSs. */ router.post('/remove/all', (req, res) => { - const userId = req.currentUserId(); let spacesIds = []; // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: userId}, {usedBy: {$elemMatch: {account: userId}}}]}) + mongo.spaces(req.currentUserId()) .then((spaces) => { spacesIds = spaces.map((value) => value._id); @@ -134,13 +120,10 @@ module.exports.factory = function(_, express, mongo) { }) .then(() => mongo.Cluster.update({space: {$in: spacesIds}}, {igfss: []}, {multi: true})) .then(() => res.sendStatus(200)) - .catch((err) => { - // TODO IGNITE-843 Send error to admin - res.status(500).send(err.message); - }); + .catch((err) => mongo.handleError(res, err)); }); - resolve(router); + factoryResolve(router); }); }; http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/notebooks.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/notebooks.js b/modules/control-center-web/src/main/js/serve/routes/notebooks.js index e65f77d..5070bcc 100644 --- a/modules/control-center-web/src/main/js/serve/routes/notebooks.js +++ b/modules/control-center-web/src/main/js/serve/routes/notebooks.js @@ -25,7 +25,7 @@ module.exports = { }; module.exports.factory = function(express, mongo) { - return new Promise((resolve) => { + return new Promise((factoryResolve) => { const router = new express.Router(); /** @@ -35,23 +35,11 @@ module.exports.factory = function(express, mongo) { * @param res Response. */ router.post('/list', (req, res) => { - const user_id = req.currentUserId(); + mongo.spaces(req.currentUserId()) + .then((spaces) => mongo.Notebook.find({space: {$in: spaces.map((value) => value._id)}}).select('_id name').sort('name').exec()) + .then((notebooks) => res.json(notebooks)) + .catch((err) => mongo.handleError(res, err)); - // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (errSpace) - return res.status(500).send(errSpace.message); - - const space_ids = spaces.map((value) => value._id); - - // Get all metadata for spaces. - mongo.Notebook.find({space: {$in: space_ids}}).select('_id name').sort('name').exec((errNotebook, notebooks) => { - if (errNotebook) - return res.status(500).send(errNotebook.message); - - res.json(notebooks); - }); - }); }); /** @@ -61,23 +49,10 @@ module.exports.factory = function(express, mongo) { * @param res Response. */ router.post('/get', (req, res) => { - const user_id = req.currentUserId(); - - // Get owned space and all accessed space. - mongo.Space.find({$or: [{owner: user_id}, {usedBy: {$elemMatch: {account: user_id}}}]}, (errSpace, spaces) => { - if (errSpace) - return res.status(500).send(errSpace.message); - - const space_ids = spaces.map((value) => value._id); - - // Get all metadata for spaces. - mongo.Notebook.findOne({space: {$in: space_ids}, _id: req.body.noteId}).exec((errNotebook, notebook) => { - if (errNotebook) - return res.status(500).send(errNotebook.message); - - res.json(notebook); - }); - }); + mongo.spaces(req.currentUserId()) + .then((spaces) => mongo.Notebook.findOne({space: {$in: spaces.map((value) => value._id)}, _id: req.body.noteId}).exec()) + .then((notebook) => res.json(notebook)) + .catch((err) => mongo.handleError(res, err)); }); /** @@ -91,28 +66,20 @@ module.exports.factory = function(express, mongo) { const noteId = note._id; if (noteId) { - mongo.Notebook.update({_id: noteId}, note, {upsert: true}, (err) => { - if (err) - return res.status(500).send(err.message); - - res.send(noteId); - }); + mongo.Notebook.update({_id: noteId}, note, {upsert: true}).exec() + .then(() => res.send(noteId)) + .catch((err) => mongo.handleError(res, err)); } else { - mongo.Notebook.findOne({space: note.space, name: note.name}, (errNotebookFind, notebookFoud) => { - if (errNotebookFind) - return res.status(500).send(errNotebookFind.message); - - if (notebookFoud) - return res.status(500).send('Notebook with name: "' + notebookFoud.name + '" already exist.'); - - (new mongo.Notebook(req.body)).save((errNotebook, noteNew) => { - if (errNotebook) - return res.status(500).send(errNotebook.message); - - res.send(noteNew._id); - }); - }); + mongo.Notebook.findOne({space: note.space, name: note.name}).exec() + .then((notebook) => { + if (notebook) + throw new Error('Notebook with name: "' + notebook.name + '" already exist.'); + + return (new mongo.Notebook(req.body)).save(); + }) + .then((notebook) => res.send(notebook._id)) + .catch((err) => mongo.handleError(res, err)); } }); @@ -123,12 +90,9 @@ module.exports.factory = function(express, mongo) { * @param res Response. */ router.post('/remove', (req, res) => { - mongo.Notebook.remove(req.body, (err) => { - if (err) - return res.status(500).send(err.message); - - res.sendStatus(200); - }); + mongo.Notebook.remove(req.body) + .then(() => res.sendStatus(200)) + .catch((err) => mongo.handleError(res, err)); }); /** @@ -138,22 +102,12 @@ module.exports.factory = function(express, mongo) { * @param res Response. */ router.post('/new', (req, res) => { - const user_id = req.currentUserId(); - - // Get owned space and all accessed space. - mongo.Space.findOne({owner: user_id}, (errSpace, space) => { - if (errSpace) - return res.status(500).send(errSpace.message); - - (new mongo.Notebook({space: space.id, name: req.body.name})).save((errNotebook, note) => { - if (errNotebook) - return res.status(500).send(errNotebook.message); - - return res.send(note._id); - }); - }); + mongo.spaces(req.currentUserId()) + .then((spaces) => (new mongo.Notebook({space: spaces[0].id, name: req.body.name})).save()) + .then((notebook) => res.send(notebook._id)) + .catch((err) => mongo.handleError(res, err)); }); - resolve(router); + factoryResolve(router); }); }; http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/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 d611df4..6b166fd 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 @@ -31,7 +31,7 @@ module.exports.factory = function(_, express, mongo) { /** * Save user profile. */ - router.post('/save', function(req, res) { + router.post('/save', (req, res) => { const params = req.body; if (params.password && _.isEmpty(params.password)) @@ -79,10 +79,7 @@ module.exports.factory = function(_, express, mongo) { return user.save(); }) .then(() => res.sendStatus(200)) - .catch((err) => { - // TODO IGNITE-843 Send error to admin - res.status(500).send(err.message); - }); + .catch((err) => mongo.handleError(res, err)); }); resolveFactory(router); http://git-wip-us.apache.org/repos/asf/ignite/blob/389c6625/modules/control-center-web/src/main/js/serve/routes/public.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/serve/routes/public.js b/modules/control-center-web/src/main/js/serve/routes/public.js index 829e9d6..b83208d 100644 --- a/modules/control-center-web/src/main/js/serve/routes/public.js +++ b/modules/control-center-web/src/main/js/serve/routes/public.js @@ -25,7 +25,7 @@ module.exports = { }; module.exports.factory = function(express, passport, nodemailer, settings, mongo) { - return new Promise(function(factoryResolve) { + return new Promise((factoryResolve) => { const router = new express.Router(); const _randomString = () => { @@ -77,7 +77,7 @@ module.exports.factory = function(express, passport, nodemailer, settings, mongo }; // GET user. - router.post('/user', function(req, res) { + router.post('/user', (req, res) => { const becomeUsed = req.session.viewedUser && req.user.admin; let user = req.user; @@ -94,7 +94,7 @@ module.exports.factory = function(express, passport, nodemailer, settings, mongo /** * Register new account. */ - router.post('/signup', function(req, res) { + router.post('/signup', (req, res) => { mongo.Account.count().exec() .then((cnt) => { req.body.admin = cnt === 0; @@ -155,15 +155,15 @@ module.exports.factory = function(express, passport, nodemailer, settings, mongo /** * Login in exist account. */ - router.post('/signin', function(req, res, next) { - passport.authenticate('local', function(errAuth, user) { + router.post('/signin', (req, res, next) => { + passport.authenticate('local', (errAuth, user) => { if (errAuth) return res.status(401).send(errAuth.message); if (!user) return res.status(401).send('Invalid email or password'); - req.logIn(user, {}, function(errLogIn) { + req.logIn(user, {}, (errLogIn) => { if (errLogIn) return res.status(401).send(errLogIn.message); @@ -175,7 +175,7 @@ module.exports.factory = function(express, passport, nodemailer, settings, mongo /** * Logout. */ - router.post('/logout', function(req, res) { + router.post('/logout', (req, res) => { req.logout(); res.sendStatus(200); @@ -184,7 +184,7 @@ module.exports.factory = function(express, passport, nodemailer, settings, mongo /** * Send e-mail to user with reset token. */ - router.post('/password/forgot', function(req, res) { + router.post('/password/forgot', (req, res) => { mongo.Account.findOne({email: req.body.email}).exec() .then((user) => { if (!user) @@ -212,7 +212,7 @@ module.exports.factory = function(express, passport, nodemailer, settings, mongo /** * Change password with given token. */ - router.post('/password/reset', function(req, res) { + router.post('/password/reset', (req, res) => { mongo.Account.findOne({resetPasswordToken: req.body.token}).exec() .then((user) => { return new Promise((resolve, reject) => { @@ -243,7 +243,7 @@ module.exports.factory = function(express, passport, nodemailer, settings, mongo }); /* GET reset password page. */ - router.post('/password/validate/token', function(req, res) { + router.post('/password/validate/token', (req, res) => { const token = req.body.token; mongo.Account.findOne({resetPasswordToken: token}).exec()
