Browse Source

try adding affiliation on user creation

Tim Alby 8 years ago
parent
commit
9d4df4271a

+ 6 - 1
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"

+ 12 - 0
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',

+ 11 - 1
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()