2018-10-07 11:40:26 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2018-10-06 12:22:39 -04:00
|
|
|
UserGetter = require("../User/UserGetter")
|
2018-10-07 11:40:26 -04:00
|
|
|
{ addAffiliation } = require("../Institutions/InstitutionsAPI")
|
|
|
|
async = require('async')
|
2018-10-06 12:22:39 -04:00
|
|
|
|
2018-10-06 10:57:25 -04:00
|
|
|
module.exports = InstitutionsController =
|
2018-10-06 12:22:39 -04:00
|
|
|
confirmDomain: (req, res, next) ->
|
2018-10-08 08:37:12 -04:00
|
|
|
hostname = req.body.hostname
|
|
|
|
reversedHostname = hostname.trim().split('').reverse().join('')
|
2018-10-06 12:22:39 -04:00
|
|
|
UserGetter.getUsersByHostname hostname, {_id:1, emails:1}, (error, users) ->
|
2018-10-08 07:08:29 -04:00
|
|
|
if error?
|
|
|
|
logger.err error: error, 'problem fetching users by hostname'
|
|
|
|
return next(error)
|
|
|
|
async.map users, ((user) ->
|
2018-10-08 08:37:12 -04:00
|
|
|
matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname
|
2018-10-08 07:08:29 -04:00
|
|
|
for email in matchingEmails
|
|
|
|
addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) =>
|
|
|
|
if error?
|
|
|
|
logger.err error: error, 'problem adding affiliation while confirming hostname'
|
|
|
|
return next(error)
|
|
|
|
), (error) ->
|
2018-10-07 11:40:26 -04:00
|
|
|
if error?
|
|
|
|
return next(error)
|
|
|
|
res.sendStatus 200
|