Skip to content

Commit

Permalink
feat: Execute macro actions, for the conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaswinichile committed Jul 20, 2022
1 parent 0cee42a commit 4f9e285
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 37 deletions.
8 changes: 7 additions & 1 deletion app/controllers/api/v1/accounts/macros_controller.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -34,6 +34,12 @@ def update
end
end

def execute
::MacrosExecutionJob.perform_later(@macro, conversation_id: params[:conversation_id])

head :ok
end

def permitted_params
params.permit(
:name, :account_id, :visibility,
Expand Down
10 changes: 10 additions & 0 deletions app/jobs/macros_execution_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class MacrosExecutionJob < ApplicationJob
queue_as :medium

def perform(macro, conversation_id:)
account = macro.account
conversation = account.conversations.find_by(display_id: conversation_id)

::Macros::ExecutionService.new(macro, account, conversation).perform if conversation.present?
end
end
9 changes: 5 additions & 4 deletions app/models/automation_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -49,7 +50,7 @@ def file_base_data
private

def json_conditions_format
return if conditions.nil?
return if conditions.blank?

attributes = conditions.map { |obj, _| obj['attribute_key'] }
conditions = attributes - CONDITIONS_ATTRS
Expand All @@ -58,9 +59,9 @@ def json_conditions_format
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?
Expand Down
18 changes: 17 additions & 1 deletion app/models/macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -46,4 +51,15 @@ def self.with_visibility(user, params)
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
4 changes: 4 additions & 0 deletions app/policies/macro_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def update?
def destroy?
true
end

def execute?
true
end
end
88 changes: 88 additions & 0 deletions app/services/macros/execution_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class Macros::ExecutionService
def initialize(macro, account, conversation)
@macro = macro
@account = account
@conversation = conversation
Current.executed_by = macro
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 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: "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: { automation_rule_id: @macro.id } }
mb = Messages::MessageBuilder.new(nil, @conversation, params)
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); 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
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@
post :clone
post :attach_file, on: :collection
end
resources :macros, only: [:index, :create, :show, :update, :destroy]
resources :macros, only: [:index, :create, :show, :update, :destroy] do
member do
post :execute
end
end
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
resources :dashboard_apps, only: [:index, :show, :create, :update, :destroy]
namespace :channels do
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20220711090528_create_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def change
t.integer :visibility, default: 0
t.references :created_by, null: false, index: true, foreign_key: { to_table: :users }
t.references :updated_by, null: false, index: true, foreign_key: { to_table: :users }
t.jsonb :actions, null: false, default: '{}'
t.jsonb :actions, null: false, default: {}
t.timestamps
t.index :account_id, name: 'index_macros_on_account_id'
end
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@
t.integer "visibility", default: 0
t.bigint "created_by_id", null: false
t.bigint "updated_by_id", null: false
t.jsonb "actions", default: "{}", null: false
t.jsonb "actions", default: {}, null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_macros_on_account_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@
{
'action_name': :add_label,
'action_params': %w[support priority_customer]
},
{
'action_name': :assign_best_administrator,
'action_params': [1]
},
{
'action_name': :update_additional_attributes,
'action_params': [{ intiated_at: '2021-12-03 17:25:26.844536 +0530' }]
}
]
}
Expand Down Expand Up @@ -283,8 +275,8 @@
],
'actions': [
{
'action_name': :update_additional_attributes,
'action_params': [{ intiated_at: '2021-12-03 17:25:26.844536 +0530' }]
'action_name': :add_label,
'action_params': %w[support priority_customer]
}
]
}
Expand Down
63 changes: 62 additions & 1 deletion spec/controllers/api/v1/accounts/macros_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rails_helper'

RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do
include ActiveJob::TestHelper

let(:account) { create(:account) }
let(:administrator) { create(:user, account: account, role: :administrator) }
let(:agent) { create(:user, account: account, role: :agent) }
Expand Down Expand Up @@ -80,7 +82,7 @@
'action_params': ['Welcome to the chatwoot platform.']
},
{
'action_name': :resolved
'action_name': :resolve_conversation
}
],
visibility: 'global',
Expand Down Expand Up @@ -173,4 +175,63 @@
end
end
end

describe 'POST /api/v1/accounts/{account.id}/macros/{macro.id}/execute' do
let!(:macro) { create(:macro, account: account, created_by: administrator, updated_by: administrator) }
let(:inbox) { create(:inbox, account: account) }
let(:contact) { create(:contact, account: account, identifier: '123') }
let(:conversation) { create(:conversation, inbox: inbox, account: account) }
let(:team) { create(:team, account: account) }
let(:user_1) { create(:user, role: 0) }

before do
create(:team_member, user: user_1, team: team)
create(:account_user, user: user_1, account: account)
macro.update!(actions:
[
{
'action_name' => 'send_email_to_team', 'action_params' => [{
'message' => 'Please pay attention to this conversation, its from high priority customer',
'team_ids' => [team.id]
}]
},
{ 'action_name' => 'assign_team', 'action_params' => [team.id] },
{ 'action_name' => 'add_label', 'action_params' => %w[support priority_customer] },
{ 'action_name' => 'assign_best_agent', 'action_params' => [user_1.id] },
{ 'action_name' => 'mute_conversation', 'action_params' => nil },
{ 'action_name' => 'change_status', 'action_params' => ['snoozed'] },
{ 'action_name' => 'send_message', 'action_params' => ['Send this message.'] }
])
end

context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/macros/#{macro.id}/execute"

expect(response).to have_http_status(:unauthorized)
end
end

context 'when it is an authenticated user' do
it 'execute the macro' do
expect(conversation.status).to eql('open')
expect(conversation.messages).to be_empty
expect(conversation.assignee).to be_nil

perform_enqueued_jobs do
post "/api/v1/accounts/#{account.id}/macros/#{macro.id}/execute",
params: { conversation_id: conversation.display_id },
headers: administrator.create_new_auth_token

expect(response).to have_http_status(:success)
end

conversation.reload

expect(conversation.status).to eql('snoozed')
expect(conversation.messages.last.content).to eq('Send this message.')
expect(conversation.assignee).to eq(user_1)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/automation_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{ 'action_name' => 'assign_team', 'action_params' => [1] },
{ 'action_name' => 'add_label', 'action_params' => %w[support priority_customer] },
{ 'action_name' => 'assign_best_agents', 'action_params' => [1, 2, 3, 4] }
{ 'action_name' => 'assign_best_agent', 'action_params' => [1, 2, 3, 4] }
]
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/listeners/automation_rule_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{ 'action_name' => 'mute_conversation', 'action_params' => nil },
{ 'action_name' => 'change_status', 'action_params' => ['snoozed'] },
{ 'action_name' => 'send_message', 'action_params' => ['Send this message.'] },
{ 'action_name' => 'send_attachments' }
{ 'action_name' => 'send_attachment' }
])
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
automation_rule.files.attach(file)
Expand Down
6 changes: 1 addition & 5 deletions spec/models/automation_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@
action_params: %w[support priority_customer]
},
{
action_name: :assign_best_administrator,
action_name: :assign_best_agent,
action_params: [1]
},
{
action_name: :update_additional_attributes,
action_params: [{ intiated_at: '2021-12-03 17:25:26.844536 +0530' }]
}
]
}.with_indifferent_access
Expand Down
Loading

0 comments on commit 4f9e285

Please sign in to comment.