Skip to content

Commit 6c83c0c

Browse files
committed
extracted updateOne method
1 parent 97a07f3 commit 6c83c0c

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

src/managers/dao.js

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function findByEmail(email, callback) {
144144
});
145145
}
146146

147-
function findOne(criteria, options, cbk) {
147+
function findOneUser(criteria, options, cbk) {
148148
usersCollection.find(criteria, options || {}).limit(1).next(function (err, user) {
149149
if (err) {
150150
return cbk(err);
@@ -157,22 +157,32 @@ function findOne(criteria, options, cbk) {
157157
});
158158
}
159159

160+
function updateOne(coll, criteria, update, cbk) {
161+
162+
coll.updateOne(criteria, update, function (err, res) {
163+
if (err) {
164+
return cbk(err, null);
165+
}
166+
return cbk(null, res.modifiedCount);
167+
});
168+
}
169+
160170
function getFromUsername(username, cbk) {
161171
if (!username) {
162172
return cbk({err: 'invalid_username'});
163173
}
164-
findOne({ username: makeRegEx(username) }, {password: 0}, cbk);
174+
findOneUser({ username: makeRegEx(username) }, {password: 0}, cbk);
165175
}
166176

167177
function getFromUsernamePassword(username, password, cbk) {
168-
findOne({ username: makeRegEx(username), password }, {password: 0}, cbk);
178+
findOneUser({ username: makeRegEx(username), password }, {password: 0}, cbk);
169179
}
170180

171181
function getAllUserFields(username, cbk) {
172182
if (!username) {
173183
return cbk({err: 'invalid_username'}, null);
174184
}
175-
findOne({ username: makeRegEx(username) }, {}, cbk);
185+
findOneUser({ username: makeRegEx(username) }, {}, cbk);
176186
}
177187

178188
function deleteAllUsers(cbk) {
@@ -182,53 +192,38 @@ function deleteAllUsers(cbk) {
182192
}
183193

184194
function getFromId(id, cbk) {
185-
findOne({_id: id}, {password: 0}, cbk);
195+
findOneUser({_id: id}, {password: 0}, cbk);
186196
}
187197

188198
function updateFieldWithMethod(userId, method, fieldName, fieldValue, cbk){
189-
const data = {
199+
updateOne(usersCollection, { _id: userId }, {
190200
[method]: {
191201
[fieldName]: fieldValue
192202
}
193-
};
194-
195-
usersCollection.updateOne({ _id: userId }, data, function (err, res) {
196-
if (err) {
197-
return cbk(err, null);
198-
}
199-
return cbk(null, res.modifiedCount);
200-
});
203+
}, cbk);
201204
}
202205

203206
function removeFromArrayFieldById(userId, fieldName, fieldValue, cbk) {
204207
updateFieldWithMethod(userId, '$pull', fieldName, fieldValue, cbk);
205208
}
206209

207210
function addToArrayFieldById(userId, fieldName, fieldValue, cbk) {
208-
209-
const data = {
211+
updateOne(usersCollection, { _id: userId }, {
210212
$push: {
211213
[fieldName]: {
212214
$each: [fieldValue]
213215
}
214216
}
215-
};
216-
usersCollection.updateOne({ _id: userId }, data, function (err, res) {
217-
if (err) {
218-
return cbk(err, null);
219-
}
220-
return cbk(null, res.modifiedCount);
221-
});
217+
}, cbk);
222218
}
223219

224220
function updateField(userId, fieldName, fieldValue, cbk) {
225221
updateFieldWithMethod(userId, '$set', fieldName, fieldValue, cbk);
226222
}
227223

228224
function updateArrayItem(userId, arrayName, itemKey, itemValue, cbk) {
229-
const _id = userId;
230225
const query = {
231-
_id,
226+
_id: userId,
232227
[`${arrayName}.${itemKey}`]: itemValue[itemKey]
233228
};
234229

@@ -244,23 +239,14 @@ function updateArrayItem(userId, arrayName, itemKey, itemValue, cbk) {
244239
return cbk(err, null);
245240
}
246241

247-
if (updateResult.modifiedCount === 0) {
248-
const update = {
249-
$push: {
250-
[arrayName]: itemValue
251-
}
252-
};
253-
254-
usersCollection.updateOne({ _id }, update, function (err, updateResult) {
255-
if (err) {
256-
return cbk(err, null);
257-
}
258-
return cbk(null, updateResult.modifiedCount);
259-
});
260-
return;
242+
if (updateResult.modifiedCount !== 0) {
243+
return cbk(null, updateResult.modifiedCount);
261244
}
262-
263-
return cbk(null, updateResult.modifiedCount);
245+
updateOne(usersCollection, { _id: userId }, {
246+
$push: {
247+
[arrayName]: itemValue
248+
}
249+
}, cbk);
264250
});
265251
}
266252

0 commit comments

Comments
 (0)