2014-05-19 10:50:32 +00:00
|
|
|
logger = require("logger-sharelatex")
|
2014-02-12 10:23:40 +00:00
|
|
|
mongojs = require("../../infrastructure/mongojs")
|
|
|
|
db = mongojs.db
|
|
|
|
ObjectId = mongojs.ObjectId
|
2014-05-16 16:29:54 +00:00
|
|
|
UserLocator = require("./UserLocator")
|
2014-02-12 10:23:40 +00: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 16:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
changeEmailAddress: (user_id, newEmail, callback)->
|
|
|
|
self = @
|
2014-05-19 10:50:32 +00:00
|
|
|
logger.log user_id:user_id, newEmail:newEmail, "updaing email address of user"
|
|
|
|
UserLocator.findByEmail newEmail, (error, user) ->
|
2014-05-16 16:29:54 +00:00
|
|
|
if user?
|
2014-08-01 13:03:38 +00:00
|
|
|
return callback({message:"alread_exists"})
|
2014-05-16 16:29:54 +00:00
|
|
|
self.updateUser user_id.toString(), {
|
|
|
|
$set: { "email": newEmail},
|
|
|
|
}, (err) ->
|
|
|
|
if err?
|
2014-05-19 10:50:32 +00:00
|
|
|
logger.err err:err, "problem updating users email"
|
2014-05-16 16:29:54 +00:00
|
|
|
return callback(err)
|
|
|
|
callback()
|
|
|
|
|