Automation enhancement (#4087)
This commit is contained in:
@@ -1,40 +1,68 @@
|
||||
class AutomationRules::ActionService
|
||||
def initialize(rule, conversation)
|
||||
def initialize(rule, account, conversation)
|
||||
@rule = rule
|
||||
@account = account
|
||||
@conversation = conversation
|
||||
@account = @conversation.account
|
||||
Current.executed_by = rule
|
||||
end
|
||||
|
||||
def perform
|
||||
@rule.actions.each do |action, _current_index|
|
||||
@rule.actions.each do |action|
|
||||
action = action.with_indifferent_access
|
||||
send(action[:action_name], action[:action_params])
|
||||
begin
|
||||
send(action[:action_name], action[:action_params])
|
||||
rescue StandardError => e
|
||||
Sentry.capture_exception(e)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
Current.reset
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_email_transcript(email)
|
||||
ConversationReplyMailer.with(account: conversation.account).conversation_transcript(@conversation, email)&.deliver_later
|
||||
end
|
||||
|
||||
def mute_conversation(_params)
|
||||
@conversation.mute!
|
||||
end
|
||||
|
||||
def change_status(status)
|
||||
@conversation.update!(status: status[0])
|
||||
end
|
||||
|
||||
def send_webhook_events(webhook_url)
|
||||
payload = @conversation.webhook_data.merge(event: "automation_event: #{@rule.event_name}")
|
||||
WebhookJob.perform_later(webhook_url, payload)
|
||||
end
|
||||
|
||||
def send_message(message)
|
||||
# params = { content: message, private: false }
|
||||
# mb = Messages::MessageBuilder.new(@administrator, @conversation, params)
|
||||
# mb.perform
|
||||
return if @rule.event_name == 'message_created'
|
||||
|
||||
params = { content: message[0], private: false }
|
||||
mb = Messages::MessageBuilder.new(@administrator, @conversation, params)
|
||||
mb.perform
|
||||
end
|
||||
|
||||
def assign_team(team_ids = [])
|
||||
return unless team_belongs_to_account?(team_ids)
|
||||
|
||||
@account.teams.find_by(id: team_ids)
|
||||
@conversation.update!(team_id: team_ids[0])
|
||||
end
|
||||
|
||||
def assign_best_agents(agent_ids = [])
|
||||
def assign_best_agent(agent_ids = [])
|
||||
return unless agent_belongs_to_account?(agent_ids)
|
||||
|
||||
@agent = @account.users.find_by(id: agent_ids)
|
||||
@conversation.update_assignee(@agent)
|
||||
|
||||
@conversation.update!(assignee_id: @agent.id) if @agent.present?
|
||||
end
|
||||
|
||||
def add_label(labels = [])
|
||||
def add_label(labels)
|
||||
return if labels.empty?
|
||||
|
||||
@conversation.add_labels(labels)
|
||||
end
|
||||
|
||||
@@ -43,11 +71,11 @@ class AutomationRules::ActionService
|
||||
|
||||
case @rule.event_name
|
||||
when 'conversation_created', 'conversation_status_changed'
|
||||
TeamNotifications::AutomationNotificationMailer.conversation_creation(@conversation, team, params[:message])
|
||||
TeamNotifications::AutomationNotificationMailer.conversation_creation(@conversation, team, params[:message])&.deliver_now
|
||||
when 'conversation_updated'
|
||||
TeamNotifications::AutomationNotificationMailer.conversation_updated(@conversation, team, params[:message])
|
||||
TeamNotifications::AutomationNotificationMailer.conversation_updated(@conversation, team, params[:message])&.deliver_now
|
||||
when 'message_created'
|
||||
TeamNotifications::AutomationNotificationMailer.message_created(@conversation, team, params[:message])
|
||||
TeamNotifications::AutomationNotificationMailer.message_created(@conversation, team, params[:message])&.deliver_now
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'json'
|
||||
|
||||
class AutomationRules::ConditionsFilterService < FilterService
|
||||
def initialize(rule, conversation)
|
||||
def initialize(rule, conversation = nil)
|
||||
super([], nil)
|
||||
@rule = rule
|
||||
@conversation = conversation
|
||||
@@ -21,14 +21,14 @@ class AutomationRules::ConditionsFilterService < FilterService
|
||||
records.any?
|
||||
end
|
||||
|
||||
def message_conditions(message)
|
||||
def message_conditions
|
||||
message_filters = @filters['messages']
|
||||
|
||||
@rule.conditions.each_with_index do |query_hash, current_index|
|
||||
current_filter = message_filters[query_hash['attribute_key']]
|
||||
@query_string += message_query_string(current_filter, query_hash.with_indifferent_access, current_index)
|
||||
end
|
||||
records = Message.where(id: message.id).where(@query_string, @filter_values.with_indifferent_access)
|
||||
records = Message.where(conversation: @conversation).where(@query_string, @filter_values.with_indifferent_access)
|
||||
records.any?
|
||||
end
|
||||
|
||||
@@ -44,6 +44,19 @@ class AutomationRules::ConditionsFilterService < FilterService
|
||||
end
|
||||
end
|
||||
|
||||
# This will be used in future for contact automation rule
|
||||
def contact_conditions(_contact)
|
||||
conversation_filters = @filters['conversations']
|
||||
|
||||
@rule.conditions.each_with_index do |query_hash, current_index|
|
||||
current_filter = conversation_filters[query_hash['attribute_key']]
|
||||
@query_string += conversation_query_string(current_filter, query_hash.with_indifferent_access, current_index)
|
||||
end
|
||||
|
||||
records = base_relation.where(@query_string, @filter_values.with_indifferent_access)
|
||||
records.any?
|
||||
end
|
||||
|
||||
def conversation_query_string(current_filter, query_hash, current_index)
|
||||
attribute_key = query_hash['attribute_key']
|
||||
query_operator = query_hash['query_operator']
|
||||
@@ -63,6 +76,6 @@ class AutomationRules::ConditionsFilterService < FilterService
|
||||
end
|
||||
|
||||
def base_relation
|
||||
Conversation.where(id: @conversation)
|
||||
Conversation.where(id: @conversation.id)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user