mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-02 05:41:33 +02:00
* Rename files to mjs * Migrate files to ESM * Fix imports * Misc. fixes: Fixup InsititutionsAPI import, ObjectId import, ... * Rename test files to mjs * Convert test files to ESM * Fix tests * Update UserMembershipErrors imports * Convert some tests: sinon -> vitest * Fixup UserMembershipHandler.test.mjs * Convert UserMembershipErrors to ESM GitOrigin-RevId: 05d34c7e112a567f9c59398740ae0830ef93d32f
146 lines
3.9 KiB
JavaScript
146 lines
3.9 KiB
JavaScript
import logger from '@overleaf/logger'
|
|
import util from 'node:util'
|
|
import { AffiliationError } from '../Errors/Errors.js'
|
|
import Features from '../../infrastructure/Features.js'
|
|
import { User } from '../../models/User.js'
|
|
import UserDeleter from './UserDeleter.js'
|
|
import UserGetter from './UserGetter.js'
|
|
import UserUpdater from './UserUpdater.js'
|
|
import Analytics from '../Analytics/AnalyticsManager.js'
|
|
import UserOnboardingEmailManager from './UserOnboardingEmailManager.mjs'
|
|
import UserPostRegistrationAnalyticsManager from './UserPostRegistrationAnalyticsManager.mjs'
|
|
import OError from '@overleaf/o-error'
|
|
|
|
async function _addAffiliation(user, affiliationOptions) {
|
|
try {
|
|
await UserUpdater.promises.addAffiliationForNewUser(
|
|
user._id,
|
|
user.email,
|
|
affiliationOptions
|
|
)
|
|
} catch (error) {
|
|
throw new AffiliationError('add affiliation failed').withCause(error)
|
|
}
|
|
|
|
try {
|
|
user = await UserGetter.promises.getUser(user._id)
|
|
} catch (error) {
|
|
logger.error(
|
|
OError.tag(error, 'could not get fresh user data', {
|
|
userId: user._id,
|
|
email: user.email,
|
|
})
|
|
)
|
|
}
|
|
return user
|
|
}
|
|
|
|
async function recordRegistrationEvent(user) {
|
|
try {
|
|
const segmentation = {
|
|
'home-registration': 'default',
|
|
}
|
|
if (user.thirdPartyIdentifiers && user.thirdPartyIdentifiers.length > 0) {
|
|
segmentation.provider = user.thirdPartyIdentifiers[0].providerId
|
|
}
|
|
Analytics.recordEventForUserInBackground(
|
|
user._id,
|
|
'user-registered',
|
|
segmentation
|
|
)
|
|
} catch (err) {
|
|
logger.warn({ err }, 'there was an error recording `user-registered` event')
|
|
}
|
|
}
|
|
|
|
async function createNewUser(attributes, options = {}) {
|
|
let user = new User()
|
|
|
|
if (attributes.first_name == null || attributes.first_name === '') {
|
|
attributes.first_name = attributes.email.split('@')[0]
|
|
}
|
|
|
|
Object.assign(user, attributes)
|
|
|
|
user.ace.syntaxValidation = true
|
|
|
|
const reversedHostname = user.email.split('@')[1].split('').reverse().join('')
|
|
|
|
const emailData = {
|
|
email: user.email,
|
|
createdAt: new Date(),
|
|
reversedHostname,
|
|
}
|
|
if (Features.hasFeature('affiliations')) {
|
|
emailData.affiliationUnchecked = true
|
|
}
|
|
if (
|
|
attributes.samlIdentifiers &&
|
|
attributes.samlIdentifiers[0] &&
|
|
attributes.samlIdentifiers[0].providerId
|
|
) {
|
|
emailData.samlProviderId = attributes.samlIdentifiers[0].providerId
|
|
}
|
|
|
|
const affiliationOptions = options.affiliationOptions || {}
|
|
|
|
if (options.confirmedAt) {
|
|
emailData.confirmedAt = options.confirmedAt
|
|
affiliationOptions.confirmedAt = options.confirmedAt
|
|
}
|
|
user.emails = [emailData]
|
|
|
|
user = await user.save()
|
|
|
|
if (Features.hasFeature('affiliations')) {
|
|
try {
|
|
user = await _addAffiliation(user, affiliationOptions)
|
|
} catch (error) {
|
|
if (options.requireAffiliation) {
|
|
await UserDeleter.promises.deleteMongoUser(user._id)
|
|
throw OError.tag(error)
|
|
} else {
|
|
const err = OError.tag(error, 'adding affiliations failed')
|
|
logger.error({ err, userId: user._id }, err.message)
|
|
}
|
|
}
|
|
}
|
|
|
|
await recordRegistrationEvent(user)
|
|
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
|
|
)
|
|
}
|
|
|
|
if (Features.hasFeature('saas')) {
|
|
try {
|
|
await UserOnboardingEmailManager.scheduleOnboardingEmail(user)
|
|
await UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
|
|
user
|
|
)
|
|
} catch (error) {
|
|
logger.error(
|
|
OError.tag(error, 'Failed to schedule sending of onboarding email', {
|
|
userId: user._id,
|
|
})
|
|
)
|
|
}
|
|
}
|
|
|
|
return user
|
|
}
|
|
|
|
const UserCreator = {
|
|
createNewUser: util.callbackify(createNewUser),
|
|
promises: {
|
|
createNewUser,
|
|
},
|
|
}
|
|
|
|
export default UserCreator
|