feat: Clone and update automation rules (#3782)
- endpoints to clone and update automation rules fixes: #3740
This commit is contained in:
parent
ab864a86fd
commit
a7c947aeae
9 changed files with 158 additions and 4 deletions
|
@ -1,5 +1,6 @@
|
||||||
class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseController
|
class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseController
|
||||||
before_action :check_authorization
|
before_action :check_authorization
|
||||||
|
before_action :fetch_automation_rule, only: [:show, :update, :destroy, :clone]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@automation_rules = Current.account.automation_rules.active
|
@automation_rules = Current.account.automation_rules.active
|
||||||
|
@ -9,13 +10,35 @@ class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseCont
|
||||||
@automation_rule = Current.account.automation_rules.create(automation_rules_permit)
|
@automation_rule = Current.account.automation_rules.create(automation_rules_permit)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def show; end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@automation_rule.update(automation_rules_permit)
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@automation_rule.destroy!
|
||||||
|
head :ok
|
||||||
|
end
|
||||||
|
|
||||||
|
def clone
|
||||||
|
automation_rule = Current.account.automation_rules.find_by(id: params[:automation_rule_id])
|
||||||
|
new_rule = automation_rule.dup
|
||||||
|
new_rule.save
|
||||||
|
@automation_rule = new_rule
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def automation_rules_permit
|
def automation_rules_permit
|
||||||
params.permit(
|
params.permit(
|
||||||
:name, :description, :event_name, :account_id,
|
:name, :description, :event_name, :account_id,
|
||||||
conditions: [:attribute_key, :filter_operator, :query_operator, { values: [] }],
|
conditions: [:attribute_key, :filter_operator, :query_operator, { values: [] }],
|
||||||
actions: [:action_name, { action_params: [:intiated_at] }]
|
actions: [:action_name, { action_params: [] }]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_automation_rule
|
||||||
|
@automation_rule = Current.account.automation_rules.find_by(id: params[:id])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,8 +20,8 @@ class AutomationRuleListener < BaseListener
|
||||||
end
|
end
|
||||||
|
|
||||||
def message_created(event_obj)
|
def message_created(event_obj)
|
||||||
conversation = event_obj.data[:conversation]
|
|
||||||
message = event_obj.data[:message]
|
message = event_obj.data[:message]
|
||||||
|
conversation = message.conversation
|
||||||
return unless rule_present?('message_created', conversation)
|
return unless rule_present?('message_created', conversation)
|
||||||
|
|
||||||
@rules.each do |rule|
|
@rules.each do |rule|
|
||||||
|
|
|
@ -6,4 +6,20 @@ class AutomationRulePolicy < ApplicationPolicy
|
||||||
def create?
|
def create?
|
||||||
@account_user.administrator?
|
@account_user.administrator?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
@account_user.administrator?
|
||||||
|
end
|
||||||
|
|
||||||
|
def update?
|
||||||
|
@account_user.administrator?
|
||||||
|
end
|
||||||
|
|
||||||
|
def clone?
|
||||||
|
@account_user.administrator?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
@account_user.administrator?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.partial! 'api/v1/accounts/automation_rules/partials/automation_rule.json.jbuilder', automation_rule: @automation_rule
|
||||||
|
end
|
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.partial! 'api/v1/accounts/automation_rules/partials/automation_rule.json.jbuilder', automation_rule: @automation_rule
|
||||||
|
end
|
|
@ -0,0 +1,3 @@
|
||||||
|
json.payload do
|
||||||
|
json.partial! 'api/v1/accounts/automation_rules/partials/automation_rule.json.jbuilder', automation_rule: @automation_rule
|
||||||
|
end
|
|
@ -52,7 +52,9 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
resources :canned_responses, except: [:show, :edit, :new]
|
resources :canned_responses, except: [:show, :edit, :new]
|
||||||
resources :automation_rules, only: [:create, :index]
|
resources :automation_rules, except: [:edit] do
|
||||||
|
post :clone
|
||||||
|
end
|
||||||
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
|
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
|
||||||
|
|
||||||
namespace :channels do
|
namespace :channels do
|
||||||
|
|
|
@ -145,7 +145,7 @@
|
||||||
"attribute_type": "standard"
|
"attribute_type": "standard"
|
||||||
},
|
},
|
||||||
"content": {
|
"content": {
|
||||||
"attribute_name": "Message Type",
|
"attribute_name": "Message Content",
|
||||||
"input_type": "search_box with name tags/plain text",
|
"input_type": "search_box with name tags/plain text",
|
||||||
"data_type": "text",
|
"data_type": "text",
|
||||||
"filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain" ],
|
"filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain" ],
|
||||||
|
|
|
@ -115,4 +115,108 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'GET /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}' do
|
||||||
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
||||||
|
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
get "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}"
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
it 'returns for automation_rule for account' do
|
||||||
|
expect(account.automation_rules.count).to eq(1)
|
||||||
|
|
||||||
|
get "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
|
||||||
|
headers: administrator.create_new_auth_token
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
body = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
expect(body[:payload]).to be_present
|
||||||
|
expect(body[:payload][:id]).to eq(automation_rule.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}/clone' do
|
||||||
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
||||||
|
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
post "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}/clone"
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
it 'returns for cloned automation_rule for account' do
|
||||||
|
expect(account.automation_rules.count).to eq(1)
|
||||||
|
|
||||||
|
post "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}/clone",
|
||||||
|
headers: administrator.create_new_auth_token
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
body = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
expect(body[:payload]).to be_present
|
||||||
|
expect(body[:payload][:id]).not_to eq(automation_rule.id)
|
||||||
|
expect(account.automation_rules.count).to eq(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'PATCH /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}' do
|
||||||
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
||||||
|
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
patch "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}"
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
it 'returns for cloned automation_rule for account' do
|
||||||
|
params = { name: 'Update name' }
|
||||||
|
expect(account.automation_rules.count).to eq(1)
|
||||||
|
|
||||||
|
patch "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
|
||||||
|
headers: administrator.create_new_auth_token,
|
||||||
|
params: params
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
body = JSON.parse(response.body, symbolize_names: true)
|
||||||
|
expect(body[:payload][:name]).to eq('Update name')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'DELETE /api/v1/accounts/{account.id}/automation_rules/{automation_rule.id}' do
|
||||||
|
let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') }
|
||||||
|
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
delete "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}"
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
it 'delete the automation_rule for account' do
|
||||||
|
expect(account.automation_rules.count).to eq(1)
|
||||||
|
|
||||||
|
delete "/api/v1/accounts/#{account.id}/automation_rules/#{automation_rule.id}",
|
||||||
|
headers: administrator.create_new_auth_token
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(account.automation_rules.count).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue