UserCreator.coffee 1.2 KB

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