UserCreator.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. User = require("../../models/User").User
  2. logger = require("logger-sharelatex")
  3. metrics = require('metrics-sharelatex')
  4. { addAffiliation } = require("../Institutions/InstitutionsAPI")
  5. module.exports = UserCreator =
  6. createNewUser: (attributes, options, callback = (error, user) ->)->
  7. if arguments.length == 2
  8. callback = options
  9. options = {}
  10. logger.log user: attributes, "creating new user"
  11. user = new User()
  12. username = attributes.email.match(/^[^@]*/)
  13. if !attributes.first_name? or attributes.first_name == ""
  14. attributes.first_name = username[0]
  15. for key, value of attributes
  16. user[key] = value
  17. user.ace.syntaxValidation = true
  18. user.featureSwitches?.pdfng = true
  19. user.emails = [
  20. email: user.email
  21. createdAt: new Date()
  22. reversedHostname: user.email.split('@')[1].split('').reverse().join('')
  23. ]
  24. user.save (err)->
  25. callback(err, user)
  26. return if options?.skip_affiliation
  27. # call addaffiliation after the main callback so it runs in the
  28. # background. There is no guaranty this will run so we must no rely on it
  29. addAffiliation user._id, user.email, (error) ->
  30. if error
  31. logger.log { userId: user._id, email: user.email, error: error },
  32. "couldn't add affiliation for user on create"
  33. else
  34. logger.log { userId: user._id, email: user.email },
  35. "added affiliation for user on create"
  36. metrics.timeAsyncMethod(
  37. UserCreator, 'createNewUser',
  38. 'mongo.UserCreator',
  39. logger
  40. )