2014-02-12 05:23:40 -05:00
|
|
|
User = require("../../models/User").User
|
2016-06-13 08:21:44 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2018-07-10 15:49:24 -04:00
|
|
|
{ addAffiliation } = require("../Institutions/InstitutionsAPI")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
|
|
|
|
module.exports = UserCreator =
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-08-17 09:08:59 -04:00
|
|
|
createNewUser: (attributes, options, callback = (error, user) ->)->
|
|
|
|
if arguments.length == 2
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
logger.log user: attributes, "creating new user"
|
2014-02-12 05:23:40 -05:00
|
|
|
user = new User()
|
2014-04-10 09:43:06 -04:00
|
|
|
|
2018-08-17 09:08:59 -04:00
|
|
|
username = attributes.email.match(/^[^@]*/)
|
|
|
|
if !attributes.first_name? or attributes.first_name == ""
|
|
|
|
attributes.first_name = username[0]
|
2014-04-10 09:43:06 -04:00
|
|
|
|
2018-08-17 09:08:59 -04:00
|
|
|
for key, value of attributes
|
2017-08-07 06:24:04 -04:00
|
|
|
user[key] = value
|
|
|
|
|
|
|
|
user.ace.syntaxValidation = true
|
2015-01-21 10:26:01 -05:00
|
|
|
user.featureSwitches?.pdfng = true
|
2018-05-28 10:08:37 -04:00
|
|
|
user.emails = [
|
|
|
|
email: user.email
|
|
|
|
createdAt: new Date()
|
2018-12-13 04:08:38 -05:00
|
|
|
reversedHostname: user.email.split('@')[1].split('').reverse().join('')
|
2018-05-28 10:08:37 -04:00
|
|
|
]
|
2014-04-10 09:43:06 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
user.save (err)->
|
|
|
|
callback(err, user)
|
2017-03-17 10:55:41 -04:00
|
|
|
|
2018-08-17 09:08:59 -04:00
|
|
|
return if options?.skip_affiliation
|
2018-07-04 09:12:54 -04:00
|
|
|
# call addaffiliation after the main callback so it runs in the
|
|
|
|
# background. There is no guaranty this will run so we must no rely on it
|
|
|
|
addAffiliation user._id, user.email, (error) ->
|
|
|
|
if error
|
|
|
|
logger.log { userId: user._id, email: user.email, error: error },
|
|
|
|
"couldn't add affiliation for user on create"
|
|
|
|
else
|
|
|
|
logger.log { userId: user._id, email: user.email },
|
|
|
|
"added affiliation for user on create"
|
|
|
|
|
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
metrics.timeAsyncMethod(
|
|
|
|
UserCreator, 'createNewUser',
|
|
|
|
'mongo.UserCreator',
|
|
|
|
logger
|
|
|
|
)
|