diff --git a/services/web/app/coffee/Features/User/UserAffiliationsManager.coffee b/services/web/app/coffee/Features/User/UserAffiliationsManager.coffee index 2a81ae6bd5..77dad7a8ef 100644 --- a/services/web/app/coffee/Features/User/UserAffiliationsManager.coffee +++ b/services/web/app/coffee/Features/User/UserAffiliationsManager.coffee @@ -12,7 +12,12 @@ module.exports = UserAffiliationsManager = }, callback - addAffiliation: (userId, email, { university, department, role }, callback = (error) ->) -> + addAffiliation: (userId, email, affiliationOptions, callback) -> + unless callback? # affiliationOptions is optional + callback = affiliationOptions + affiliationOptions = {} + + { university, department, role } = affiliationOptions makeAffiliationRequest { method: 'POST' path: "/api/v2/users/#{userId.toString()}/affiliations" diff --git a/services/web/app/coffee/Features/User/UserCreator.coffee b/services/web/app/coffee/Features/User/UserCreator.coffee index 4b4dd30ae2..b0676e09e0 100644 --- a/services/web/app/coffee/Features/User/UserCreator.coffee +++ b/services/web/app/coffee/Features/User/UserCreator.coffee @@ -1,6 +1,7 @@ User = require("../../models/User").User logger = require("logger-sharelatex") metrics = require('metrics-sharelatex') +{ addAffiliation } = require("./UserAffiliationsManager") module.exports = UserCreator = @@ -26,6 +27,17 @@ module.exports = UserCreator = user.save (err)-> callback(err, user) + # call addaffiliation after the main callback so it runs in the + # background. There is no guaranty this will run so we must no rely on it + addAffiliation user._id, user.email, (error) -> + if error + logger.log { userId: user._id, email: user.email, error: error }, + "couldn't add affiliation for user on create" + else + logger.log { userId: user._id, email: user.email }, + "added affiliation for user on create" + + metrics.timeAsyncMethod( UserCreator, 'createNewUser', 'mongo.UserCreator', diff --git a/services/web/test/unit/coffee/User/UserCreatorTests.coffee b/services/web/test/unit/coffee/User/UserCreatorTests.coffee index 3764c8a765..1945453b9e 100644 --- a/services/web/test/unit/coffee/User/UserCreatorTests.coffee +++ b/services/web/test/unit/coffee/User/UserCreatorTests.coffee @@ -17,11 +17,12 @@ describe "UserCreator", -> @UserGetter = getUserByMainEmail: sinon.stub() + @addAffiliation = sinon.stub().yields() @UserCreator = SandboxedModule.require modulePath, requires: "../../models/User": User:@UserModel - "./UserGetter":@UserGetter "logger-sharelatex":{log:->} 'metrics-sharelatex': {timeAsyncMethod: ()->} + "./UserAffiliationsManager": addAffiliation: @addAffiliation @email = "bob.oswald@gmail.com" @@ -78,3 +79,12 @@ describe "UserCreator", -> user.emails[0].email.should.equal @email user.emails[0].createdAt.should.be.a 'date' done() + + it "should add affiliation in background", (done)-> + @UserCreator.createNewUser email: @email, (err, user) => + # addaffiliation should not be called before the callback but only after + # a tick of the event loop + sinon.assert.notCalled(@addAffiliation) + process.nextTick () => + sinon.assert.calledWith(@addAffiliation, user._id, user.email) + done()