mirror of
https://github.com/overleaf/overleaf.git
synced 2025-02-24 13:31:36 +00:00
try adding affiliation on user creation
This commit is contained in:
parent
cc837236b4
commit
9d4df4271a
3 changed files with 29 additions and 2 deletions
|
@ -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"
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue