A platform for community discussion. Free, open, simple.
FEATURE: check assignment node (#42608)
Joffrey JAFFEUX212acf0577bb
diff --git a/plugins/discourse-assign/config/locales/client.en.yml b/plugins/discourse-assign/config/locales/client.en.ymlindex 132e5ee93b5..f70a7698b42 100644--- a/plugins/discourse-assign/config/locales/client.en.yml+++ b/plugins/discourse-assign/config/locales/client.en.yml@@ -204,9 +204,11 @@ en:requires_assign: "Requires the Assign plugin to be enabled"nodes:"action:assign_topic": "Assign topic"+ "action:check_assignment": "Check assignment""trigger:assigned": "Assigned"node_descriptions:"action:assign_topic": "Assign or unassign a topic"+ "action:check_assignment": "Check whether a user or group is assigned to a topic or post""trigger:assigned": "Fires when a topic or post is assigned"assign_topic:operation: "Operation"@@ -216,6 +218,14 @@ en:topic_id: "Topic ID"assignee: "Assignee"replace_existing: "Replace existing assignment"+ check_assignment:+ target_type: "Target type"+ target_types:+ topic: "Topic"+ post: "Post"+ topic_id: "Topic ID"+ post_id: "Post ID"+ assignee: "Assignee"assigned:topic_assignments_only: "Only trigger when the assignment target is the topic, not an individual post"topic_assignments_only_description: "Topic assignments only"diff --git a/plugins/discourse-assign/config/locales/server.en.yml b/plugins/discourse-assign/config/locales/server.en.ymlindex d20d3e372d7..a7dd1f8de62 100644--- a/plugins/discourse-assign/config/locales/server.en.yml+++ b/plugins/discourse-assign/config/locales/server.en.yml@@ -62,6 +62,8 @@ en:discourse_workflows:assign_topic:error: "Failed to assign topic: %{reason}"+ check_assignment:+ unknown_target_type: "Unknown assignment target type: %{target_type}"assign_mailer:title: "Assign Mailer"subject_template: "[%{email_prefix}] %{assignee_name} assigned you to '%{topic_title}'!"diff --git a/plugins/discourse-assign/lib/discourse_assign/workflows/schema.rb b/plugins/discourse-assign/lib/discourse_assign/workflows/schema.rbindex 7481b138b69..74d06ec9227 100644--- a/plugins/discourse-assign/lib/discourse_assign/workflows/schema.rb+++ b/plugins/discourse-assign/lib/discourse_assign/workflows/schema.rb@@ -29,6 +29,16 @@ module DiscourseAssign},}.freeze+ CHECK_ASSIGNMENT_OUTPUT_SCHEMA = {+ "$schema" => DiscourseWorkflows::Schema::DRAFT_URI,+ "type" => "object",+ "properties" => {+ "is_assigned" => {+ "type" => "boolean",+ },+ },+ }.freeze+ASSIGNMENT_SCHEMA = {"$schema" => DiscourseWorkflows::Schema::DRAFT_URI,"type" => "object",diff --git a/plugins/discourse-assign/lib/discourse_workflows/nodes/check_assignment/v1.rb b/plugins/discourse-assign/lib/discourse_workflows/nodes/check_assignment/v1.rbnew file mode 100644index 00000000000..993201bd271--- /dev/null+++ b/plugins/discourse-assign/lib/discourse_workflows/nodes/check_assignment/v1.rb@@ -0,0 +1,108 @@+# frozen_string_literal: true++if defined?(DiscourseWorkflows)+ module DiscourseWorkflows+ module Nodes+ module CheckAssignment+ class V1 < DiscourseWorkflows::NodeType+ TARGET_TYPES = %w[topic post].freeze++ description(+ name: "action:check_assignment",+ version: "1.0",+ defaults: {+ icon: "user-check",+ color: "cyan",+ },+ group: "discourse_actions",+ available: -> { SiteSetting.assign_enabled },+ unavailable_reason_key: "discourse_workflows.node_unavailable.requires_assign",+ capabilities: {+ run_scope: "per_item",+ },+ output_contracts: [+ {+ schema: DiscourseAssign::Workflows::Schema::CHECK_ASSIGNMENT_OUTPUT_SCHEMA,+ mode: :merge,+ },+ ],+ properties: {+ target_type: {+ type: :options,+ required: true,+ options: TARGET_TYPES,+ default: "topic",+ no_data_expression: true,+ },+ topic_id: {+ type: :string,+ required: true,+ display_options: {+ show: {+ target_type: ["topic"],+ },+ },+ },+ post_id: {+ type: :string,+ required: true,+ display_options: {+ show: {+ target_type: ["post"],+ },+ },+ },+ assignee: {+ type: :string,+ required: true,+ ui: {+ control: :user_or_group,+ },+ },+ },+ )++ def execute(exec_ctx)+ items =+ exec_ctx.input_items.map.with_index do |item, item_index|+ target_type =+ exec_ctx.get_node_parameter("target_type", item_index, default: "topic").to_s+ target = find_target(exec_ctx, target_type, item_index)+ assignee = find_assignee(exec_ctx.get_node_parameter("assignee", item_index))+ is_assigned = ::Assignment.active.exists?(target:, assigned_to: assignee)++ wrap(+ item.fetch("json", {}).merge("is_assigned" => is_assigned),+ paired_item: exec_ctx.paired_item_for(item),+ )+ end++ [items]+ end++ private++ def find_target(exec_ctx, target_type, item_index)+ case target_type+ when "topic"+ ::Topic.find(exec_ctx.get_node_parameter("topic_id", item_index))+ when "post"+ ::Post.find(exec_ctx.get_node_parameter("post_id", item_index))+ else+ raise_node_error!(+ I18n.t(+ "discourse_assign.discourse_workflows.check_assignment.unknown_target_type",+ target_type:,+ ),+ )+ end+ end++ def find_assignee(identifier)+ ::User.find_by(username: identifier) || ::Group.find_by!(name: identifier)+ end+ end+ end+ end+ end+enddiff --git a/plugins/discourse-assign/plugin.rb b/plugins/discourse-assign/plugin.rbindex dcc4b1c091f..e4813d5397c 100644--- a/plugins/discourse-assign/plugin.rb+++ b/plugins/discourse-assign/plugin.rb@@ -29,7 +29,12 @@ after_initialize doregister_discourse_workflows_node dorequire_relative "lib/discourse_workflows/nodes/assign_topic/v1"require_relative "lib/discourse_workflows/nodes/assigned/v1"- [DiscourseWorkflows::Nodes::AssignTopic::V1, DiscourseWorkflows::Nodes::Assigned::V1]+ require_relative "lib/discourse_workflows/nodes/check_assignment/v1"+ [+ DiscourseWorkflows::Nodes::AssignTopic::V1,+ DiscourseWorkflows::Nodes::Assigned::V1,+ DiscourseWorkflows::Nodes::CheckAssignment::V1,+ ]endon(:assigned) do |assignment|diff --git a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/check_assignment/v1_spec.rb b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/check_assignment/v1_spec.rbnew file mode 100644index 00000000000..9a2a9a75bb3--- /dev/null+++ b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/check_assignment/v1_spec.rb@@ -0,0 +1,185 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::CheckAssignment::V1 do+ fab!(:user)+ fab!(:other_user, :user)+ fab!(:group)++ before do+ SiteSetting.assign_enabled = true+ SiteSetting.enable_discourse_workflows = true+ end++ describe ".output_schemas" do+ it "merges the assignment result into the input schema" do+ input_schema = {+ "$schema" => DiscourseWorkflows::Schema::DRAFT_URI,+ "type" => "object",+ "properties" => {+ "source" => {+ "type" => "string",+ },+ "is_assigned" => {+ "type" => "string",+ },+ },+ }++ output_schema = described_class.output_schemas({}, input_schemas: [input_schema]).first++ expect(output_schema.dig("properties", "source")).to eq("type" => "string")+ expect(output_schema.dig("properties", "is_assigned")).to eq("type" => "boolean")+ end+ end++ describe "#execute" do+ it "checks user and group topic assignments for each input item", :aggregate_failures do+ user_topic = Fabricate(:topic)+ group_topic = Fabricate(:topic)+ unassigned_topic = Fabricate(:topic)+ Fabricate(:topic_assignment, topic: user_topic, assigned_to: user)+ Fabricate(:topic_assignment, topic: group_topic, assigned_to: group)+ configuration = {+ "target_type" => "topic",+ "topic_id" => "={{ $json.topic_id }}",+ "assignee" => "={{ $json.assignee }}",+ }+ input_items = [+ {+ "json" => {+ "topic_id" => user_topic.id,+ "assignee" => user.username,+ "source" => "user assignment",+ },+ },+ {+ "json" => {+ "topic_id" => group_topic.id,+ "assignee" => group.name,+ "source" => "group assignment",+ },+ },+ {+ "json" => {+ "topic_id" => unassigned_topic.id,+ "assignee" => user.username,+ "source" => "no assignment",+ },+ },+ ]++ output = execute_node_output(configuration:, input_items:).first++ expect(output.map { |item| item["json"]["is_assigned"] }).to eq([true, true, false])+ expect(output.map { |item| item["json"]["source"] }).to eq(+ ["user assignment", "group assignment", "no assignment"],+ )+ expect(output.map { |item| item["pairedItem"] }).to eq(+ [{ "item" => 0 }, { "item" => 1 }, { "item" => 2 }],+ )+ output.each do |item|+ expect(item["json"]).to match_node_output_schema(described_class, configuration:)+ end+ end++ it "returns true for a direct post assignment" do+ post = Fabricate(:post)+ Fabricate(:post_assignment, post:, assigned_to: group)++ result =+ execute_node(+ configuration: {+ "target_type" => "post",+ "post_id" => post.id,+ "assignee" => group.name,+ },+ )++ expect(result["is_assigned"]).to eq(true)+ end++ it "keeps topic and post assignment targets separate", :aggregate_failures do+ topic_with_post_assignment = Fabricate(:topic)+ assigned_post = Fabricate(:post, topic: topic_with_post_assignment)+ Fabricate(:post_assignment, post: assigned_post, assigned_to: user)++ topic_with_topic_assignment = Fabricate(:topic)+ unassigned_post = Fabricate(:post, topic: topic_with_topic_assignment)+ Fabricate(:topic_assignment, topic: topic_with_topic_assignment, assigned_to: user)++ topic_result =+ execute_node(+ configuration: {+ "target_type" => "topic",+ "topic_id" => topic_with_post_assignment.id,+ "assignee" => user.username,+ },+ )+ post_result =+ execute_node(+ configuration: {+ "target_type" => "post",+ "post_id" => unassigned_post.id,+ "assignee" => user.username,+ },+ )++ expect(topic_result["is_assigned"]).to eq(false)+ expect(post_result["is_assigned"]).to eq(false)+ end++ it "returns false for inactive, unmatched, and missing assignments" do+ inactive_topic = Fabricate(:topic)+ unmatched_topic = Fabricate(:topic)+ unassigned_topic = Fabricate(:topic)+ Fabricate(:topic_assignment, topic: inactive_topic, assigned_to: user, active: false)+ Fabricate(:topic_assignment, topic: unmatched_topic, assigned_to: user)+ configuration = {+ "target_type" => "topic",+ "topic_id" => "={{ $json.topic_id }}",+ "assignee" => "={{ $json.assignee }}",+ }+ input_items = [+ { "json" => { "topic_id" => inactive_topic.id, "assignee" => user.username } },+ { "json" => { "topic_id" => unmatched_topic.id, "assignee" => other_user.username } },+ { "json" => { "topic_id" => unassigned_topic.id, "assignee" => user.username } },+ ]++ output = execute_node_output(configuration:, input_items:).first++ expect(output.map { |item| item["json"]["is_assigned"] }).to eq([false, false, false])+ end++ it "raises for invalid target types and missing records", :aggregate_failures do+ expect do+ execute_node(configuration: { "target_type" => "category", "assignee" => user.username })+ end.to raise_error(+ DiscourseWorkflows::NodeError,+ I18n.t(+ "discourse_assign.discourse_workflows.check_assignment.unknown_target_type",+ target_type: "category",+ ),+ )++ expect do+ execute_node(+ configuration: {+ "target_type" => "topic",+ "topic_id" => -1,+ "assignee" => user.username,+ },+ )+ end.to raise_error(ActiveRecord::RecordNotFound)++ expect do+ execute_node(+ configuration: {+ "target_type" => "topic",+ "topic_id" => Fabricate(:topic).id,+ "assignee" => "missing-assignee",+ },+ )+ end.to raise_error(ActiveRecord::RecordNotFound)+ end+ end+end