UserCreator.coffee 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. ]
  23. user.save (err)->
  24. callback(err, user)
  25. return if options?.skip_affiliation
  26. # call addaffiliation after the main callback so it runs in the
  27. # background. There is no guaranty this will run so we must no rely on it
  28. addAffiliation user._id, user.email, (error) ->
  29. if error
  30. logger.log { userId: user._id, email: user.email, error: error },
  31. "couldn't add affiliation for user on create"
  32. else
  33. logger.log { userId: user._id, email: user.email },
  34. "added affiliation for user on create"
  35. metrics.timeAsyncMethod(
  36. UserCreator, 'createNewUser',
  37. 'mongo.UserCreator',
  38. logger
  39. )