From 81c102b5010944ca11f4ff2ae00f11afe1d71d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Fern=C3=A1ndez=20Capel?= Date: Mon, 18 Jun 2018 14:18:23 +0100 Subject: [PATCH] Add method to import invites --- .../Subscription/TeamInvitesHandler.coffee | 15 ++++++++++++++- .../Subscription/TeamInvitesHandlerTests.coffee | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/services/web/app/coffee/Features/Subscription/TeamInvitesHandler.coffee b/services/web/app/coffee/Features/Subscription/TeamInvitesHandler.coffee index e09e99c94a..c3b3b11089 100644 --- a/services/web/app/coffee/Features/Subscription/TeamInvitesHandler.coffee +++ b/services/web/app/coffee/Features/Subscription/TeamInvitesHandler.coffee @@ -56,6 +56,20 @@ module.exports = TeamInvitesHandler = return callback(error) if error? 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) -> logger.log {userId}, "Accepting invite" TeamInvitesHandler.getInvite token, (err, invite, subscription) -> @@ -93,7 +107,6 @@ createInvite = (subscription, email, inviterName, callback) -> return callback(error) if error? return callback(reason) unless possible - invite = subscription.teamInvites.find (invite) -> invite.email == email if !invite? diff --git a/services/web/test/unit/coffee/Subscription/TeamInvitesHandlerTests.coffee b/services/web/test/unit/coffee/Subscription/TeamInvitesHandlerTests.coffee index 527ba8a143..d23d1fc3a6 100644 --- a/services/web/test/unit/coffee/Subscription/TeamInvitesHandlerTests.coffee +++ b/services/web/test/unit/coffee/Subscription/TeamInvitesHandlerTests.coffee @@ -175,6 +175,22 @@ describe "TeamInvitesHandler", -> ).should.equal true 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", -> beforeEach -> @user = {