diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index ccfe280d7..c8a091f29 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -331,29 +331,22 @@ def route_to def create_group group = begin - Group.new(new_group_params.except(:usernames, :owner_usernames)) + Group.new(new_group_params) rescue ArgumentError => e raise Discourse::InvalidParameters, "Invalid group params" end if group.save - def get_user_ids(username_string) - User.where(username: username_string.split(",")).pluck(:id) - end + owner_ids = User.where(username_lower: map_usernames(action["owner_usernames"])).pluck(:id) + usernames = map_usernames(action["usernames"]) + user_ids = User.where(username_lower: usernames).pluck(:id) - if new_group_params[:owner_usernames].present? - owner_ids = get_user_ids(new_group_params[:owner_usernames]) - owner_ids.each { |user_id| group.group_users.build(user_id: user_id, owner: true) } + if user_ids.size < usernames.size + log_error("Warning, group creation: some users were not found!") end - if new_group_params[:usernames].present? - user_ids = get_user_ids(new_group_params[:usernames]) - if user_ids.count < new_group_params[:usernames].count - log_error("Warning, group creation: some users were not found!") - end - user_ids -= owner_ids if owner_ids - user_ids.each { |user_id| group.group_users.build(user_id: user_id) } - end + owner_ids.each { |user_id| group.group_users.build(user_id: user_id, owner: true) } + (user_ids - owner_ids).each { |user_id| group.group_users.build(user_id: user_id) } log_success("Group created", group.name) if group.save @@ -411,13 +404,15 @@ def action_tags end def map_list(inputs) + return [] if inputs.blank? + output = CustomWizard::Mapper.new( inputs: inputs, data: mapper_data, user: user, opts: { - multiple: inputs&.all? { |input| input["type"] == "assignment" }, + multiple: inputs.all? { |input| input["type"] == "assignment" }, }, ).perform @@ -428,14 +423,12 @@ def map_list(inputs) .compact_blank end - def action_users - users = [] - - if action["usernames"].present? - usernames = map_list(action["usernames"]).map { |value| User.normalize_username(value) } - users.push(*User.where(username_lower: usernames)) - end + def map_usernames(inputs) + map_list(inputs).map { |value| User.normalize_username(value) }.uniq + end + def action_users + users = User.where(username_lower: map_usernames(action["usernames"])).to_a users.push(user) if ActiveRecord::Type::Boolean.new.cast(action["wizard_user"]) users.uniq @@ -666,8 +659,6 @@ def new_group_params full_name title bio_raw - owner_usernames - usernames mentionable_level messageable_level visibility_level diff --git a/plugin.rb b/plugin.rb index 340a37e35..aef351273 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.18.11 +# version: 2.18.12 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # 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 e1a983b4e..4cc552e85 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -843,6 +843,33 @@ def complete_as_guest(email) expect(GroupUser.where(group_id: group_id).count).to eq(1) end + it "#create_group adds the users of every username input" do + user2 = Fabricate(:user) + create_group["usernames"] = [ + { + type: "assignment", + output_type: "text", + output_connector: "set", + output: user2.username.upcase, + }, + { + type: "assignment", + output_type: "user", + output_connector: "set", + output: [user1.username], + }, + ] + wizard_template["actions"] << create_group + update_template(wizard_template) + + wizard = CustomWizard::Builder.new(@template[:id], user).build + wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update + + group = Group.find_by!(name: wizard.current_submission.fields["action_9"]) + expect(group.users).to contain_exactly(user, user1, user2) + expect(group.group_users.where(owner: true).pluck(:user_id)).to eq([user.id]) + end + it "#add_to_group" do wizard_template["actions"] << create_group wizard_template["actions"] << add_to_group