UserCreator.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const logger = require('@overleaf/logger')
  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 SplitTestHandler = require('../SplitTests/SplitTestHandler')
  11. const UserOnboardingEmailManager = require('./UserOnboardingEmailManager')
  12. const UserPostRegistrationAnalyticsManager = require('./UserPostRegistrationAnalyticsManager')
  13. const OError = require('@overleaf/o-error')
  14. async function _addAffiliation(user, affiliationOptions) {
  15. try {
  16. await UserUpdater.promises.addAffiliationForNewUser(
  17. user._id,
  18. user.email,
  19. affiliationOptions
  20. )
  21. } catch (error) {
  22. throw new AffiliationError('add affiliation failed').withCause(error)
  23. }
  24. try {
  25. user = await UserGetter.promises.getUser(user._id)
  26. } catch (error) {
  27. logger.error(
  28. OError.tag(error, 'could not get fresh user data', {
  29. userId: user._id,
  30. email: user.email,
  31. })
  32. )
  33. }
  34. return user
  35. }
  36. async function recordRegistrationEvent(user) {
  37. SplitTestHandler.promises
  38. .getAssignmentForUser(user._id, 'highlight-sso')
  39. .then(assignment => {
  40. const segmentation = {
  41. highlightSSO: assignment.variant === 'active',
  42. }
  43. if (user.thirdPartyIdentifiers && user.thirdPartyIdentifiers.length > 0) {
  44. segmentation.provider = user.thirdPartyIdentifiers[0].providerId
  45. }
  46. return Analytics.recordEventForUser(
  47. user._id,
  48. 'user-registered',
  49. segmentation
  50. )
  51. })
  52. .catch(err =>
  53. logger.warn(
  54. { err },
  55. 'there was an error recording `user-registered` event'
  56. )
  57. )
  58. }
  59. async function createNewUser(attributes, options = {}) {
  60. let user = new User()
  61. if (attributes.first_name == null || attributes.first_name === '') {
  62. attributes.first_name = attributes.email.split('@')[0]
  63. }
  64. Object.assign(user, attributes)
  65. user.ace.syntaxValidation = true
  66. if (user.featureSwitches != null) {
  67. user.featureSwitches.pdfng = true
  68. }
  69. const reversedHostname = user.email.split('@')[1].split('').reverse().join('')
  70. const emailData = {
  71. email: user.email,
  72. createdAt: new Date(),
  73. reversedHostname,
  74. }
  75. if (Features.hasFeature('affiliations')) {
  76. emailData.affiliationUnchecked = true
  77. }
  78. if (
  79. attributes.samlIdentifiers &&
  80. attributes.samlIdentifiers[0] &&
  81. attributes.samlIdentifiers[0].providerId
  82. ) {
  83. emailData.samlProviderId = attributes.samlIdentifiers[0].providerId
  84. }
  85. user.emails = [emailData]
  86. user = await user.save()
  87. if (Features.hasFeature('affiliations')) {
  88. try {
  89. user = await _addAffiliation(user, options.affiliationOptions || {})
  90. } catch (error) {
  91. if (options.requireAffiliation) {
  92. await UserDeleter.promises.deleteMongoUser(user._id)
  93. throw OError.tag(error)
  94. } else {
  95. logger.error(OError.tag(error))
  96. }
  97. }
  98. }
  99. await recordRegistrationEvent(user)
  100. await Analytics.setUserPropertyForUser(user._id, 'created-at', new Date())
  101. await Analytics.setUserPropertyForUser(user._id, 'user-id', user._id)
  102. if (attributes.analyticsId) {
  103. await Analytics.setUserPropertyForUser(
  104. user._id,
  105. 'analytics-id',
  106. attributes.analyticsId
  107. )
  108. }
  109. if (Features.hasFeature('saas')) {
  110. try {
  111. await UserOnboardingEmailManager.scheduleOnboardingEmail(user)
  112. await UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
  113. user
  114. )
  115. } catch (error) {
  116. logger.error(
  117. OError.tag(error, 'Failed to schedule sending of onboarding email', {
  118. userId: user._id,
  119. })
  120. )
  121. }
  122. }
  123. return user
  124. }
  125. const UserCreator = {
  126. createNewUser: util.callbackify(createNewUser),
  127. promises: {
  128. createNewUser,
  129. },
  130. }
  131. module.exports = UserCreator