Add method to import invites

This commit is contained in:
Alberto Fernández Capel 2018-06-18 14:18:23 +01:00
parent 55112dc7dc
commit 81c102b501
2 changed files with 30 additions and 1 deletions

View file

@ -56,6 +56,20 @@ module.exports = TeamInvitesHandler =
return callback(error) if error? return callback(error) if error?
createInvite(subscription, email, inviterName, callback) createInvite(subscription, email, inviterName, callback)
importInvite: (subscription, inviterName, email, token, sentAt, callback) ->
checkIfInviteIsPossible subscription, email, (error, possible, reason) ->
return callback(error) if error?
return callback(reason) unless possible
subscription.teamInvites.push({
email: email
inviterName: inviterName
token: token
sentAt: sentAt
})
subscription.save callback
acceptInvite: (token, userId, callback) -> acceptInvite: (token, userId, callback) ->
logger.log {userId}, "Accepting invite" logger.log {userId}, "Accepting invite"
TeamInvitesHandler.getInvite token, (err, invite, subscription) -> TeamInvitesHandler.getInvite token, (err, invite, subscription) ->
@ -93,7 +107,6 @@ createInvite = (subscription, email, inviterName, callback) ->
return callback(error) if error? return callback(error) if error?
return callback(reason) unless possible return callback(reason) unless possible
invite = subscription.teamInvites.find (invite) -> invite.email == email invite = subscription.teamInvites.find (invite) -> invite.email == email
if !invite? if !invite?

View file

@ -175,6 +175,22 @@ describe "TeamInvitesHandler", ->
).should.equal true ).should.equal true
done() done()
describe "importInvite", ->
beforeEach ->
@sentAt = new Date()
it "can imports an invite from v1", ->
@TeamInvitesHandler.importInvite @subscription, "A-Team", "hannibal@a-team.org",
"secret", @sentAt, (error) =>
expect(error).not.to.exist
@subscription.save.calledOnce.should.eq true
invite = @subscription.teamInvites.find (i) -> i.email == "hannibal@a-team.org"
expect(invite.token).to.eq("secret")
expect(invite.sentAt).to.eq(@sentAt)
describe "acceptInvite", -> describe "acceptInvite", ->
beforeEach -> beforeEach ->
@user = { @user = {