feat: Execute macro actions, for the conversation (#5066)

This commit is contained in:
Tejaswini Chile
2022-07-26 12:41:22 +05:30
committed by GitHub
parent bd7a56061e
commit 6a4c0a1578
17 changed files with 232 additions and 89 deletions

View File

@@ -1,6 +1,6 @@
class Api::V1::Accounts::MacrosController < Api::V1::Accounts::BaseController
before_action :check_authorization
before_action :fetch_macro, only: [:show, :update, :destroy]
before_action :fetch_macro, only: [:show, :update, :destroy, :execute]
def index
@macros = Macro.with_visibility(current_user, params)
@@ -34,6 +34,12 @@ class Api::V1::Accounts::MacrosController < Api::V1::Accounts::BaseController
end
end
def execute
::MacrosExecutionJob.perform_later(@macro, conversation_ids: params[:conversation_ids], user: Current.user)
head :ok
end
def permitted_params
params.permit(
:name, :account_id, :visibility,

View File

@@ -0,0 +1,14 @@
class MacrosExecutionJob < ApplicationJob
queue_as :medium
def perform(macro, conversation_ids:, user:)
account = macro.account
conversations = account.conversations.where(display_id: conversation_ids.to_a)
return if conversations.blank?
conversations.each do |conversation|
::Macros::ExecutionService.new(macro, conversation, user).perform
end
end
end

View File

@@ -30,7 +30,8 @@ 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].freeze
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_best_agents send_attachment].freeze
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_best_agent send_webhook_event mute_conversation send_attachment
change_status resolve_conversation snooze_conversation send_email_transcript].freeze
def file_base_data
files.map do |file|
@@ -49,7 +50,7 @@ class AutomationRule < ApplicationRecord
private
def json_conditions_format
return if conditions.nil?
return if conditions.blank?
attributes = conditions.map { |obj, _| obj['attribute_key'] }
conditions = attributes - CONDITIONS_ATTRS
@@ -58,9 +59,9 @@ class AutomationRule < ApplicationRecord
end
def json_actions_format
return if actions.nil?
return if actions.blank?
attributes = actions.map { |obj, _| obj['attribute_key'] }
attributes = actions.map { |obj, _| obj['action_name'] }
actions = attributes - ACTIONS_ATTRS
errors.add(:actions, "Automation actions #{actions.join(',')} not supported.") if actions.any?

View File

@@ -5,7 +5,7 @@
# id :bigint not null, primary key
# actions :jsonb not null
# name :string not null
# visibility :integer default("user")
# visibility :integer default("personal")
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
@@ -31,6 +31,11 @@ class Macro < ApplicationRecord
class_name: :User
enum visibility: { personal: 0, global: 1 }
validate :json_actions_format
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_best_agent send_webhook_event mute_conversation change_status
resolve_conversation snooze_conversation].freeze
def set_visibility(user, params)
self.visibility = params[:visibility]
self.visibility = :personal if user.agent?
@@ -46,4 +51,15 @@ class Macro < ApplicationRecord
def self.current_page(params)
params[:page] || 1
end
private
def json_actions_format
return if actions.blank?
attributes = actions.map { |obj, _| obj['action_name'] }
actions = attributes - ACTIONS_ATTRS
errors.add(:actions, "Macro execution actions #{actions.join(',')} not supported.") if actions.any?
end
end

View File

@@ -18,4 +18,8 @@ class MacroPolicy < ApplicationPolicy
def destroy?
true
end
def execute?
true
end
end

View 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

View File

@@ -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

View 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