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
17 changes: 7 additions & 10 deletions lib/custom_wizard/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ def watch_tags
end

def watch_categories
watched_categories =
CustomWizard::Mapper.new(inputs: action["categories"], data: mapper_data, user: user).perform

watched_categories = [*watched_categories].map(&:to_i)
watched_categories = map_list(action["categories"]).map(&:to_i)

notification_level = action["notification_level"]

Expand Down Expand Up @@ -424,18 +421,18 @@ def map_list(inputs)
},
).perform

Array.wrap(output).flatten.compact_blank
Array
.wrap(output)
.flatten
.flat_map { |value| value.is_a?(String) ? value.split(/[,\n]/).map(&:strip) : value }
.compact_blank
end

def action_users
users = []

if action["usernames"].present?
output =
CustomWizard::Mapper.new(inputs: action["usernames"], data: mapper_data, user: user).perform
values = Array.wrap(output).flatten.flat_map { |value| value.to_s.split(",") }
usernames = values.filter_map { |value| User.normalize_username(value.strip) }

usernames = map_list(action["usernames"]).map { |value| User.normalize_username(value) }
users.push(*User.where(username_lower: usernames))
end

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.10
# version: 2.18.11
# 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
74 changes: 59 additions & 15 deletions spec/components/custom_wizard/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ def topic_tags
expect(topic_tags).to contain_exactly(tag.name, tag2.name)
end

it "adds each tag of a list input" do
set_tag_inputs(text_input("#{tag.name}\n#{tag2.name}").merge(output_type: "list"))

complete_wizard

expect(topic_tags).to contain_exactly(tag.name, tag2.name)
end

it "adds the tags of every input" do
set_tag_inputs(tag_field_input, text_input(tag2.name))

Expand Down Expand Up @@ -417,6 +425,15 @@ def composer_query
context "standard actions" do
before { Jobs.run_immediately! }

let(:step_1_field_1_input) do
{
type: "assignment",
output_type: "wizard_field",
output_connector: "set",
output: "step_1_field_1",
}
end

it "watches tags" do
watch_tags[:tags][0][:output] = tag.name
wizard_template[:actions] << watch_tags
Expand Down Expand Up @@ -504,6 +521,28 @@ def composer_query
).to match_array([user1.id, user2.id])
end

it "watches the categories of every category input for the users of every user input" do
category2 = Fabricate(:category)
user2 = Fabricate(:user)

watch_categories[:categories] = [category, category2].map do |c|
{ type: "assignment", output_type: "category", output_connector: "set", output: [c.id] }
end
watch_categories[:wizard_user] = false
watch_categories[:usernames] = [user1, user2].map do |u|
{ type: "assignment", output_type: "user", output_connector: "set", output: [u.username] }
end
wizard_template[:actions] << watch_categories
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

expect(
CategoryUser.where(notification_level: 2).pluck(:user_id, :category_id),
).to match_array([user1, user2].product([category, category2]).map { |u, c| [u.id, c.id] })
end

it "#send_message" do
Jobs.run_immediately!

Expand Down Expand Up @@ -578,12 +617,7 @@ def composer_query

it "#send_message sends to the recipients of every recipient input" do
send_message_multi["recipient"] = [
{
type: "assignment",
output_type: "wizard_field",
output_connector: "set",
output: "step_1_field_1",
},
step_1_field_1_input,
{
type: "assignment",
output_type: "user",
Expand All @@ -606,7 +640,24 @@ def composer_query
wizard.create_updater(wizard.steps[1].id, {}).update

topic =
Topic.find_by(archetype: Archetype.private_message, title: "Multiple Recipients title")
Topic.find_by!(archetype: Archetype.private_message, title: "Multiple Recipients title")
expect(topic.allowed_users).to include(user1)
expect(topic.allowed_groups).to contain_exactly(group)
end

it "#send_message sends to each recipient in a comma separated value" do
send_message["recipient"] = [step_1_field_1_input]
wizard_template["actions"] << send_message
update_template(wizard_template)

wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps[0].id,
step_1_field_1: "#{user1.username},#{group.name}",
).update
wizard.create_updater(wizard.steps[1].id, {}).update

topic = Topic.find_by!(archetype: Archetype.private_message, title: "Message title")
expect(topic.allowed_users).to include(user1)
expect(topic.allowed_groups).to contain_exactly(group)
end
Expand Down Expand Up @@ -714,14 +765,7 @@ def complete_as_guest(email)
wizard_template["permitted"] = guests_permitted["permitted"]
wizard_template.delete("actions")

send_message["recipient"] = [
{
type: "assignment",
output: "step_1_field_1",
output_type: "wizard_field",
output_connector: "set",
},
]
send_message["recipient"] = [step_1_field_1_input]

wizard_template["actions"] = [send_message]
update_template(wizard_template)
Expand Down
Loading