feat: Save automation rules (#3359)

This commit is contained in:
Tejaswini Chile
2022-01-10 12:41:59 +05:30
committed by GitHub
parent 9a9462f5cb
commit a0884310f4
24 changed files with 746 additions and 3 deletions

View File

@@ -0,0 +1,118 @@
require 'rails_helper'
RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
let(:account) { create(:account) }
let(:administrator) { create(:user, account: account, role: :administrator) }
let!(:inbox) { create(:inbox, account: account, enable_auto_assignment: false) }
let!(:contact) { create(:contact, account: account) }
let(:contact_inbox) { create(:contact_inbox, inbox_id: inbox.id, contact_id: contact.id) }
describe 'GET /api/v1/accounts/{account.id}/automation_rules' do
context 'when it is an authenticated user' do
it 'returns all records' do
automation_rule = create(:automation_rule, account: account, name: 'Test Automation Rule')
get "/api/v1/accounts/#{account.id}/automation_rules",
headers: administrator.create_new_auth_token
expect(response).to have_http_status(:success)
body = JSON.parse(response.body, symbolize_names: true)
expect(body[:data].first[:id]).to eq(automation_rule.id)
end
end
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/automation_rules"
expect(response).to have_http_status(:unauthorized)
end
end
end
describe 'POST /api/v1/accounts/{account.id}/automation_rules' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post "/api/v1/accounts/#{account.id}/automation_rules"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:params) do
{
name: 'Notify Conversation Created and mark priority query',
description: 'Notify all administrator about conversation created and mark priority query',
event_name: 'conversation_created',
conditions: [
{
attribute_key: 'browser_language',
filter_operator: 'equal_to',
values: ['en'],
query_operator: 'AND'
},
{
attribute_key: 'country_code',
filter_operator: 'equal_to',
values: %w[USA UK],
query_operator: nil
}
],
actions: [
{
action_name: :send_message,
action_params: ['Welcome to the chatwoot platform.']
},
{
action_name: :assign_team,
action_params: [1]
},
{
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' }]
}
]
}.with_indifferent_access
end
it 'Saves for automation_rules for account with country_code and browser_language conditions' do
expect(account.automation_rules.count).to eq(0)
post "/api/v1/accounts/#{account.id}/automation_rules",
headers: administrator.create_new_auth_token,
params: params
expect(response).to have_http_status(:success)
expect(account.automation_rules.count).to eq(1)
end
it 'Saves for automation_rules for account with status conditions' do
params[:conditions] = [
{
attribute_key: 'status',
filter_operator: 'equal_to',
values: ['resolved'],
query_operator: nil
}.with_indifferent_access
]
expect(account.automation_rules.count).to eq(0)
post "/api/v1/accounts/#{account.id}/automation_rules",
headers: administrator.create_new_auth_token,
params: params
expect(response).to have_http_status(:success)
expect(account.automation_rules.count).to eq(1)
end
end
end
end

View File

@@ -0,0 +1,19 @@
FactoryBot.define do
factory :automation_rule do
account
event_name { 'conversation_status_changed' }
conditions { [{ 'values': ['resolved'], 'attribute_key': 'status', 'query_operator': nil, 'filter_operator': 'equal_to' }] }
actions do
[
{
'action_name' => 'send_email_to_team', 'action_params' => {
'message' => 'Please pay attention to this conversation, its from high priority customer', 'team_ids' => [1]
}
},
{ '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] }
]
end
end
end

View File

@@ -0,0 +1,72 @@
require 'rails_helper'
describe AutomationRuleListener do
let(:listener) { described_class.instance }
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
let(:contact) { create(:contact, account: account, identifier: '123') }
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
let(:conversation) { create(:conversation, contact_inbox: contact_inbox, inbox: inbox, account: account) }
let(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
let(:team) { create(:team, account: account) }
let(:user_1) { create(:user, role: 0) }
let(:user_2) { create(:user, role: 0) }
let!(:event) do
Events::Base.new('conversation_status_changed', Time.zone.now, { conversation: conversation })
end
before do
create(:team_member, user: user_1, team: team)
create(:team_member, user: user_2, team: team)
create(:account_user, user: user_2, account: account)
create(:account_user, user: user_1, account: account)
conversation.resolved!
automation_rule.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_agents', 'action_params' => [user_1.id] }
])
end
describe '#conversation_status_changed' do
context 'when rule matches' do
it 'triggers automation rule to assign team' do
expect(conversation.team_id).not_to eq(team.id)
automation_rule
listener.conversation_status_changed(event)
conversation.reload
expect(conversation.team_id).to eq(team.id)
end
it 'triggers automation rule to add label' do
expect(conversation.labels).to eq([])
automation_rule
listener.conversation_status_changed(event)
conversation.reload
expect(conversation.labels.pluck(:name)).to eq(%w[support priority_customer])
end
it 'triggers automation rule to assign best agents' do
expect(conversation.assignee).to be_nil
automation_rule
listener.conversation_status_changed(event)
conversation.reload
expect(conversation.assignee).to eq(user_1)
end
end
end
end

View File

@@ -0,0 +1,54 @@
require 'rails_helper'
RSpec.describe AutomationRule, type: :model do
describe 'associations' do
let(:params) do
{
name: 'Notify Conversation Created and mark priority query',
description: 'Notify all administrator about conversation created and mark priority query',
event_name: 'conversation_created',
conditions: [
{
attribute_key: 'browser_language',
filter_operator: 'equal_to',
values: ['en'],
query_operator: 'AND'
},
{
attribute_key: 'country_code',
filter_operator: 'equal_to',
values: %w[USA UK],
query_operator: nil
}
],
actions: [
{
action_name: :send_message,
action_params: ['Welcome to the chatwoot platform.']
},
{
action_name: :assign_team,
action_params: [1]
},
{
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' }]
}
]
}.with_indifferent_access
end
it 'returns valid record' do
rule = FactoryBot.build(:automation_rule, params)
expect(rule.valid?).to eq true
end
end
end