mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
a22e252666
Replaced logger statements with error tagging in Controllers GitOrigin-RevId: c5231913c82f95a325f4c3ab406d89cb534835a4
73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
const OError = require('@overleaf/o-error')
|
|
const UserGetter = require('../User/UserGetter')
|
|
const { addAffiliation } = require('../Institutions/InstitutionsAPI')
|
|
const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
|
|
const async = require('async')
|
|
const ASYNC_AFFILIATIONS_LIMIT = 10
|
|
|
|
module.exports = {
|
|
confirmDomain(req, res, next) {
|
|
const { hostname } = req.body
|
|
affiliateUsers(hostname, function(error) {
|
|
if (error) {
|
|
return next(error)
|
|
}
|
|
res.sendStatus(200)
|
|
})
|
|
}
|
|
}
|
|
|
|
var affiliateUsers = function(hostname, callback) {
|
|
const reversedHostname = hostname
|
|
.trim()
|
|
.split('')
|
|
.reverse()
|
|
.join('')
|
|
UserGetter.getUsersByHostname(hostname, { _id: 1, emails: 1 }, function(
|
|
error,
|
|
users
|
|
) {
|
|
if (error) {
|
|
OError.tag(error, 'problem fetching users by hostname')
|
|
return callback(error)
|
|
}
|
|
|
|
async.mapLimit(
|
|
users,
|
|
ASYNC_AFFILIATIONS_LIMIT,
|
|
(user, innerCallback) =>
|
|
affiliateUserByReversedHostname(user, reversedHostname, innerCallback),
|
|
callback
|
|
)
|
|
})
|
|
}
|
|
|
|
var affiliateUserByReversedHostname = function(
|
|
user,
|
|
reversedHostname,
|
|
callback
|
|
) {
|
|
const matchingEmails = user.emails.filter(
|
|
email => email.reversedHostname === reversedHostname
|
|
)
|
|
async.mapSeries(
|
|
matchingEmails,
|
|
(email, innerCallback) =>
|
|
addAffiliation(
|
|
user._id,
|
|
email.email,
|
|
{ confirmedAt: email.confirmedAt },
|
|
error => {
|
|
if (error) {
|
|
OError.tag(
|
|
error,
|
|
'problem adding affiliation while confirming hostname'
|
|
)
|
|
return innerCallback(error)
|
|
}
|
|
FeaturesUpdater.refreshFeatures(user._id, innerCallback)
|
|
}
|
|
),
|
|
callback
|
|
)
|
|
}
|