From c32a891cc8b5b9019003b03ac478d89105f53634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Tue, 30 Oct 2018 16:18:29 +0200 Subject: [PATCH] Merge pull request #1077 from sharelatex/ta-create-institution Create Institution When Confirming New Domain GitOrigin-RevId: c0f5f0edde558eed744e813d6edccfc1f5acf8f0 --- .../InstitutionsController.coffee | 43 ++++++++++++------- .../InstitutionsControllerTests.coffee | 19 +++++++- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee b/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee index af8c7c5644..83ac256eac 100644 --- a/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee +++ b/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee @@ -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() diff --git a/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee b/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee index 4e269b1f6f..d8c080389c 100644 --- a/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee +++ b/services/web/test/unit/coffee/Institutions/InstitutionsControllerTests.coffee @@ -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