2014-05-19 06:50:32 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2014-02-12 05:23:40 -05:00
|
|
|
mongojs = require("../../infrastructure/mongojs")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require("metrics-sharelatex")
|
2014-02-12 05:23:40 -05:00
|
|
|
db = mongojs.db
|
|
|
|
ObjectId = mongojs.ObjectId
|
2018-05-23 10:12:23 -04:00
|
|
|
UserGetter = require("./UserGetter")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = UserUpdater =
|
|
|
|
updateUser: (query, update, callback = (error) ->) ->
|
|
|
|
if typeof query == "string"
|
|
|
|
query = _id: ObjectId(query)
|
|
|
|
else if query instanceof ObjectId
|
|
|
|
query = _id: query
|
|
|
|
|
|
|
|
db.users.update query, update, callback
|
2014-05-16 12:29:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
changeEmailAddress: (user_id, newEmail, callback)->
|
|
|
|
self = @
|
2014-05-19 06:50:32 -04:00
|
|
|
logger.log user_id:user_id, newEmail:newEmail, "updaing email address of user"
|
2018-05-23 10:12:23 -04:00
|
|
|
UserGetter.getUserByMainEmail newEmail, (error, user) ->
|
2014-05-16 12:29:54 -04:00
|
|
|
if user?
|
2014-08-01 09:03:38 -04:00
|
|
|
return callback({message:"alread_exists"})
|
2014-05-16 12:29:54 -04:00
|
|
|
self.updateUser user_id.toString(), {
|
|
|
|
$set: { "email": newEmail},
|
|
|
|
}, (err) ->
|
|
|
|
if err?
|
2014-05-19 06:50:32 -04:00
|
|
|
logger.err err:err, "problem updating users email"
|
2014-05-16 12:29:54 -04:00
|
|
|
return callback(err)
|
|
|
|
callback()
|
|
|
|
|
2017-03-16 06:59:18 -04:00
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
metrics.timeAsyncMethod UserUpdater, 'updateUser', 'mongo.UserUpdater', logger
|