mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #1077 from sharelatex/ta-create-institution
Create Institution When Confirming New Domain GitOrigin-RevId: c0f5f0edde558eed744e813d6edccfc1f5acf8f0
This commit is contained in:
parent
92582fdc38
commit
c32a891cc8
2 changed files with 45 additions and 17 deletions
|
@ -1,24 +1,37 @@
|
|||
logger = require("logger-sharelatex")
|
||||
UserGetter = require("../User/UserGetter")
|
||||
{ addAffiliation } = require("../Institutions/InstitutionsAPI")
|
||||
Institution = require('../../models/Institution').Institution
|
||||
async = require('async')
|
||||
|
||||
module.exports = InstitutionsController =
|
||||
confirmDomain: (req, res, next) ->
|
||||
hostname = req.body.hostname
|
||||
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 next(error)
|
||||
async.map users, ((user) ->
|
||||
matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname
|
||||
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) ->
|
||||
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)
|
||||
async.map users, ((user) ->
|
||||
matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname
|
||||
for email in matchingEmails
|
||||
addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) =>
|
||||
if error?
|
||||
return next(error)
|
||||
res.sendStatus 200
|
||||
logger.err error: error, 'problem adding affiliation while confirming hostname'
|
||||
return callback(error)
|
||||
), (error) ->
|
||||
if error?
|
||||
return callback(error)
|
||||
callback()
|
||||
|
|
|
@ -36,9 +36,11 @@ describe "InstitutionsController", ->
|
|||
getUsersByHostname: @getUsersByHostname
|
||||
'../Institutions/InstitutionsAPI':
|
||||
addAffiliation: @addAffiliation
|
||||
'../../models/Institution': Institution: @Institution =
|
||||
findOneAndUpdate: sinon.stub().yields()
|
||||
|
||||
@req =
|
||||
body:{}
|
||||
body: hostname: 'mit.edu'
|
||||
|
||||
@res =
|
||||
send: sinon.stub()
|
||||
|
@ -47,7 +49,6 @@ describe "InstitutionsController", ->
|
|||
|
||||
describe 'confirmDomain', ->
|
||||
it 'should add affiliations for matching users', (done)->
|
||||
@req.body.hostname = "mit.edu"
|
||||
@res.sendStatus = (code) =>
|
||||
@getUsersByHostname.calledOnce.should.equal true
|
||||
@addAffiliation.calledThrice.should.equal true
|
||||
|
@ -55,3 +56,17 @@ describe "InstitutionsController", ->
|
|||
@addAffiliation.calledWith(@stubbedUser1._id, @stubbedUser1.emails[2].email).should.equal true
|
||||
done()
|
||||
@InstitutionsController.confirmDomain @req, @res, @next
|
||||
|
||||
describe 'create institution', ->
|
||||
it 'should create new institution', (done)->
|
||||
@req.body.institution_id = 123
|
||||
expectedData = v1Id: 123
|
||||
@res.sendStatus = (code) =>
|
||||
sinon.assert.calledWith(
|
||||
@Institution.findOneAndUpdate,
|
||||
expectedData,
|
||||
expectedData,
|
||||
{ upsert: true }
|
||||
)
|
||||
done()
|
||||
@InstitutionsController.confirmDomain @req, @res, @next
|
||||
|
|
Loading…
Reference in a new issue