overleaf/services/web/app/coffee/Features/User/UserCreator.coffee
James Allen 3bc9ce995a Merge pull request #1264 from sharelatex/hb-set-reversedHostname-on-user-create
Set reversedHostname on user creation

GitOrigin-RevId: 35ba4a5ae6aa792ee1c46e8b2b99d2e6ed00af17
2018-12-13 09:25:58 +00:00

50 lines
1.4 KiB
CoffeeScript

User = require("../../models/User").User
logger = require("logger-sharelatex")
metrics = require('metrics-sharelatex')
{ addAffiliation } = require("../Institutions/InstitutionsAPI")
module.exports = UserCreator =
createNewUser: (attributes, options, callback = (error, user) ->)->
if arguments.length == 2
callback = options
options = {}
logger.log user: attributes, "creating new user"
user = new User()
username = attributes.email.match(/^[^@]*/)
if !attributes.first_name? or attributes.first_name == ""
attributes.first_name = username[0]
for key, value of attributes
user[key] = value
user.ace.syntaxValidation = true
user.featureSwitches?.pdfng = true
user.emails = [
email: user.email
createdAt: new Date()
reversedHostname: user.email.split('@')[1].split('').reverse().join('')
]
user.save (err)->
callback(err, user)
return if options?.skip_affiliation
# 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"
metrics.timeAsyncMethod(
UserCreator, 'createNewUser',
'mongo.UserCreator',
logger
)