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")
|
2018-10-30 10:18:29 -04:00
|
|
|
Institution = require('../../models/Institution').Institution
|
2018-10-07 11:40:26 -04:00
|
|
|
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
|
2018-10-30 10:18:29 -04:00
|
|
|
institutionId = req.body.institution_id
|
|
|
|
createInstitution institutionId, (error) ->
|
|
|
|
return next(error) if error?
|
|
|
|
affiliateUsers hostname, (error) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.sendStatus 200
|
|
|
|
|
|
|
|
createInstitution = (institutionId, callback = (error)->) ->
|
|
|
|
data = v1Id: institutionId
|
|
|
|
Institution.findOneAndUpdate data, data, { upsert: true }, callback
|
|
|
|
|
|
|
|
affiliateUsers = (hostname, callback = (error)->) ->
|
|
|
|
reversedHostname = hostname.trim().split('').reverse().join('')
|
|
|
|
UserGetter.getUsersByHostname hostname, {_id:1, emails:1}, (error, users) ->
|
|
|
|
if error?
|
|
|
|
logger.err error: error, 'problem fetching users by hostname'
|
|
|
|
return callback(error)
|
2018-11-13 05:32:29 -05:00
|
|
|
|
|
|
|
async.map users, ((user, innerCallback) ->
|
|
|
|
affiliateUserByReversedHostname user, reversedHostname, innerCallback
|
|
|
|
), callback
|
|
|
|
|
|
|
|
affiliateUserByReversedHostname = (user, reversedHostname, callback) ->
|
|
|
|
matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname
|
|
|
|
async.map matchingEmails, ((email, innerCallback) ->
|
|
|
|
addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) =>
|
|
|
|
if error?
|
|
|
|
logger.err error: error, 'problem adding affiliation while confirming hostname'
|
|
|
|
return innerCallback(error)
|
|
|
|
innerCallback()
|
|
|
|
), callback
|