diff options
| author | HumairAK <humair88@hotmail.com> | 2016-07-26 07:43:08 +0000 |
|---|---|---|
| committer | HumairAK <humair88@hotmail.com> | 2016-07-26 07:43:08 +0000 |
| commit | 3983edb2518acd91f991ad5fc6308bcaf46700f5 (patch) | |
| tree | a81da7c47c8f9d0b853d478c619baadcf35d8237 /node_simple.js | |
| parent | f016bdbc35b7efa44bd63413bde09433ea916c59 (diff) | |
| parent | a345cf13d3b952ef0d427b61475f5cf722b3cbb0 (diff) | |
Merge branch 'master' of https://github.com/HumairAK/solutions_repo
Diffstat (limited to 'node_simple.js')
| -rw-r--r-- | node_simple.js | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/node_simple.js b/node_simple.js index 596cef6..228e861 100644 --- a/node_simple.js +++ b/node_simple.js @@ -552,10 +552,20 @@ exports.find_user_name = function (user_name, callback) { .then(function (db) { var logins = db.collection('logins'); + var admins = db.collection('admins'); logins.find( { user_name: user_name } ).toArray(function (err, result) { if (err) throw err; - else if (result.length == 0) { // nothing was found so this user is new - callback(false); + else if (result.length == 0) { // nothing was found in users + // check if user_name is taken by admin + admins.find({username : user_name}).toArray(function(err, data) { + if (err) throw err; + else if (data.length == 0) { + callback(false); + } + else { + callback(true); + } + }); } else { callback(true); @@ -1240,25 +1250,32 @@ exports.addAdmin = function (admin_data, callback) { console.log('addAdmin, admin_data: ' + admin_data.fname); console.log("inside addAdmin"); - + var logins = db.collection('logins'); // Check if the admin username already exists. Also check for user username. If it does, then we don't add the admin_data and return a message. exports.adminExists( admin_data.username , function (error, exists, data, message) { console.log("adminExists: " + message); if (!exists && !error) { - mongoFactory.getConnection(uri).then(function (db) { - - var admins = db.collection('admins'); - - admins.insertOne( admin_data, function (err) { - if (err) { - callback(false, true, message); - } - else { - callback(true, false, "Admin added."); - } - - }); + logins.find({username: username}).toArray(function (err, result) { + if (err){ + callback(true, false, null, "Error: could not retreive logins collection in addAdmin()."); + } else if (result.length) { + callback(false, false, 'User with given username already exists.'); + } else { + mongoFactory.getConnection(uri).then(function (db) { + + var admins = db.collection('admins'); + + admins.insertOne(admin_data, function (err) { + if (err) { + callback(false, true, message); + } + else { + callback(true, false, "Admin added."); + } + }); + }); + } }).catch(function (err) { callbackUser(false, true, "Unable to connect."); }) |
