2021-11-01 11:05:16 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-09-18 11:22:27 -04:00
|
|
|
const util = require('util')
|
2020-09-01 08:37:09 -04:00
|
|
|
const { AffiliationError } = require('../Errors/Errors')
|
2020-02-20 11:08:40 -05:00
|
|
|
const Features = require('../../infrastructure/Features')
|
2020-09-01 08:37:09 -04:00
|
|
|
const { User } = require('../../models/User')
|
|
|
|
const UserDeleter = require('./UserDeleter')
|
2020-02-20 11:08:40 -05:00
|
|
|
const UserGetter = require('./UserGetter')
|
2020-09-01 08:37:09 -04:00
|
|
|
const UserUpdater = require('./UserUpdater')
|
2021-03-10 04:20:55 -05:00
|
|
|
const Analytics = require('../Analytics/AnalyticsManager')
|
2021-08-17 09:08:54 -04:00
|
|
|
const UserOnboardingEmailManager = require('./UserOnboardingEmailManager')
|
2021-06-10 04:13:06 -04:00
|
|
|
const UserPostRegistrationAnalyticsManager = require('./UserPostRegistrationAnalyticsManager')
|
2021-05-25 09:24:41 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2020-02-20 11:08:40 -05:00
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
async function _addAffiliation(user, affiliationOptions) {
|
2020-02-20 11:08:40 -05:00
|
|
|
try {
|
2020-09-01 08:37:09 -04:00
|
|
|
await UserUpdater.promises.addAffiliationForNewUser(
|
|
|
|
user._id,
|
|
|
|
user.email,
|
|
|
|
affiliationOptions
|
|
|
|
)
|
2020-02-20 11:08:40 -05:00
|
|
|
} catch (error) {
|
2020-09-01 08:37:09 -04:00
|
|
|
throw new AffiliationError('add affiliation failed').withCause(error)
|
2020-02-20 11:08:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
user = await UserGetter.promises.getUser(user._id)
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
2021-05-25 09:24:41 -04:00
|
|
|
OError.tag(error, 'could not get fresh user data', {
|
|
|
|
userId: user._id,
|
|
|
|
email: user.email,
|
|
|
|
})
|
2020-02-20 11:08:40 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-09-01 08:37:09 -04:00
|
|
|
async function createNewUser(attributes, options = {}) {
|
2019-09-18 11:22:27 -04:00
|
|
|
let user = new User()
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
if (attributes.first_name == null || attributes.first_name === '') {
|
|
|
|
attributes.first_name = attributes.email.split('@')[0]
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
Object.assign(user, attributes)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
user.ace.syntaxValidation = true
|
|
|
|
if (user.featureSwitches != null) {
|
|
|
|
user.featureSwitches.pdfng = true
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const reversedHostname = user.email.split('@')[1].split('').reverse().join('')
|
2019-09-18 11:22:27 -04:00
|
|
|
|
2019-10-07 11:23:45 -04:00
|
|
|
const emailData = {
|
|
|
|
email: user.email,
|
|
|
|
createdAt: new Date(),
|
2021-04-27 03:52:58 -04:00
|
|
|
reversedHostname,
|
2019-10-07 11:23:45 -04:00
|
|
|
}
|
2020-02-20 11:08:40 -05:00
|
|
|
if (Features.hasFeature('affiliations')) {
|
|
|
|
emailData.affiliationUnchecked = true
|
|
|
|
}
|
2019-10-07 11:23:45 -04:00
|
|
|
if (
|
|
|
|
attributes.samlIdentifiers &&
|
|
|
|
attributes.samlIdentifiers[0] &&
|
|
|
|
attributes.samlIdentifiers[0].providerId
|
|
|
|
) {
|
|
|
|
emailData.samlProviderId = attributes.samlIdentifiers[0].providerId
|
|
|
|
}
|
|
|
|
|
|
|
|
user.emails = [emailData]
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
user = await user.save()
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-02-20 11:08:40 -05:00
|
|
|
if (Features.hasFeature('affiliations')) {
|
2020-09-01 08:37:09 -04:00
|
|
|
try {
|
|
|
|
user = await _addAffiliation(user, options.affiliationOptions || {})
|
|
|
|
} catch (error) {
|
|
|
|
if (options.requireAffiliation) {
|
|
|
|
await UserDeleter.promises.deleteMongoUser(user._id)
|
2021-05-25 09:24:41 -04:00
|
|
|
throw OError.tag(error)
|
2020-09-01 08:37:09 -04:00
|
|
|
} else {
|
2021-05-25 09:24:41 -04:00
|
|
|
logger.error(OError.tag(error))
|
2020-09-01 08:37:09 -04:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 11:08:40 -05:00
|
|
|
}
|
2019-09-18 11:22:27 -04:00
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
await Analytics.recordEventForUser(user._id, 'user-registered')
|
|
|
|
await Analytics.setUserPropertyForUser(user._id, 'created-at', new Date())
|
|
|
|
await Analytics.setUserPropertyForUser(user._id, 'user-id', user._id)
|
|
|
|
if (attributes.analyticsId) {
|
|
|
|
await Analytics.setUserPropertyForUser(
|
|
|
|
user._id,
|
|
|
|
'analytics-id',
|
|
|
|
attributes.analyticsId
|
|
|
|
)
|
|
|
|
}
|
2021-05-25 09:24:41 -04:00
|
|
|
|
|
|
|
if (Features.hasFeature('saas')) {
|
|
|
|
try {
|
2021-08-17 09:08:54 -04:00
|
|
|
await UserOnboardingEmailManager.scheduleOnboardingEmail(user)
|
2021-06-10 04:13:06 -04:00
|
|
|
await UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
|
|
|
|
user
|
|
|
|
)
|
2021-05-25 09:24:41 -04:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
OError.tag(error, 'Failed to schedule sending of onboarding email', {
|
|
|
|
userId: user._id,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2021-03-31 05:24:39 -04:00
|
|
|
}
|
2021-03-10 04:20:55 -05:00
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
const UserCreator = {
|
|
|
|
createNewUser: util.callbackify(createNewUser),
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
createNewUser: createNewUser,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:22:27 -04:00
|
|
|
module.exports = UserCreator
|