Просмотр исходного кода

Resend email if the user has already been invited

Alberto Fernández Capel 8 лет назад
Родитель
Сommit
553878064b

+ 12 - 17
services/web/app/coffee/Features/Subscription/TeamInvitesHandler.coffee

@@ -80,25 +80,26 @@ createInvite = (subscription, email, inviterName, callback) ->
 		return callback(error) if error?
 		return callback(reason) unless possible
 
-		token = crypto.randomBytes(32).toString("hex")
-
 		# TODO: use standard way to canonalise email addresses
-		invite = {
-			email: email.trim().toLowerCase(),
-			token: token,
-			inviterName: inviterName,
-			sentAt: new Date(),
-		}
+		email = email.trim().toLowerCase()
+
+		invite = subscription.teamInvites.find (invite) -> invite.email == email
+
+		if !invite?
+			invite ||= { email: email }
+			subscription.teamInvites.push(invite)
 
-		subscription.teamInvites.push(invite)
+		invite.inviterName = inviterName
+		invite.token  = crypto.randomBytes(32).toString("hex")
+		invite.sentAt = new Date()
 
 		subscription.save (error) ->
 			return callback(error) if error?
 
 			opts =
-				to: email.trim().toLowerCase()
+				to: email
 				inviterName: inviterName
-				acceptInviteUrl: "#{settings.siteUrl}/subscription/invites/#{token}/"
+				acceptInviteUrl: "#{settings.siteUrl}/subscription/invites/#{invite.token}/"
 			EmailHandler.sendEmail "verifyEmailToJoinTeam", opts, (error) ->
 				return callback(error, invite)
 
@@ -113,12 +114,6 @@ checkIfInviteIsPossible = (subscription, email, callback = (error, possible, rea
 		logger.log {subscriptionId: subscription.id}, "team has reached member limit"
 		return callback(null, false, limitReached: true)
 
-	existingInvite = subscription.teamInvites.find (invite) -> invite.email == email
-
-	if existingInvite
-		logger.log {subscriptionId: subscription.id, email}, "user already invited"
-		return callback(null, false, alreadyInvited: true)
-
 	async.map subscription.member_ids, UserGetter.getUser, (error, members) ->
 		return callback(error) if error?
 

+ 18 - 6
services/web/test/unit/coffee/Subscription/TeamInvitesHandlerTests.coffee

@@ -116,6 +116,23 @@ describe "TeamInvitesHandler", ->
 				).should.equal true
 				done()
 
+		it "refreshes the existing invite if the email has already been invited", (done) ->
+			originalInvite = Object.assign({}, @teamInvite)
+
+			@TeamInvitesHandler.createManagerInvite @manager.id, originalInvite.email, (err, invite) =>
+				expect(err).to.eq(null)
+				expect(invite).to.exist
+
+				expect(@subscription.teamInvites.length).to.eq 1
+				expect(@subscription.teamInvites).to.deep.include invite
+
+				expect(invite.email).to.eq originalInvite.email
+				expect(invite.token).not.to.eq originalInvite.token
+
+				@subscription.save.calledOnce.should.eq true
+
+				done()
+
 	describe "createDomainInvite", ->
 		beforeEach ->
 			@licence =
@@ -202,6 +219,7 @@ describe "TeamInvitesHandler", ->
 				@TeamInvitesHandler.createManagerInvite.callCount.should.eq 1
 
 				done()
+
 	describe "validation", ->
 		it "doesn't create an invite if the team limit has been reached", (done) ->
 			@LimitationsManager.teamHasReachedMemberLimit = sinon.stub().returns(true)
@@ -209,12 +227,6 @@ describe "TeamInvitesHandler", ->
 				expect(err).to.deep.equal(limitReached: true)
 				done()
 
-		it "doen't create an invite if the email has already been invited",(done) ->
-			@TeamInvitesHandler.createManagerInvite @manager.id, "jorah@mormont.org", (err, invite) =>
-				expect(err).to.deep.equal(alreadyInvited: true)
-				expect(invite).not.to.exist
-				done()
-
 		it "doen't create an invite if the user is already part of the team", (done) ->
 			member = {
 				id: "1a2b",