overleaf/services/web/app/coffee/Features/User/UserUpdater.coffee

41 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-05-19 06:50:32 -04:00
logger = require("logger-sharelatex")
2014-02-12 05:23:40 -05:00
mongojs = require("../../infrastructure/mongojs")
db = mongojs.db
ObjectId = mongojs.ObjectId
UserLocator = require("./UserLocator")
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
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"
UserLocator.findByEmail newEmail, (error, user) ->
if user?
return callback({message:"User with that email already exists."})
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"
return callback(err)
callback()
2014-06-11 07:46:28 -04:00
updatePersonalInfo: (user_id, info, callback)->
self = @
update =
$set:
"first_name": info.first_name || ""
"last_name": info.last_name || ""
"role": info.role || ""
"institution": info.institution || ""
2014-06-11 07:46:28 -04:00
self.updateUser user_id.toString(), update, (err)->
callback(err)