From 83320e227ce1bb1419bcc33eaa11a5e5d4e5bdc2 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 5 Aug 2024 12:18:19 +0800 Subject: [PATCH] FIX: return user matching guest_email if exists --- lib/custom_wizard/action.rb | 24 +++++++++------- plugin.rb | 2 +- spec/components/custom_wizard/action_spec.rb | 29 ++++++++++++++++++-- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index d539d785..c97a07f4 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -381,7 +381,7 @@ class CustomWizard::Action group_map = group_map.flatten.compact - unless group_map.present? + if group_map.blank? log_error("invalid group map") return end @@ -415,7 +415,7 @@ class CustomWizard::Action end def route_to - return unless (url_input = action['url']).present? + return if (url_input = action['url']).blank? if url_input.is_a?(String) url = mapper.interpolate(url_input) @@ -510,7 +510,7 @@ class CustomWizard::Action user: user ).perform - return false unless output.present? + return false if output.blank? if output.is_a?(Array) output.first @@ -528,7 +528,7 @@ class CustomWizard::Action user: user, ).perform - return false unless output.present? + return false if output.blank? if output.is_a?(Array) output.flatten @@ -682,12 +682,16 @@ class CustomWizard::Action ).perform if email&.match(/@/) - User.create!( - email: email, - username: UserNameSuggester.suggest(email), - name: User.suggest_name(email), - staged: true, - ) + if user = User.find_by_email(email) + user + else + User.create!( + email: email, + username: UserNameSuggester.suggest(email), + name: User.suggest_name(email), + staged: true, + ) + end end end end diff --git a/plugin.rb b/plugin.rb index 357c959a..a4f2bea7 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.8.1 +# version: 2.8.2 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index 13f1b13f..e6185a2a 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -5,7 +5,7 @@ describe CustomWizard::Action do fab!(:user1) { Fabricate(:user, name: "Angus One", username: 'angus1', email: "angus_one@email.com", trust_level: TrustLevel[2]) } fab!(:category) { Fabricate(:category, name: 'cat1', slug: 'cat-slug') } fab!(:tag) { Fabricate(:tag, name: 'tag1') } - fab!(:group) { Fabricate(:group) } + fab!(:group) let(:wizard_template) { get_wizard_fixture("wizard") } let(:open_composer) { get_wizard_fixture("actions/open_composer") } @@ -373,7 +373,7 @@ describe CustomWizard::Action do context "with a guest" do describe "#create_topic" do - it "creates a staged guest poster if guest_email is set" do + before do Jobs.run_immediately! wizard_template["permitted"] = guests_permitted["permitted"] @@ -404,7 +404,9 @@ describe CustomWizard::Action do wizard_template[:actions] = [create_topic] update_template(wizard_template) + end + it "creates a staged guest poster if guest_email is set" do wizard = CustomWizard::Builder.new( @template[:id], nil, @@ -424,6 +426,29 @@ describe CustomWizard::Action do expect(topic.posts.first.user.staged).to eq(true) expect(topic.posts.first.user.primary_email.email).to eq('guest@email.com') end + + it "returns an existing user with the same email" do + existing = Fabricate(:user, email: 'guest@email.com') + + wizard = CustomWizard::Builder.new( + @template[:id], + nil, + CustomWizard::Wizard.generate_guest_id + ).build + wizard.create_updater( + wizard.steps.first.id, + step_1_field_5: "guest@email.com" + ).update + wizard.create_updater(wizard.steps.second.id, {}).update + wizard.create_updater(wizard.steps.last.id, + step_3_field_3: category.id + ).update + + topic = Topic.where(category_id: category.id).first + expect(topic.present?).to eq(true) + expect(topic.posts.first.user.staged).to eq(false) + expect(topic.posts.first.user.primary_email.email).to eq('guest@email.com') + end end describe "#send_message" do