From a8a39d86b472daf9acac8bc290fb6016d80ed6ef Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Tue, 27 Feb 2024 18:24:20 +0800 Subject: [PATCH] UX: Improve invite error message when a user uses an email that has already redeemed (#25695) Improve invite error message when a user uses an email that has already redeemed --- app/models/invite_redeemer.rb | 2 +- spec/models/invite_redeemer_spec.rb | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index 9c760415627..35fc22b78f4 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -192,7 +192,7 @@ class InviteRedeemer redeeming_user ||= User.where(admin: false, staged: false).find_by_email(email) if redeeming_user.present? && InvitedUser.exists?(user_id: redeeming_user.id, invite_id: invite.id) - return false + raise Invite::UserExists.new(I18n.t("invite.existing_user_already_redemeed")) end true diff --git a/spec/models/invite_redeemer_spec.rb b/spec/models/invite_redeemer_spec.rb index aadd985fc61..f4bbc920697 100644 --- a/spec/models/invite_redeemer_spec.rb +++ b/spec/models/invite_redeemer_spec.rb @@ -587,13 +587,15 @@ RSpec.describe InviteRedeemer do expect(invite_link.redemption_count).to eq(1) end - it "should not redeem the invite if InvitedUser record already exists for email" do + it "raises an error if email has already been invited" do invite_redeemer.redeem invite_link.reload another_invite_redeemer = InviteRedeemer.new(invite: invite_link, email: "foo@example.com") - another_user = another_invite_redeemer.redeem - expect(another_user).to eq(nil) + expect { another_invite_redeemer.redeem }.to raise_error( + Invite::UserExists, + I18n.t("invite.existing_user_already_redemeed"), + ) end it "should redeem the invite if InvitedUser record does not exists for email" do