2020-08-19 05:11:32 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const UserGetter = require('../User/UserGetter')
|
|
|
|
const { addAffiliation } = require('../Institutions/InstitutionsAPI')
|
|
|
|
const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
|
|
|
|
const async = require('async')
|
2019-06-11 09:15:04 -04:00
|
|
|
const ASYNC_AFFILIATIONS_LIMIT = 10
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-01 04:37:56 -04:00
|
|
|
module.exports = {
|
2019-05-29 05:21:06 -04:00
|
|
|
confirmDomain(req, res, next) {
|
|
|
|
const { hostname } = req.body
|
2020-06-01 04:37:56 -04:00
|
|
|
affiliateUsers(hostname, function(error) {
|
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2020-06-01 04:37:56 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var affiliateUsers = function(hostname, callback) {
|
|
|
|
const reversedHostname = hostname
|
|
|
|
.trim()
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
|
|
|
.join('')
|
2020-06-01 04:37:56 -04:00
|
|
|
UserGetter.getUsersByHostname(hostname, { _id: 1, emails: 1 }, function(
|
|
|
|
error,
|
|
|
|
users
|
|
|
|
) {
|
|
|
|
if (error) {
|
2020-08-19 05:11:32 -04:00
|
|
|
OError.tag(error, 'problem fetching users by hostname')
|
2020-06-01 04:37:56 -04:00
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-06-01 04:37:56 -04:00
|
|
|
|
|
|
|
async.mapLimit(
|
|
|
|
users,
|
|
|
|
ASYNC_AFFILIATIONS_LIMIT,
|
|
|
|
(user, innerCallback) =>
|
|
|
|
affiliateUserByReversedHostname(user, reversedHostname, innerCallback),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var affiliateUserByReversedHostname = function(
|
|
|
|
user,
|
|
|
|
reversedHostname,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
const matchingEmails = user.emails.filter(
|
|
|
|
email => email.reversedHostname === reversedHostname
|
|
|
|
)
|
2020-06-01 04:37:56 -04:00
|
|
|
async.mapSeries(
|
2019-05-29 05:21:06 -04:00
|
|
|
matchingEmails,
|
|
|
|
(email, innerCallback) =>
|
|
|
|
addAffiliation(
|
|
|
|
user._id,
|
|
|
|
email.email,
|
|
|
|
{ confirmedAt: email.confirmedAt },
|
|
|
|
error => {
|
2020-06-01 04:37:56 -04:00
|
|
|
if (error) {
|
2020-08-19 05:11:32 -04:00
|
|
|
OError.tag(
|
|
|
|
error,
|
2019-05-29 05:21:06 -04:00
|
|
|
'problem adding affiliation while confirming hostname'
|
|
|
|
)
|
|
|
|
return innerCallback(error)
|
|
|
|
}
|
2020-06-01 04:37:56 -04:00
|
|
|
FeaturesUpdater.refreshFeatures(user._id, innerCallback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|