diff --git a/app/controllers/api/v1/accounts/automation_rules_controller.rb b/app/controllers/api/v1/accounts/automation_rules_controller.rb index 723e587e6..12b7b5957 100644 --- a/app/controllers/api/v1/accounts/automation_rules_controller.rb +++ b/app/controllers/api/v1/accounts/automation_rules_controller.rb @@ -1,5 +1,6 @@ class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseController before_action :check_authorization + before_action :fetch_automation_rule, only: [:show, :update, :destroy, :clone] def index @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) 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 def automation_rules_permit params.permit( :name, :description, :event_name, :account_id, conditions: [:attribute_key, :filter_operator, :query_operator, { values: [] }], - actions: [:action_name, { action_params: [:intiated_at] }] + actions: [:action_name, { action_params: [] }] ) end + + def fetch_automation_rule + @automation_rule = Current.account.automation_rules.find_by(id: params[:id]) + end end diff --git a/app/listeners/automation_rule_listener.rb b/app/listeners/automation_rule_listener.rb index 7b64a0b3a..cfd97e560 100644 --- a/app/listeners/automation_rule_listener.rb +++ b/app/listeners/automation_rule_listener.rb @@ -20,8 +20,8 @@ class AutomationRuleListener < BaseListener end def message_created(event_obj) - conversation = event_obj.data[:conversation] message = event_obj.data[:message] + conversation = message.conversation return unless rule_present?('message_created', conversation) @rules.each do |rule| diff --git a/app/policies/automation_rule_policy.rb b/app/policies/automation_rule_policy.rb index bd644f075..431b32964 100644 --- a/app/policies/automation_rule_policy.rb +++ b/app/policies/automation_rule_policy.rb @@ -6,4 +6,20 @@ class AutomationRulePolicy < ApplicationPolicy def create? @account_user.administrator? 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 diff --git a/app/views/api/v1/accounts/automation_rules/clone.json.jbuilder b/app/views/api/v1/accounts/automation_rules/clone.json.jbuilder new file mode 100644 index 000000000..1a6c02187 --- /dev/null +++ b/app/views/api/v1/accounts/automation_rules/clone.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.partial! 'api/v1/accounts/automation_rules/partials/automation_rule.json.jbuilder', automation_rule: @automation_rule +end diff --git a/app/views/api/v1/accounts/automation_rules/show.json.jbuilder b/app/views/api/v1/accounts/automation_rules/show.json.jbuilder new file mode 100644 index 000000000..1a6c02187 --- /dev/null +++ b/app/views/api/v1/accounts/automation_rules/show.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.partial! 'api/v1/accounts/automation_rules/partials/automation_rule.json.jbuilder', automation_rule: @automation_rule +end diff --git a/app/views/api/v1/accounts/automation_rules/update.json.jbuilder b/app/views/api/v1/accounts/automation_rules/update.json.jbuilder new file mode 100644 index 000000000..1a6c02187 --- /dev/null +++ b/app/views/api/v1/accounts/automation_rules/update.json.jbuilder @@ -0,0 +1,3 @@ +json.payload do + json.partial! 'api/v1/accounts/automation_rules/partials/automation_rule.json.jbuilder', automation_rule: @automation_rule +end diff --git a/config/routes.rb b/config/routes.rb index 493810c03..aaf0330ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,7 +52,9 @@ Rails.application.routes.draw do end end 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] namespace :channels do diff --git a/lib/filters/filter_keys.json b/lib/filters/filter_keys.json index a34dad067..2aa5fdab1 100644 --- a/lib/filters/filter_keys.json +++ b/lib/filters/filter_keys.json @@ -145,7 +145,7 @@ "attribute_type": "standard" }, "content": { - "attribute_name": "Message Type", + "attribute_name": "Message Content", "input_type": "search_box with name tags/plain text", "data_type": "text", "filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain" ], diff --git a/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb b/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb index dda022839..5755bf95a 100644 --- a/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/automation_rules_controller_spec.rb @@ -115,4 +115,108 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do 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