Skip to content

Commit cf3158c

Browse files
committed
Merge branch 'merged-branches' into all-pull-requests
2 parents 74a9222 + 75560be commit cf3158c

15 files changed

+685
-226
lines changed

.codeclimate.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ engines:
1111
ratings:
1212
paths:
1313
- "src/**.js"
14-
exclude_paths: []
14+
exclude_paths:
15+
- tests-unit/**/*
16+
- tests-acceptance/**/*

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"http-proxy": "^1.8.1",
3030
"jsonschema": "^1.0.0",
3131
"lodash": "^4.3.0",
32-
"mongodb": "^1.4.40",
32+
"mongodb": "^2.1.7",
3333
"passport": "^0.3.2",
3434
"passport-forcedotcom": "^0.1.1",
3535
"passport-google-oauth2": "^0.1.6",

src/managers/dao.js

+74-94
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const async = require('async');
55
const escapeRegexp = require('escape-regexp');
66
const config = require('../../config');
77
const MongoClient = require('mongodb').MongoClient;
8-
const ObjectID = require('mongodb').ObjectID;
98
const _ = require('lodash');
109

1110
const TIME_TO_REFRESH = 1000 * 60 * 60;
@@ -96,12 +95,14 @@ function addUser(userToAdd, cbk) {
9695
user.roles = ['user'];
9796
}
9897

