mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 03:27:52 +00:00
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 <sojan@pepalo.com>
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -89,3 +89,5 @@ class ActionService
|
||||
@conversation.additional_attributes['type'] == 'tweet'
|
||||
end
|
||||
end
|
||||
|
||||
ActionService.include_mod_with('ActionService')
|
||||
|
||||
9
enterprise/app/models/enterprise/automation_rule.rb
Normal file
9
enterprise/app/models/enterprise/automation_rule.rb
Normal file
@@ -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
|
||||
15
enterprise/app/services/enterprise/action_service.rb
Normal file
15
enterprise/app/services/enterprise/action_service.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
23
spec/enterprise/services/enterprise/action_service_spec.rb
Normal file
23
spec/enterprise/services/enterprise/action_service_spec.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user