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
13 changes: 10 additions & 3 deletions lib/custom_wizard/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,15 @@ def update_profile
end

def watch_tags
tags = action_tags || []
level = action["notification_level"].to_sym
names = action_tags || []
tags = Tag.where_name(names).pluck(:name)

if tags.empty?
log_error("tags not found", "tags: #{names.join(",")}")
return
end

level = action["notification_level"]

if level.blank?
log_error("Notifcation Level was not set. Exiting watch tags action")
Expand All @@ -158,7 +165,7 @@ def watch_tags
users = action_users

users.each do |user|
result = TagUser.batch_set(user, level, tags)
result = TagUser.batch_set(user, level.to_sym, tags)

if result
log_success("#{user.username} notifications for #{tags} set to #{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.14
# version: 2.18.15
# 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
75 changes: 41 additions & 34 deletions spec/components/custom_wizard/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,30 +447,34 @@ def composer_query
}
end

it "watches tags" do
watch_tags[:tags][0][:output] = tag.name
wizard_template[:actions] << watch_tags
def run_action(action, step_1_field_1: "Text input")
wizard_template[:actions] << action
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
wizard.create_updater(wizard.steps[0].id, step_1_field_1: step_1_field_1).update
end

expect(TagUser.where(tag_id: tag.id, user_id: user.id).first.notification_level).to eq(2)
def tag_level(tag)
TagUser.find_by(tag: tag, user: user)&.notification_level
end

it "watches tags mapped to tag objects" do
watch_tags[:tags][0][:output] = "step_1_field_1"
watch_tags[:tags][0][:output_type] = "wizard_field"
wizard_template[:actions] << watch_tags
update_template(wizard_template)
def category_level(category)
CategoryUser.find_by(category: category, user: user)&.notification_level
end

wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps[0].id,
step_1_field_1: [{ id: tag.id, name: tag.name }],
).update
it "watches tags" do
watch_tags[:tags][0][:output] = tag.name
run_action(watch_tags)

expect(TagUser.where(tag_id: tag.id, user_id: user.id).first.notification_level).to eq(2)
expect(tag_level(tag)).to eq(TagUser.notification_levels[:tracking])
end

it "watches tags mapped to tag objects" do
watch_tags[:tags] = [step_1_field_1_input]
run_action(watch_tags, step_1_field_1: [{ id: tag.id, name: tag.name }])

expect(tag_level(tag)).to eq(TagUser.notification_levels[:tracking])
end

it "watches tags for the users in the action" do
Expand All @@ -486,52 +490,55 @@ def composer_query
output: [user1.username, user2.username],
},
]
wizard_template[:actions] << watch_tags
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
run_action(watch_tags)

expect(TagUser.where(tag_id: tag.id, notification_level: 2).pluck(:user_id)).to match_array(
[user1.id, user2.id],
)
end

def run_watch_categories
wizard_template[:actions] << watch_categories
update_template(wizard_template)
it "doesn't watch tags when no notification level is set" do
watch_tags[:tags][0][:output] = tag.name
watch_tags[:notification_level] = nil
run_action(watch_tags)

wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
expect(tag_level(tag)).to be_nil
end

def category_level(category)
CategoryUser.find_by(category: category, user: user)&.notification_level
{ "blank" => "", "not found" => "missing" }.each do |description, value|
it "keeps watched tags when the tags are #{description}" do
TagUser.change(user, tag.id, TagUser.notification_levels[:tracking])
watch_tags[:tags] = [step_1_field_1_input]
run_action(watch_tags, step_1_field_1: value)

expect(tag_level(tag)).to eq(TagUser.notification_levels[:tracking])
expect(CustomWizard::Log.list.logs.first.message).to include("tags not found")
end
end

it "watches categories" do
watch_categories[:categories][0][:output] = category.id
run_watch_categories
run_action(watch_categories)

expect(category_level(category)).to eq(CategoryUser.notification_levels[:tracking])
end

it "watches categories given by slug" do
watch_categories[:categories][0][:output] = category.slug
run_watch_categories
run_action(watch_categories)

expect(category_level(category)).to eq(CategoryUser.notification_levels[:tracking])
end

it "mutes the other categories when no category is given" do
run_watch_categories
run_action(watch_categories)

expect(category_level(category)).to eq(CategoryUser.notification_levels[:muted])
end

it "doesn't mute categories when no category is found" do
watch_categories[:categories][0][:output] = "missing"
run_watch_categories
run_action(watch_categories)

expect(CategoryUser.where(user: user)).to be_empty
end
Expand All @@ -549,7 +556,7 @@ def category_level(category)
output: [user1.username, user2.username],
},
]
run_watch_categories
run_action(watch_categories)

expect(
CategoryUser.where(category_id: category.id, notification_level: 2).pluck(:user_id),
Expand All @@ -567,7 +574,7 @@ def category_level(category)
watch_categories[:usernames] = [user1, user2].map do |u|
{ type: "assignment", output_type: "user", output_connector: "set", output: [u.username] }
end
run_watch_categories
run_action(watch_categories)

expect(
CategoryUser.where(notification_level: 2).pluck(:user_id, :category_id),
Expand Down
Loading