From 31c8396a7ed962f3f14281aef42a7a5c216dc649 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Tue, 14 Jan 2025 17:20:02 +0100 Subject: [PATCH] Add new automation condition types & tests for automation UnassingTagAction --- ...omation_rules_table_row_component.html.erb | 4 ++ .../rules/condition_form_component.html.erb | 4 ++ .../conditions_form_row_component.html.erb | 4 ++ app/models/automation/action.rb | 20 +++++++- app/models/automation/condition.rb | 34 ++++++++++++-- ...message_thread_tag_action.turbo_stream.erb | 2 + config/locales/sk.yml | 9 +++- test/fixtures/automation/actions.yml | 5 ++ test/fixtures/automation/conditions.yml | 12 +++++ test/fixtures/automation/rules.yml | 6 +++ test/fixtures/govbox/messages.yml | 46 +++++++++++++++++++ .../message_thread_merge_identifiers.yml | 5 ++ test/fixtures/message_threads.yml | 7 +++ test/fixtures/message_threads_tags.yml | 4 ++ test/fixtures/messages.yml | 12 +++++ test/fixtures/tags.yml | 6 +++ test/models/automation/attachment_test.rb | 2 +- test/models/automation/rule_test.rb | 43 +++++++++++++++++ 18 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 app/views/settings/automation_actions/_edit_form_unassign_message_thread_tag_action.turbo_stream.erb diff --git a/app/components/settings/rules/automation_rules_table_row_component.html.erb b/app/components/settings/rules/automation_rules_table_row_component.html.erb index 45abb4ab1..e9b9466fa 100644 --- a/app/components/settings/rules/automation_rules_table_row_component.html.erb +++ b/app/components/settings/rules/automation_rules_table_row_component.html.erb @@ -9,6 +9,10 @@ <%= t condition.attr %> <%= t condition.type %> <%= condition.condition_object.name %> + <% elsif condition.type == 'Automation::BooleanCondition' %> + <%= t condition.attr %> + <%= t condition.type %> + <%= t condition.value %> <% else %> <%= t condition.attr %> <%= t condition.type %> diff --git a/app/components/settings/rules/condition_form_component.html.erb b/app/components/settings/rules/condition_form_component.html.erb index 45cbe1284..fb4505263 100644 --- a/app/components/settings/rules/condition_form_component.html.erb +++ b/app/components/settings/rules/condition_form_component.html.erb @@ -6,6 +6,10 @@ <%= @form.select :type, @condition_type_list, {}, class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !@enabled %> <%= @form.select :condition_object_id, @form.object.box_list, {}, class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !@enabled %> <%= @form.hidden_field :condition_object_type, value: 'Box' %> + <% elsif @form.object.attr.in? ['outbox'] %> + <%= @form.select :attr, @attr_list, {}, onchange: "this.form.requestSubmit(this.form.querySelector(\"#rerender\"))", class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !@enabled %> + <%= @form.select :type, @condition_type_list, {}, class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !@enabled %> + <%= @form.select :value, [['áno', true], ['nie', false]], {}, class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !@enabled %> <% else %> <%= @form.select :attr, @attr_list, {}, onchange: "this.form.requestSubmit(this.form.querySelector(\"#rerender\"))", class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !@enabled %> <%= @form.select :type, @condition_type_list, {}, class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !@enabled %> diff --git a/app/components/settings/rules/conditions_form_row_component.html.erb b/app/components/settings/rules/conditions_form_row_component.html.erb index de02aaecd..b28a012c6 100644 --- a/app/components/settings/rules/conditions_form_row_component.html.erb +++ b/app/components/settings/rules/conditions_form_row_component.html.erb @@ -6,6 +6,10 @@
<%= t @form.object.attr %>
<%= t @form.object.type %>
<%= @form.object&.condition_object&.name %>
+ <% elsif @form.object.attr.in? ['outbox'] %> +<%= t @form.object.attr %>
+<%= t @form.object.type %>
+<%= t @form.object.value %>
<% else %><%= t @form.object.attr %>
<%= t @form.object.type %>
diff --git a/app/models/automation/action.rb b/app/models/automation/action.rb index 37d00df71..c183cd2c8 100644 --- a/app/models/automation/action.rb +++ b/app/models/automation/action.rb @@ -17,7 +17,7 @@ class Action < ApplicationRecord belongs_to :action_object, polymorphic: true, optional: true attr_accessor :delete_record - ACTION_LIST = ['Automation::AddMessageThreadTagAction', 'Automation::FireWebhookAction', 'Automation::ChangeMessageThreadTitleAction'].freeze + ACTION_LIST = ['Automation::AddMessageThreadTagAction', 'Automation::UnassignMessageThreadTagAction', 'Automation::FireWebhookAction', 'Automation::ChangeMessageThreadTitleAction'].freeze def tag_list automation_rule.tenant.tags.pluck(:name, :id) @@ -28,6 +28,24 @@ def tag_list class AddTagAction < Action end + class UnassignMessageThreadTagAction < Action + def run!(thing, _event) + tag = action_object + return if thing.tenant != tag.tenant + + object = if thing.respond_to? :thread + thing.thread + else + thing + end + object.unassign_tag(tag) if tag && object.tags.include?(tag) + end + + def object_based? + true + end + end + class AddMessageThreadTagAction < Action def run!(thing, _event) tag = action_object diff --git a/app/models/automation/condition.rb b/app/models/automation/condition.rb index a69fe3cfd..e3fb51a07 100644 --- a/app/models/automation/condition.rb +++ b/app/models/automation/condition.rb @@ -21,7 +21,7 @@ class Condition < ApplicationRecord attr_accessor :delete_record # when adding items, check defaults in condition_form_component.rb - ATTR_LIST = %i[box sender_name recipient_name title sender_uri recipient_uri attachment fs_submission_status fs_message_type object_type].freeze + ATTR_LIST = %i[box sender_name recipient_name title sender_uri recipient_uri outbox attachment edesk_class fs_submission_status fs_message_type object_type].freeze def valid_condition_type_list_for_attr Automation::Condition.subclasses.map do |subclass| @@ -48,9 +48,23 @@ def cleanup_record end end + class BooleanCondition < Automation::Condition + validates :value, presence: true + VALID_ATTR_LIST = %w[outbox].freeze + validates :attr, inclusion: { in: VALID_ATTR_LIST } + + def satisfied?(thing) + thing[attr] == ActiveRecord::Type::Boolean.new.cast(value) + end + + def cleanup_record + self.condition_object = nil + end + end + class MetadataValueCondition < Automation::Condition validates :value, presence: true - VALID_ATTR_LIST = %w[sender_uri recipient_uri fs_submission_status fs_message_type].freeze + VALID_ATTR_LIST = %w[sender_uri recipient_uri edesk_class fs_submission_status fs_message_type].freeze validates :attr, inclusion: { in: VALID_ATTR_LIST } def satisfied?(thing) @@ -62,6 +76,20 @@ def cleanup_record end end + class MetadataValueNotCondition < Automation::Condition + validates :value, presence: true + VALID_ATTR_LIST = %w[sender_uri recipient_uri edesk_class fs_submission_status fs_message_type].freeze + validates :attr, inclusion: { in: VALID_ATTR_LIST } + + def satisfied?(thing) + thing.metadata && !thing.metadata[attr]&.match?(value) + end + + def cleanup_record + self.condition_object = nil + end + end + class BoxCondition < Automation::Condition validates_associated :condition_object VALID_ATTR_LIST = ['box'].freeze @@ -83,7 +111,7 @@ def cleanup_record class MessageMetadataValueCondition < Automation::Condition validates :value, presence: true - VALID_ATTR_LIST = %w[fs_message_type].freeze + VALID_ATTR_LIST = %w[edesk_class fs_message_type].freeze validates :attr, inclusion: { in: VALID_ATTR_LIST } def satisfied?(thing) diff --git a/app/views/settings/automation_actions/_edit_form_unassign_message_thread_tag_action.turbo_stream.erb b/app/views/settings/automation_actions/_edit_form_unassign_message_thread_tag_action.turbo_stream.erb new file mode 100644 index 000000000..f8a5baf08 --- /dev/null +++ b/app/views/settings/automation_actions/_edit_form_unassign_message_thread_tag_action.turbo_stream.erb @@ -0,0 +1,2 @@ +<%= form.select :action_object_id, form.object.tag_list, {}, class: "mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-400 ring-1 ring-inset ring-gray-400 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6", disabled: !enabled %> +<%= form.hidden_field :action_object_type, value: 'Tag' %> diff --git a/config/locales/sk.yml b/config/locales/sk.yml index 8b86a5cb9..8c3fb570a 100644 --- a/config/locales/sk.yml +++ b/config/locales/sk.yml @@ -1,4 +1,6 @@ sk: + true: "áno" + false: "nie" activerecord: attributes: message: @@ -262,9 +264,11 @@ sk: recipient_name: "Prijímateľ" sender_uri: "URI odosielateľa" recipient_uri: "URI prijímateľa" + outbox: "Odoslaná pošta" object_type: "Typ objektu" fs_submission_status: "Stav podania" - fs_message_type: "Typ správy" + edesk_class: "Typ ÚPVS správy" + fs_message_type: "Typ FS správy" box: "Schránka správy" attachment: "Príloha" boxes: @@ -276,11 +280,14 @@ sk: message_draft_submitted: "Odoslaná správa" message_object_downloaded: "Stiahnutý objekt správy" "Automation::ContainsCondition": "obsahuje" + "Automation::BooleanCondition": "je" "Automation::AttachmentContentContainsCondition": "obsahuje" "Automation::MetadataValueCondition": "v metadátach obsahuje" + "Automation::MetadataValueNotCondition": "v metadátach neobsahuje" "Automation::MessageMetadataValueCondition": "správa v metadátach obsahuje" "Automation::BoxCondition": "je" "Automation::AddMessageThreadTagAction": "Pridaj štítok na vlákno" + "Automation::UnassignMessageThreadTagAction": "Odober štítok z vlákna" "Automation::AddTagAction": "Pridaj štítok" "Automation::ChangeMessageThreadTitleAction": "Premenuj vlákno na" "Automation::FireWebhookAction": "Zavolaj integráciu" diff --git a/test/fixtures/automation/actions.yml b/test/fixtures/automation/actions.yml index 19406586c..4c92353ca 100644 --- a/test/fixtures/automation/actions.yml +++ b/test/fixtures/automation/actions.yml @@ -37,3 +37,8 @@ seven: automation_rule: six type: Automation::AddMessageThreadTagAction action_object: ssd_crac_success (Tag) + +unassign_done_tag: + automation_rule: unassign_done_tag + type: Automation::UnassignMessageThreadTagAction + action_object: ssd_done (Tag) diff --git a/test/fixtures/automation/conditions.yml b/test/fixtures/automation/conditions.yml index f52bf7880..908eedf39 100644 --- a/test/fixtures/automation/conditions.yml +++ b/test/fixtures/automation/conditions.yml @@ -36,3 +36,15 @@ seven: automation_rule: six type: Automation::AttachmentContentContainsCondition value: "úspešne spracovaná" + +is_not_outbox: + automation_rule: unassign_done_tag + attr: outbox + type: Automation::BooleanCondition + value: false + +is_not_posting_confirmation: + automation_rule: unassign_done_tag + attr: edesk_class + type: Automation::MetadataValueNotCondition + value: POSTING_CONFIRMATION diff --git a/test/fixtures/automation/rules.yml b/test/fixtures/automation/rules.yml index 8817dfb85..a28da6a24 100644 --- a/test/fixtures/automation/rules.yml +++ b/test/fixtures/automation/rules.yml @@ -36,3 +36,9 @@ six: name: AttachmentContentContainsCondition2 trigger_event: message_created +unassign_done_tag: + user: basic + tenant: ssd + name: RemoveDoneTagWhenNewInboxMessage + trigger_event: message_created + diff --git a/test/fixtures/govbox/messages.yml b/test/fixtures/govbox/messages.yml index ba8f38012..bc011821d 100644 --- a/test/fixtures/govbox/messages.yml +++ b/test/fixtures/govbox/messages.yml @@ -299,3 +299,49 @@ ssd_crac: