feat: Execute macro actions, for the conversation (#5066)
This commit is contained in:
57
app/services/action_service.rb
Normal file
57
app/services/action_service.rb
Normal file
@@ -0,0 +1,57 @@
|
||||
class ActionService
|
||||
def initialize(conversation)
|
||||
@conversation = conversation
|
||||
end
|
||||
|
||||
def mute_conversation(_params)
|
||||
@conversation.mute!
|
||||
end
|
||||
|
||||
def snooze_conversation(_params)
|
||||
@conversation.snoozed!
|
||||
end
|
||||
|
||||
def resolve_conversation(_params)
|
||||
@conversation.resolved!
|
||||
end
|
||||
|
||||
def change_status(status)
|
||||
@conversation.update!(status: status[0])
|
||||
end
|
||||
|
||||
def add_label(labels)
|
||||
return if labels.empty?
|
||||
|
||||
@conversation.add_labels(labels)
|
||||
end
|
||||
|
||||
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_id: @agent.id) if @agent.present?
|
||||
end
|
||||
|
||||
def assign_team(team_ids = [])
|
||||
return unless team_belongs_to_account?(team_ids)
|
||||
|
||||
@conversation.update!(team_id: team_ids[0])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def agent_belongs_to_account?(agent_ids)
|
||||
@account.agents.pluck(:id).include?(agent_ids[0])
|
||||
end
|
||||
|
||||
def team_belongs_to_account?(team_ids)
|
||||
@account.team_ids.include?(team_ids[0])
|
||||
end
|
||||
|
||||
def conversation_a_tweet?
|
||||
return false if @conversation.additional_attributes.blank?
|
||||
|
||||
@conversation.additional_attributes['type'] == 'tweet'
|
||||
end
|
||||
end
|
||||
@@ -1,8 +1,8 @@
|
||||
class AutomationRules::ActionService
|
||||
class AutomationRules::ActionService < ActionService
|
||||
def initialize(rule, account, conversation)
|
||||
super(conversation)
|
||||
@rule = rule
|
||||
@account = account
|
||||
@conversation = conversation
|
||||
Current.executed_by = rule
|
||||
end
|
||||
|
||||
@@ -41,22 +41,6 @@ class AutomationRules::ActionService
|
||||
end
|
||||
end
|
||||
|
||||
def mute_conversation(_params)
|
||||
@conversation.mute!
|
||||
end
|
||||
|
||||
def snooze_conversation(_params)
|
||||
@conversation.snoozed!
|
||||
end
|
||||
|
||||
def resolve_conversation(_params)
|
||||
@conversation.resolved!
|
||||
end
|
||||
|
||||
def change_status(status)
|
||||
@conversation.update!(status: status[0])
|
||||
end
|
||||
|
||||
def send_webhook_event(webhook_url)
|
||||
payload = @conversation.webhook_data.merge(event: "automation_event.#{@rule.event_name}")
|
||||
WebhookJob.perform_later(webhook_url[0], payload)
|
||||
@@ -70,26 +54,6 @@ class AutomationRules::ActionService
|
||||
mb.perform
|
||||
end
|
||||
|
||||
def assign_team(team_ids = [])
|
||||
return unless team_belongs_to_account?(team_ids)
|
||||
|
||||
@conversation.update!(team_id: team_ids[0])
|
||||
end
|
||||
|
||||
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_id: @agent.id) if @agent.present?
|
||||
end
|
||||
|
||||
def add_label(labels)
|
||||
return if labels.empty?
|
||||
|
||||
@conversation.add_labels(labels)
|
||||
end
|
||||
|
||||
def send_email_to_team(params)
|
||||
teams = Team.where(id: params[0][:team_ids])
|
||||
|
||||
@@ -97,18 +61,4 @@ class AutomationRules::ActionService
|
||||
TeamNotifications::AutomationNotificationMailer.conversation_creation(@conversation, team, params[0][:message])&.deliver_now
|
||||
end
|
||||
end
|
||||
|
||||
def agent_belongs_to_account?(agent_ids)
|
||||
@account.agents.pluck(:id).include?(agent_ids[0])
|
||||
end
|
||||
|
||||
def team_belongs_to_account?(team_ids)
|
||||
@account.team_ids.include?(team_ids[0])
|
||||
end
|
||||
|
||||
def conversation_a_tweet?
|
||||
return false if @conversation.additional_attributes.blank?
|
||||
|
||||
@conversation.additional_attributes['type'] == 'tweet'
|
||||
end
|
||||
end
|
||||
|
||||
38
app/services/macros/execution_service.rb
Normal file
38
app/services/macros/execution_service.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
class Macros::ExecutionService < ActionService
|
||||
def initialize(macro, conversation, user)
|
||||
super(conversation)
|
||||
@macro = macro
|
||||
@account = macro.account
|
||||
Current.user = user
|
||||
end
|
||||
|
||||
def perform
|
||||
@macro.actions.each do |action|
|
||||
action = action.with_indifferent_access
|
||||
begin
|
||||
send(action[:action_name], action[:action_params])
|
||||
rescue StandardError => e
|
||||
ChatwootExceptionTracker.new(e, account: @account).capture_exception
|
||||
end
|
||||
end
|
||||
ensure
|
||||
Current.reset
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_webhook_event(webhook_url)
|
||||
payload = @conversation.webhook_data.merge(event: "macro_event.#{@macro.name}")
|
||||
WebhookJob.perform_later(webhook_url[0], payload)
|
||||
end
|
||||
|
||||
def send_message(message)
|
||||
return if conversation_a_tweet?
|
||||
|
||||
params = { content: message[0], private: false, content_attributes: { macro_id: @macro.id } }
|
||||
mb = Messages::MessageBuilder.new(nil, @conversation, params)
|
||||
mb.perform
|
||||
end
|
||||
|
||||
def send_email_to_team(_params); end
|
||||
end
|
||||
Reference in New Issue
Block a user