2022-01-10 07:11:59 +00:00
|
|
|
class AutomationRules::ActionService
|
2022-03-21 07:42:27 +00:00
|
|
|
def initialize(rule, account, conversation)
|
2022-01-10 07:11:59 +00:00
|
|
|
@rule = rule
|
2022-03-21 07:42:27 +00:00
|
|
|
@account = account
|
2022-01-10 07:11:59 +00:00
|
|
|
@conversation = conversation
|
2022-03-21 07:42:27 +00:00
|
|
|
Current.executed_by = rule
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2022-03-21 07:42:27 +00:00
|
|
|
@rule.actions.each do |action|
|
2022-01-10 07:11:59 +00:00
|
|
|
action = action.with_indifferent_access
|
2022-03-21 07:42:27 +00:00
|
|
|
begin
|
|
|
|
send(action[:action_name], action[:action_params])
|
|
|
|
rescue StandardError => e
|
|
|
|
Sentry.capture_exception(e)
|
|
|
|
end
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
2022-03-21 07:42:27 +00:00
|
|
|
ensure
|
|
|
|
Current.reset
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-29 07:57:16 +00:00
|
|
|
def send_email_transcript(emails)
|
|
|
|
emails.each do |email|
|
|
|
|
ConversationReplyMailer.with(account: @conversation.account).conversation_transcript(@conversation, email)&.deliver_later
|
|
|
|
end
|
2022-03-21 07:42:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mute_conversation(_params)
|
|
|
|
@conversation.mute!
|
|
|
|
end
|
|
|
|
|
2022-03-29 07:57:16 +00:00
|
|
|
def snooze_conversation(_params)
|
|
|
|
@conversation.ensure_snooze_until_reset
|
|
|
|
end
|
|
|
|
|
2022-03-21 07:42:27 +00:00
|
|
|
def change_status(status)
|
|
|
|
@conversation.update!(status: status[0])
|
|
|
|
end
|
|
|
|
|
2022-03-29 07:57:16 +00:00
|
|
|
def send_webhook_event(webhook_url)
|
2022-03-21 07:42:27 +00:00
|
|
|
payload = @conversation.webhook_data.merge(event: "automation_event: #{@rule.event_name}")
|
2022-03-29 07:57:16 +00:00
|
|
|
WebhookJob.perform_later(webhook_url[0], payload)
|
2022-03-21 07:42:27 +00:00
|
|
|
end
|
|
|
|
|
2022-01-10 07:11:59 +00:00
|
|
|
def send_message(message)
|
2022-03-21 07:42:27 +00:00
|
|
|
return if @rule.event_name == 'message_created'
|
|
|
|
|
|
|
|
params = { content: message[0], private: false }
|
|
|
|
mb = Messages::MessageBuilder.new(@administrator, @conversation, params)
|
|
|
|
mb.perform
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def assign_team(team_ids = [])
|
|
|
|
return unless team_belongs_to_account?(team_ids)
|
|
|
|
|
|
|
|
@conversation.update!(team_id: team_ids[0])
|
|
|
|
end
|
|
|
|
|
2022-03-21 07:42:27 +00:00
|
|
|
def assign_best_agent(agent_ids = [])
|
2022-01-10 07:11:59 +00:00
|
|
|
return unless agent_belongs_to_account?(agent_ids)
|
|
|
|
|
|
|
|
@agent = @account.users.find_by(id: agent_ids)
|
2022-03-21 07:42:27 +00:00
|
|
|
|
|
|
|
@conversation.update!(assignee_id: @agent.id) if @agent.present?
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
|
2022-03-21 07:42:27 +00:00
|
|
|
def add_label(labels)
|
|
|
|
return if labels.empty?
|
|
|
|
|
2022-01-10 07:11:59 +00:00
|
|
|
@conversation.add_labels(labels)
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_email_to_team(params)
|
|
|
|
team = Team.find(params[:team_ids][0])
|
|
|
|
|
|
|
|
case @rule.event_name
|
|
|
|
when 'conversation_created', 'conversation_status_changed'
|
2022-03-21 07:42:27 +00:00
|
|
|
TeamNotifications::AutomationNotificationMailer.conversation_creation(@conversation, team, params[:message])&.deliver_now
|
2022-01-10 07:11:59 +00:00
|
|
|
when 'conversation_updated'
|
2022-03-21 07:42:27 +00:00
|
|
|
TeamNotifications::AutomationNotificationMailer.conversation_updated(@conversation, team, params[:message])&.deliver_now
|
2022-01-13 05:51:06 +00:00
|
|
|
when 'message_created'
|
2022-03-21 07:42:27 +00:00
|
|
|
TeamNotifications::AutomationNotificationMailer.message_created(@conversation, team, params[:message])&.deliver_now
|
2022-01-10 07:11:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def administrator
|
|
|
|
@administrator ||= @account.administrators.first
|
|
|
|
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
|
|
|
|
end
|