@@ -144,7 +144,7 @@ function findByEmail(email, callback) {
144
144
} ) ;
145
145
}
146
146
147
- function findOne ( criteria , options , cbk ) {
147
+ function findOneUser ( criteria , options , cbk ) {
148
148
usersCollection . find ( criteria , options || { } ) . limit ( 1 ) . next ( function ( err , user ) {
149
149
if ( err ) {
150
150
return cbk ( err ) ;
@@ -157,22 +157,32 @@ function findOne(criteria, options, cbk) {
157
157
} ) ;
158
158
}
159
159
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
+
160
170
function getFromUsername ( username , cbk ) {
161
171
if ( ! username ) {
162
172
return cbk ( { err : 'invalid_username' } ) ;
163
173
}
164
- findOne ( { username : makeRegEx ( username ) } , { password : 0 } , cbk ) ;
174
+ findOneUser ( { username : makeRegEx ( username ) } , { password : 0 } , cbk ) ;
165
175
}
166
176
167
177
function getFromUsernamePassword ( username , password , cbk ) {
168
- findOne ( { username : makeRegEx ( username ) , password } , { password : 0 } , cbk ) ;
178
+ findOneUser ( { username : makeRegEx ( username ) , password } , { password : 0 } , cbk ) ;
169
179
}
170
180
171
181
function getAllUserFields ( username , cbk ) {
172
182
if ( ! username ) {
173
183
return cbk ( { err : 'invalid_username' } , null ) ;
174
184
}
175
- findOne ( { username : makeRegEx ( username ) } , { } , cbk ) ;
185
+ findOneUser ( { username : makeRegEx ( username ) } , { } , cbk ) ;
176
186
}
177
187
178
188
function deleteAllUsers ( cbk ) {
@@ -182,53 +192,38 @@ function deleteAllUsers(cbk) {
182
192
}
183
193
184
194
function getFromId ( id , cbk ) {
185
- findOne ( { _id : id } , { password : 0 } , cbk ) ;
195
+ findOneUser ( { _id : id } , { password : 0 } , cbk ) ;
186
196
}
187
197
188
198
function updateFieldWithMethod ( userId , method , fieldName , fieldValue , cbk ) {
189
- const data = {
199
+ updateOne ( usersCollection , { _id : userId } , {
190
200
[ method ] : {
191
201
[ fieldName ] : fieldValue
192
202
}
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 ) ;
201
204
}
202
205
203
206
function removeFromArrayFieldById ( userId , fieldName , fieldValue , cbk ) {
204
207
updateFieldWithMethod ( userId , '$pull' , fieldName , fieldValue , cbk ) ;
205
208
}
206
209
207
210
function addToArrayFieldById ( userId , fieldName , fieldValue , cbk ) {
208
-
209
- const data = {
211
+ updateOne ( usersCollection , { _id : userId } , {
210
212
$push : {
211
213
[ fieldName ] : {
212
214
$each : [ fieldValue ]
213
215
}
214
216
}
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 ) ;
222
218
}
223
219
224
220
function updateField ( userId , fieldName , fieldValue , cbk ) {
225
221
updateFieldWithMethod ( userId , '$set' , fieldName , fieldValue , cbk ) ;
226
222
}
227
223
228
224
function updateArrayItem ( userId , arrayName , itemKey , itemValue , cbk ) {
229
- const _id = userId ;
230
225
const query = {
231
- _id,
226
+ _id : userId ,
232
227
[ `${ arrayName } .${ itemKey } ` ] : itemValue [ itemKey ]
233
228
} ;
234
229
@@ -244,23 +239,14 @@ function updateArrayItem(userId, arrayName, itemKey, itemValue, cbk) {
244
239
return cbk ( err , null ) ;
245
240
}
246
241
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 ) ;
261
244
}
262
-
263
- return cbk ( null , updateResult . modifiedCount ) ;
245
+ updateOne ( usersCollection , { _id : userId } , {
246
+ $push : {
247
+ [ arrayName ] : itemValue
248
+ }
249
+ } , cbk ) ;
264
250
} ) ;
265
251
}
266
252
0 commit comments