From b8047f0912e43f8786598aa525cf97941b853c85 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Thu, 1 Feb 2024 15:42:12 +0530 Subject: [PATCH] feat: sla-2 add automation backend support for SLA (#8775) * feat: add automation support for SLA * feat: add sla action in automtion UI * chore: revert frontend changes * chore: refactor to ee namespace * chore: refactor automation rule to ee namespace * feat: create applied_sla table entry * chore: add applied_sla spec * chore: rubocop fixes --------- Co-authored-by: Sojan --- app/models/automation_rule.rb | 18 ++++++++++----- app/services/action_service.rb | 2 ++ .../app/models/enterprise/automation_rule.rb | 9 ++++++++ .../app/services/enterprise/action_service.rb | 15 ++++++++++++ .../enterprise/models/automation_rule_spec.rb | 7 ++++++ .../enterprise/action_service_spec.rb | 23 +++++++++++++++++++ 6 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 enterprise/app/models/enterprise/automation_rule.rb create mode 100644 enterprise/app/services/enterprise/action_service.rb create mode 100644 spec/enterprise/services/enterprise/action_service_spec.rb diff --git a/app/models/automation_rule.rb b/app/models/automation_rule.rb index 5f5d59814..955df7497 100644 --- a/app/models/automation_rule.rb +++ b/app/models/automation_rule.rb @@ -30,10 +30,15 @@ class AutomationRule < ApplicationRecord scope :active, -> { where(active: true) } - CONDITIONS_ATTRS = %w[content email country_code status message_type browser_language assignee_id team_id referer city company inbox_id - mail_subject phone_number priority conversation_language].freeze - ACTIONS_ATTRS = %w[send_message add_label remove_label send_email_to_team assign_team assign_agent send_webhook_event mute_conversation - send_attachment change_status resolve_conversation snooze_conversation change_priority send_email_transcript].freeze + def conditions_attributes + %w[content email country_code status message_type browser_language assignee_id team_id referer city company inbox_id + mail_subject phone_number priority conversation_language] + end + + def actions_attributes + %w[send_message add_label remove_label send_email_to_team assign_team assign_agent send_webhook_event mute_conversation + send_attachment change_status resolve_conversation snooze_conversation change_priority send_email_transcript].freeze + end def file_base_data files.map do |file| @@ -55,7 +60,7 @@ class AutomationRule < ApplicationRecord return if conditions.blank? attributes = conditions.map { |obj, _| obj['attribute_key'] } - conditions = attributes - CONDITIONS_ATTRS + conditions = attributes - conditions_attributes conditions -= account.custom_attribute_definitions.pluck(:attribute_key) errors.add(:conditions, "Automation conditions #{conditions.join(',')} not supported.") if conditions.any? end @@ -64,7 +69,7 @@ class AutomationRule < ApplicationRecord return if actions.blank? attributes = actions.map { |obj, _| obj['action_name'] } - actions = attributes - ACTIONS_ATTRS + actions = attributes - actions_attributes errors.add(:actions, "Automation actions #{actions.join(',')} not supported.") if actions.any? end @@ -78,3 +83,4 @@ class AutomationRule < ApplicationRecord end AutomationRule.include_mod_with('Audit::AutomationRule') +AutomationRule.prepend_mod_with('AutomationRule') diff --git a/app/services/action_service.rb b/app/services/action_service.rb index 5a38db47f..7a9a14d0d 100644 --- a/app/services/action_service.rb +++ b/app/services/action_service.rb @@ -89,3 +89,5 @@ class ActionService @conversation.additional_attributes['type'] == 'tweet' end end + +ActionService.include_mod_with('ActionService') diff --git a/enterprise/app/models/enterprise/automation_rule.rb b/enterprise/app/models/enterprise/automation_rule.rb new file mode 100644 index 000000000..1e1e85a6d --- /dev/null +++ b/enterprise/app/models/enterprise/automation_rule.rb @@ -0,0 +1,9 @@ +module Enterprise::AutomationRule + def conditions_attributes + super + %w[sla_policy_id] + end + + def actions_attributes + super + %w[add_sla] + end +end diff --git a/enterprise/app/services/enterprise/action_service.rb b/enterprise/app/services/enterprise/action_service.rb new file mode 100644 index 000000000..6e21031aa --- /dev/null +++ b/enterprise/app/services/enterprise/action_service.rb @@ -0,0 +1,15 @@ +module Enterprise::ActionService + def add_sla(sla_policy) + @conversation.update!(sla_policy_id: sla_policy.id) + create_applied_sla(sla_policy) + end + + def create_applied_sla(sla_policy) + AppliedSla.create!( + account_id: @conversation.account_id, + sla_policy_id: sla_policy.id, + conversation_id: @conversation.id, + sla_status: 'active' + ) + end +end diff --git a/spec/enterprise/models/automation_rule_spec.rb b/spec/enterprise/models/automation_rule_spec.rb index 69e7949b7..e2e929d2c 100644 --- a/spec/enterprise/models/automation_rule_spec.rb +++ b/spec/enterprise/models/automation_rule_spec.rb @@ -25,5 +25,12 @@ RSpec.describe AutomationRule do expect(Audited::Audit.where(auditable_type: 'AutomationRule', action: 'destroy').count).to eq 1 end end + + context 'when automation rule is in enterprise namespace' do + it 'has associated sla methods available' do + expect(automation_rule.conditions_attributes).to include('sla_policy_id') + expect(automation_rule.actions_attributes).to include('add_sla') + end + end end end diff --git a/spec/enterprise/services/enterprise/action_service_spec.rb b/spec/enterprise/services/enterprise/action_service_spec.rb new file mode 100644 index 000000000..6efffca6d --- /dev/null +++ b/spec/enterprise/services/enterprise/action_service_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +describe ActionService do + let(:account) { create(:account) } + + describe '#add_sla' do + let(:sla_policy) { create(:sla_policy, account: account) } + let(:conversation) { create(:conversation, account: account) } + let(:action_service) { described_class.new(conversation) } + + it 'adds the sla policy to the conversation and create applied_sla entry' do + action_service.add_sla(sla_policy) + expect(conversation.reload.sla_policy_id).to eq(sla_policy.id) + + # check if appliedsla table entry is created with matching attributes + applied_sla = AppliedSla.last + expect(applied_sla.account_id).to eq(account.id) + expect(applied_sla.sla_policy_id).to eq(sla_policy.id) + expect(applied_sla.conversation_id).to eq(conversation.id) + expect(applied_sla.sla_status).to eq('active') + end + end +end