Skip to content

Commit

Permalink
Merge pull request #534 from slovensko-digital/feature/unassign_tag_a…
Browse files Browse the repository at this point in the history
…ction

Add new automation condition types & tests for automation UnassignMessageThreadTagAction
  • Loading branch information
luciajanikova authored Jan 15, 2025
2 parents 9ef8c5a + 31c8396 commit 48cdfec
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<span class="flex-grow-0 flex-shrink-0 text-lg font-medium text-left text-gray-900"><%= t condition.attr %></span>
<span class="flex-grow-0 flex-shrink-0 text-lg text-left text-gray-900"><%= t condition.type %></span>
<span class="flex-grow-0 flex-shrink-0 font-medium text-lg text-left text-gray-900"><%= condition.condition_object.name %></span>
<% elsif condition.type == 'Automation::BooleanCondition' %>
<span class="flex-grow-0 flex-shrink-0 text-lg font-medium text-left text-gray-900"><%= t condition.attr %></span>
<span class="flex-grow-0 flex-shrink-0 text-lg text-left text-gray-900"><%= t condition.type %></span>
<span class="flex-grow-0 flex-shrink-0 font-medium text-lg text-left text-gray-900"><%= t condition.value %></span>
<% else %>
<span class="flex-grow-0 flex-shrink-0 text-lg font-medium text-left text-gray-900"><%= t condition.attr %></span>
<span class="flex-grow-0 flex-shrink-0 text-lg text-left text-gray-900"><%= t condition.type %></span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<p class="text-base font-medium text-left text-gray-900"><%= t @form.object.attr %></p>
<p class="text-base text-left text-gray-900"><%= t @form.object.type %></p>
<p class="text-base font-medium text-left text-gray-900"><%= @form.object&.condition_object&.name %></p>
<% elsif @form.object.attr.in? ['outbox'] %>
<p class="text-base font-medium text-left text-gray-900"><%= t @form.object.attr %></p>
<p class="text-base text-left text-gray-900"><%= t @form.object.type %></p>
<p class="text-base font-medium text-left text-gray-900"><%= t @form.object.value %></p>
<% else %>
<p class="text-base font-medium text-left text-gray-900"><%= t @form.object.attr %></p>
<p class="text-base text-left text-gray-900"><%= t @form.object.type %></p>
Expand Down
20 changes: 19 additions & 1 deletion app/models/automation/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
34 changes: 31 additions & 3 deletions app/models/automation/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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' %>
9 changes: 8 additions & 1 deletion config/locales/sk.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
sk:
true: "áno"
false: "nie"
activerecord:
attributes:
message:
Expand Down Expand Up @@ -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:
Expand All @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/automation/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 12 additions & 0 deletions test/fixtures/automation/conditions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/fixtures/automation/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ six:
name: AttachmentContentContainsCondition2
trigger_event: message_created

unassign_done_tag:
user: basic
tenant: ssd
name: RemoveDoneTagWhenNewInboxMessage
trigger_event: message_created

46 changes: 46 additions & 0 deletions test/fixtures/govbox/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,49 @@ ssd_crac:
<ns6:Subject>Spracovanie po&#x17E;iadavky v registri autentifika&#x10D;n&#xFD;ch certifik&#xE1;tov</ns6:Subject>
<ns6:Text>&#x17D;iados&#x165; o zmenu z&#xE1;pisu autentifika&#x10D;n&#xE9;ho certifik&#xE1;tu v registri autentifika&#x10D;n&#xFD;ch certifik&#xE1;tov bola &#xFA;spe&#x161;ne spracovan&#xE1; 29.11.2024 13:00.</ns6:Text>
</ns6:InformationMessage>

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
5 changes: 5 additions & 0 deletions test/fixtures/message_thread_merge_identifiers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/message_threads.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/message_threads_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/models/automation/attachment_test.rb
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
43 changes: 43 additions & 0 deletions test/models/automation/rule_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 48cdfec

Please sign in to comment.