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: Spracovanie požiadavky v registri autentifikačných certifikátov Žiadosť o zmenu zápisu autentifikačného certifikátu v registri autentifikačných certifikátov bola úspešne spracovaná 29.11.2024 13:00. + +ssd_done_new: + message_id: <%= SecureRandom.uuid %> + correlation_id: d2d6ab13-347e-49f4-9c3b-0b8390430870 + edesk_message_id: 101 + delivered_at: <%= DateTime.current %> + edesk_class: TEST + folder: ssd_one + payload: + message_id: <%= SecureRandom.uuid %> + subject: MySubject + sender_name: MySender + sender_uri: MySenderURI + recipient_name: MyRecipient + delivered_at: <%= DateTime.current.to_s %> + original_html: MyHtml + class: TEST + objects: + - name: form + mime_type: application/x-eform-xml + signed: false + class: FORM + content: Dummy + +ssd_posting_confirmation: + message_id: <%= SecureRandom.uuid %> + correlation_id: d2d6ab13-347e-49f4-9c3b-0b8390430870 + edesk_message_id: 102 + delivered_at: <%= DateTime.current %> + edesk_class: POSTING_CONFIRMATION + folder: ssd_one + payload: + message_id: <%= SecureRandom.uuid %> + subject: MySubject + sender_name: MySender + sender_uri: MySenderURI + recipient_name: MyRecipient + delivered_at: <%= DateTime.current.to_s %> + original_html: MyHtml + class: POSTING_CONFIRMATION + objects: + - name: form + mime_type: application/x-eform-xml + signed: false + class: FORM + content: Dummy diff --git a/test/fixtures/message_thread_merge_identifiers.yml b/test/fixtures/message_thread_merge_identifiers.yml index 5951701c6..22f129411 100644 --- a/test/fixtures/message_thread_merge_identifiers.yml +++ b/test/fixtures/message_thread_merge_identifiers.yml @@ -20,6 +20,11 @@ ssd_main_issue_one: uuid: <%= SecureRandom.uuid %> box: ssd_main +ssd_main_done: + message_thread: ssd_main_done + uuid: d2d6ab13-347e-49f4-9c3b-0b8390430870 + box: ssd_main + ssd_main_draft_to_be_signed: message_thread: ssd_main_draft_to_be_signed uuid: 7a364355-882c-41d2-b1b3-e215644f805b diff --git a/test/fixtures/message_threads.yml b/test/fixtures/message_threads.yml index 65ac7df3b..0ee20cd34 100644 --- a/test/fixtures/message_threads.yml +++ b/test/fixtures/message_threads.yml @@ -35,6 +35,13 @@ ssd_main_with_application: delivered_at: 2023-05-18 18:15:22 last_message_delivered_at: 2023-05-18 18:16:22 +ssd_main_done: + box: ssd_main + title: General agenda SSD - done + original_title: Original title + delivered_at: 2023-05-18 16:05:00 + last_message_delivered_at: 2023-05-18 16:06:00 + ssd_main_draft_to_be_signed: box: ssd_main title: Draft to be signed diff --git a/test/fixtures/message_threads_tags.yml b/test/fixtures/message_threads_tags.yml index 28b65ed05..0790c3b70 100644 --- a/test/fixtures/message_threads_tags.yml +++ b/test/fixtures/message_threads_tags.yml @@ -36,6 +36,10 @@ ssd_main_delivery_everything: message_thread: ssd_main_delivery tag: ssd_everything +ssd_main_done_done: + message_thread: ssd_main_done + tag: ssd_done + ssd_main_draft_only_everything: message_thread: ssd_main_draft_only tag: ssd_everything diff --git a/test/fixtures/messages.yml b/test/fixtures/messages.yml index 744b7946a..24e10771f 100644 --- a/test/fixtures/messages.yml +++ b/test/fixtures/messages.yml @@ -50,6 +50,18 @@ ssd_main_general_four: posp_id: App.GeneralAgenda posp_version: 1.9 message_type: App.GeneralAgenda + correlation_id: d2d6ab13-347e-49f4-9c3b-0b8390430870 + +ssd_main_general_done: + uuid: <%= SecureRandom.uuid %> + title: The done Message + delivered_at: 2023-05-18 16:17:26 + thread: ssd_main_done + metadata: + authorized: false + posp_id: App.GeneralAgenda + posp_version: 1.9 + message_type: App.GeneralAgenda ssd_main_fs_one: uuid: <%= SecureRandom.uuid %> diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml index 06947b6bf..0754f3e35 100644 --- a/test/fixtures/tags.yml +++ b/test/fixtures/tags.yml @@ -80,6 +80,12 @@ ssd_crac_success: visible: true tenant: ssd +ssd_done: + name: Vybavené + type: SimpleTag + visible: true + tenant: ssd + ssd_signature_requested: name: Na podpis type: SignatureRequestedTag diff --git a/test/models/automation/attachment_test.rb b/test/models/automation/attachment_test.rb index 4df940206..2cb30dad5 100644 --- a/test/models/automation/attachment_test.rb +++ b/test/models/automation/attachment_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class Automation::RuleTest < ActiveSupport::TestCase +class Automation::AttachmentTest < ActiveSupport::TestCase test 'should run an automation on message created AttachmentContentContainsCondition ChangeMessageThreadTitleAction and not match' do govbox_message = govbox_messages(:ssd_general_agenda_with_lorem_pdf) govbox_message.payload["objects"].first["content"] = File.read("test/fixtures/files/lorem_ipsum.pdf") diff --git a/test/models/automation/rule_test.rb b/test/models/automation/rule_test.rb index 0a5b118ae..ac53175a4 100644 --- a/test/models/automation/rule_test.rb +++ b/test/models/automation/rule_test.rb @@ -79,4 +79,47 @@ class Automation::RuleTest < ActiveSupport::TestCase assert_includes message.thread.tags, tags(:ssd_crac_success) assert_not_includes message.tags, tags(:ssd_crac_success) end + + test 'should run an automation on message created outbox BooleanCondition, edesk_class MessageMetadataValueNotCondition UnassignMessageThreadTagAction if conditions satisfied' do + tag = tags(:ssd_done) + message_thread = message_threads(:ssd_main_done) + govbox_message = govbox_messages(:ssd_done_new) + + assert_includes message_thread.tags, tag + + Govbox::Message.create_message_with_thread!(govbox_message) + travel_to(15.minutes.from_now) { GoodJob.perform_inline } + + assert_not_includes message_thread.tags.reload, tag + end + + test 'should not run an automation on message created outbox BooleanCondition, edesk_class MessageMetadataValueNotCondition UnassignMessageThreadTagAction if outbox message delivered' do + tag = tags(:ssd_done) + message_thread = message_threads(:ssd_main_done) + govbox_message = govbox_messages(:ssd_outbox) + + govbox_message.update_column(:correlation_id, 'd2d6ab13-347e-49f4-9c3b-0b8390430870') + + assert_includes message_thread.tags, tag + + Govbox::Message.create_message_with_thread!(govbox_message) + travel_to(15.minutes.from_now) { GoodJob.perform_inline } + + assert_includes message_thread.tags, tag + end + + test 'should not run an automation on message created outbox BooleanCondition, edesk_class MessageMetadataValueNotCondition UnassignMessageThreadTagAction if POSTING_CONFIRMATION delivered' do + tag = tags(:ssd_done) + message_thread = message_threads(:ssd_main_done) + govbox_message = govbox_messages(:ssd_posting_confirmation) + + govbox_message.update_column(:correlation_id, 'd2d6ab13-347e-49f4-9c3b-0b8390430870') + + assert_includes message_thread.tags, tag + + Govbox::Message.create_message_with_thread!(govbox_message) + travel_to(15.minutes.from_now) { GoodJob.perform_inline } + + assert_includes message_thread.tags, tag + end end