Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions lib/custom_wizard/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -666,8 +659,6 @@ def new_group_params
full_name
title
bio_raw
owner_usernames
usernames
mentionable_level
messageable_level
visibility_level
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 27 additions & 0 deletions spec/components/custom_wizard/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading