UserCreator.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const logger = require('logger-sharelatex')
  2. const util = require('util')
  3. const { AffiliationError } = require('../Errors/Errors')
  4. const Features = require('../../infrastructure/Features')
  5. const { User } = require('../../models/User')
  6. const UserDeleter = require('./UserDeleter')
  7. const UserGetter = require('./UserGetter')
  8. const UserUpdater = require('./UserUpdater')
  9. const Analytics = require('../Analytics/AnalyticsManager')
  10. const UserOnboardingEmailQueueManager = require('./UserOnboardingEmailManager')
  11. const OError = require('@overleaf/o-error')
  12. async function _addAffiliation(user, affiliationOptions) {
  13. try {
  14. await UserUpdater.promises.addAffiliationForNewUser(
  15. user._id,
  16. user.email,
  17. affiliationOptions
  18. )
  19. } catch (error) {
  20. throw new AffiliationError('add affiliation failed').withCause(error)
  21. }
  22. try {
  23. user = await UserGetter.promises.getUser(user._id)
  24. } catch (error) {
  25. logger.error(
  26. OError.tag(error, 'could not get fresh user data', {
  27. userId: user._id,
  28. email: user.email,
  29. })
  30. )
  31. }
  32. return user
  33. }
  34. async function createNewUser(attributes, options = {}) {
  35. let user = new User()
  36. if (attributes.first_name == null || attributes.first_name === '') {
  37. attributes.first_name = attributes.email.split('@')[0]
  38. }
  39. Object.assign(user, attributes)
  40. user.ace.syntaxValidation = true
  41. if (user.featureSwitches != null) {
  42. user.featureSwitches.pdfng = true
  43. }
  44. const reversedHostname = user.email.split('@')[1].split('').reverse().join('')
  45. const emailData = {
  46. email: user.email,
  47. createdAt: new Date(),
  48. reversedHostname,
  49. }
  50. if (Features.hasFeature('affiliations')) {
  51. emailData.affiliationUnchecked = true
  52. }
  53. if (
  54. attributes.samlIdentifiers &&
  55. attributes.samlIdentifiers[0] &&
  56. attributes.samlIdentifiers[0].providerId
  57. ) {
  58. emailData.samlProviderId = attributes.samlIdentifiers[0].providerId
  59. }
  60. user.emails = [emailData]
  61. user = await user.save()
  62. if (Features.hasFeature('affiliations')) {
  63. try {
  64. user = await _addAffiliation(user, options.affiliationOptions || {})
  65. } catch (error) {
  66. if (options.requireAffiliation) {
  67. await UserDeleter.promises.deleteMongoUser(user._id)
  68. throw OError.tag(error)
  69. } else {
  70. logger.error(OError.tag(error))
  71. }
  72. }
  73. }
  74. Analytics.recordEvent(user._id, 'user-registered')
  75. Analytics.setUserProperty(user._id, 'created-at', new Date())
  76. if (Features.hasFeature('saas')) {
  77. try {
  78. await UserOnboardingEmailQueueManager.scheduleOnboardingEmail(user)
  79. } catch (error) {
  80. logger.error(
  81. OError.tag(error, 'Failed to schedule sending of onboarding email', {
  82. userId: user._id,
  83. })
  84. )
  85. }
  86. }
  87. return user
  88. }
  89. const UserCreator = {
  90. createNewUser: util.callbackify(createNewUser),
  91. promises: {
  92. createNewUser: createNewUser,
  93. },
  94. }
  95. module.exports = UserCreator