99-
return usersCollection.insert(user, function (err, result) {
98+
return usersCollection.insertOne(user, function (err, insertResult) {
10099
if (err) {
101100
return cbk(err, null);
102101
}
103102

104-
return cbk(null, result[0]);
103+
user._id = insertResult.insertedId;
104+
105+
return cbk(null, user);
105106
});
106107
}
107108
return cbk(err);
@@ -123,7 +124,7 @@ function findByEmail(email, callback) {
123124

124125
const targetEmail = makeRegEx(email);
125126

126-
usersCollection.find({username: targetEmail}, {password: 0}).toArray(function (error, foundUsers) {
127+
usersCollection.count({username: targetEmail}, function (error, totalCount) {
127128

128129
if (error) {
129130
return callback({
@@ -135,106 +136,75 @@ function findByEmail(email, callback) {
135136
});
136137
}
137138

138-
if (_.isEmpty(foundUsers)) {
139-
return callback(null, {available: true});
139+
if (totalCount) {
140+
return callback(null, {available: false});
140141
}
141142

142-
return callback(null, {available: false});
143+
return callback(null, {available: true});
143144
});
144145
}
145146

146-
function getFromUsername(username, cbk) {
147-
if (!username) {
148-
return cbk({err: 'invalid_username'});
149-
}
150-
const usernameRe = makeRegEx(username);
151-
usersCollection.find({ username: usernameRe }, {password: 0}, function (err, users) {
147+
function findOne(criteria, options, cbk) {
148+
usersCollection.find(criteria, options || {}).limit(1).next(function (err, user) {
152149
if (err) {
153150
return cbk(err);
154151
}
155152

156-
users.nextObject(function (err, user) {
157-
if (err) {
158-
return cbk(err);
159-
}
160-
if (!user) {
161-
return cbk(new Error(ERROR_USER_NOT_FOUND));
162-
}
163-
return cbk(null, user);
164-
});
153+
if (!user) {
154+
return cbk(new Error(ERROR_USER_NOT_FOUND));
155+
}
156+
return cbk(null, user);
165157
});
166158
}
167159

168-
function getFromUsernamePassword(username, password, cbk) {
169-
const usernameRE = makeRegEx(username);
170-
171-
usersCollection.find({ username: usernameRE, password }, {password: 0}, function (err, users) {
172-
if (err) {
173-
return cbk(err, null);
174-
}
160+
function getFromUsername(username, cbk) {
161+
if (!username) {
162+
return cbk({err: 'invalid_username'});
163+
}
164+
findOne({ username: makeRegEx(username) }, {password: 0}, cbk);
165+
}
175166

176-
users.nextObject(function (nextErr, user) {
177-
if (nextErr) {
178-
return cbk(nextErr);
179-
}
180-
if (!user) {
181-
return cbk(new Error(ERROR_USER_NOT_FOUND), null);
182-
}
183-
return cbk(null, user);
184-
});
185-
});
167+
function getFromUsernamePassword(username, password, cbk) {
168+
findOne({ username: makeRegEx(username), password }, {password: 0}, cbk);
186169
}
187170

188171
function getAllUserFields(username, cbk) {
189172
if (!username) {
190173
return cbk({err: 'invalid_username'}, null);
191174
}
192-
const usernameRE = makeRegEx(username);
193-
usersCollection.find({ username: usernameRE }, function (err, users) {
194-
if (err) {
195-
return cbk(err, null);
196-
}
197-
198-
users.nextObject(function (nextErr, user) {
199-
if (nextErr) {
200-
return cbk(nextErr);
201-
}
202-
if (!user) {
203-
return cbk(new Error(ERROR_USER_NOT_FOUND), null);
204-
}
205-
return cbk(null, user);
206-
});
207-
});
175+
findOne({ username: makeRegEx(username) }, {}, cbk);
208176
}
209177

210178
function deleteAllUsers(cbk) {
211-
usersCollection.remove({}, function (err) {
179+
usersCollection.deleteMany({}, function (err) {
212180
return cbk(err);
213181
});
214182
}
215183

216184
function getFromId(id, cbk) {
217-
usersCollection.find({_id: id}, {password: 0}, function (err, users) {
185+
findOne({_id: id}, {password: 0}, cbk);
186+
}
187+
188+
function updateFieldWithMethod(userId, method, fieldName, fieldValue, cbk){
189+
const data = {
190+
[method]: {
191+
[fieldName]: fieldValue
192+
}
193+
};
194+
195+
usersCollection.findOneAndUpdate({ _id: userId }, data, { returnOriginal: false, projection: { password: 0 }}, function (err, updatedProfiles) {
218196
if (err) {
219197
return cbk(err, null);
220198
}
221-
222-
users.nextObject(function (err, user) {
223-
if (err) {
224-
return cbk(err);
225-
}
226-
if (!user) {
227-
return cbk(new Error(ERROR_USER_NOT_FOUND), null);
228-
}
229-
if (user._id === id) {
230-
return cbk(null, user);
231-
}
232-
});
199+
return cbk(null, updatedProfiles);
233200
});
234201
}
235202

203+
function removeFromArrayFieldById(userId, fieldName, fieldValue, cbk) {
204+
updateFieldWithMethod(userId, '$pull', fieldName, fieldValue, cbk);
205+
}
206+
236207
function addToArrayFieldById(userId, fieldName, fieldValue, cbk) {
237-
const _id = new ObjectID(userId);
238208

239209
const data = {
240210
$push: {
@@ -243,32 +213,22 @@ function addToArrayFieldById(userId, fieldName, fieldValue, cbk) {
243213
}
244214
}
245215
};
246-
usersCollection.update({ _id }, data, function (err, updatedProfiles) {
216+
usersCollection.findOneAndUpdate({ _id: userId }, data, { returnOriginal: false, projection: { password: 0 }}, function (err) {
247217
if (err) {
248218
return cbk(err, null);
249219
}
250-
return cbk(null, updatedProfiles);
220+
return cbk(null, 1);
251221
});
252222
}
253223

254224
function updateField(userId, fieldName, fieldValue, cbk) {
255-
const data = {
256-
$set: {
257-
[fieldName]: fieldValue
258-
}
259-
};
260-
261-
usersCollection.update({_id: userId}, data, function (err, updatedUsers) {
262-
if (err) {
263-
return cbk(err, null);
264-
}
265-
return cbk(null, updatedUsers);
266-
});
225+
updateFieldWithMethod(userId, '$set', fieldName, fieldValue, cbk);
267226
}
268227

269228
function updateArrayItem(userId, arrayName, itemKey, itemValue, cbk) {
229+
const _id = userId;
270230
const query = {
271-
_id: userId,
231+
_id,
272232
[`${arrayName}.${itemKey}`]: itemValue[itemKey]
273233
};
274234

@@ -279,38 +239,55 @@ function updateArrayItem(userId, arrayName, itemKey, itemValue, cbk) {
279239
};
280240

281241
// first tries to update array item if already exists
282-
usersCollection.update(query, update, function (err, updatedUsers) {
242+
usersCollection.updateOne(query, update, function (err, updateResult) {
283243
if (err) {
284244
return cbk(err, null);
285245
}
286246

287-
if (updatedUsers === 0) {
247+
if (updateResult.modifiedCount === 0) {
288248
const update = {
289249
$push: {
290250
[arrayName]: itemValue
291251
}
292252
};
293253

294-
usersCollection.update({_id: userId}, update, function (err, updatedUsers) {
254+
usersCollection.updateOne({ _id }, update, function (err, updateResult) {
295255
if (err) {
296256
return cbk(err, null);
297257
}
298-
return cbk(null, updatedUsers);
258+
return cbk(null, updateResult.modifiedCount);
299259
});
300260
return;
301261
}
302262

303-
return cbk(null, updatedUsers);
263+
return cbk(null, updateResult.modifiedCount);
304264
});
305265
}
306266

307267
function addRealm(realmToAdd, cbk) {
308-
realmsCollection.insert(realmToAdd, function (err, result) {
268+
realmsCollection.insertOne(realmToAdd, function (err, result) {
309269
if (err) {
310270
return cbk(err, null);
311271
}
312272

313-
return cbk(null, result[0]);
273+
return cbk(null, result);
274+
});
275+
}
276+
277+
function getRealmFromName(name, cbk) {
278+
if (!name) {
279+
return cbk({err: 'invalid_realm_name', code: 400});
280+
}
281+
const nameRe = makeRegEx(name);
282+
realmsCollection.find({name: nameRe}, {_id: 0}).limit(1).next(function (err, realm) {
283+
if (err) {
284+
return cbk(err);
285+
}
286+
287+
if (!realm) {
288+
return cbk({err: 'realm_not_found', code: 400});
289+
}
290+
return cbk(null, realm);
314291
});
315292
}
316293

@@ -339,7 +316,7 @@ function resetRealmsVariables() {
339316
}
340317

341318
function deleteAllRealms(cbk) {
342-
realmsCollection.remove({}, function (err) {
319+
realmsCollection.deleteMany({}, function (err) {
343320
return cbk(err);
344321
});
345322
}
@@ -348,7 +325,7 @@ function getStatus(cbk) {
348325
if (!db || !usersCollection) {
349326
return cbk(MONGO_ERR);
350327
}
351-
usersCollection.count(function (err) {
328+
usersCollection.count({}, function (err) {
352329
if (err) {
353330
return cbk(MONGO_ERR);
354331
}
@@ -367,15 +344,18 @@ module.exports = {
367344
getFromId,
368345

369346
updateField,
347+
updateFieldWithMethod,
370348
updateArrayItem,
371349
addToArrayFieldById,
350+
removeFromArrayFieldById,
372351
getAllUserFields,
373352

374353
ERROR_USER_NOT_FOUND,
375354
ERROR_USERNAME_ALREADY_EXISTS,
376355

377356
addRealm,
378357
getRealms,
358+
getRealmFromName,
379359
resetRealmsVariables,
380360
deleteAllRealms,
381361
findByEmail,

0 commit comments

Comments
 (0)