Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@

case trigger["kind"]
when DiscourseAutomation::Triggerable::API_CALL, DiscourseAutomation::Triggerable::RECURRING
query = DB.query(<<-SQL, custom_field_name)
query = DB.query(<<-SQL, custom_field_name: custom_field_name, prefix: ::User::USER_FIELD_PREFIX)
SELECT u.id as user_id, g.id as group_id
FROM users u
JOIN user_custom_fields ucf
ON u.id = ucf.user_id
AND ucf.name = ?
AND ucf.name = CASE
WHEN (SELECT id FROM user_fields WHERE name = :custom_field_name) > 0
THEN CONCAT(:prefix, (SELECT id FROM user_fields WHERE name = :custom_field_name))
ELSE :custom_field_name
END
JOIN groups g
on g.full_name ilike ucf.value
ON g.full_name ilike ucf.value
FULL OUTER JOIN group_users gu
ON gu.user_id = u.id
AND gu.group_id = g.id
Expand Down
40 changes: 39 additions & 1 deletion spec/scripts/add_user_to_group_through_custom_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@

context "with one matching user" do
before do
user_field = UserField.create!(
name: "groupity_group",
description: "What group would you like to be added to?",
field_type: "text",
)
UserCustomField.create!(
user_id: user_1.id,
name: "groupity_group",
name: "user_field_#{user_field.id}",
value: target_group.full_name,
)
end
Expand Down Expand Up @@ -130,4 +135,37 @@
end
end
end

context "when UserCustomField name is set as custom_field_name" do
context "with one matching user" do
before do
user_field = UserField.create!(
name: "groupity_group",
description: "What group would you like to be added to?",
field_type: "text",
)
UserCustomField.create!(
user_id: user_1.id,
name: "user_field_#{user_field.id}",
value: target_group.full_name,
)
automation.upsert_field!(
"custom_field_name",
"text",
{ value: "user_field_#{user_field.id}" },
target: "script",
)
end

it "works" do
expect(user_1.in_any_groups?([target_group.id])).to eq(false)
expect(user_2.in_any_groups?([target_group.id])).to eq(false)

automation.trigger!("kind" => DiscourseAutomation::Triggerable::RECURRING)

expect(user_1.reload.in_any_groups?([target_group.id])).to eq(true)
expect(user_2.reload.in_any_groups?([target_group.id])).to eq(false)
end
end
end
end