From 69f55a25b64536f819aa77c9a33de557e8703d19 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Thu, 28 Oct 2021 15:26:20 +0530 Subject: [PATCH] chore: Minor API fixes (#3290) - Log whether web-hook was successful - Inbox update shouldn't need channel params --- app/controllers/api/v1/accounts/inboxes_controller.rb | 6 +++++- lib/webhooks/trigger.rb | 6 ++++-- spec/controllers/api/v1/accounts/inboxes_controller_spec.rb | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/accounts/inboxes_controller.rb b/app/controllers/api/v1/accounts/inboxes_controller.rb index 3f8686499..d09565da3 100644 --- a/app/controllers/api/v1/accounts/inboxes_controller.rb +++ b/app/controllers/api/v1/accounts/inboxes_controller.rb @@ -43,7 +43,11 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController @inbox.update_working_hours(params.permit(working_hours: Inbox::OFFISABLE_ATTRS)[:working_hours]) if params[:working_hours] channel_attributes = get_channel_attributes(@inbox.channel_type) - @inbox.channel.update!(permitted_params(channel_attributes)[:channel]) if permitted_params(channel_attributes)[:channel].present? + + # Inbox update doesn't necessarily need channel attributes + return if permitted_params(channel_attributes)[:channel].blank? + + @inbox.channel.update!(permitted_params(channel_attributes)[:channel]) update_channel_feature_flags end diff --git a/lib/webhooks/trigger.rb b/lib/webhooks/trigger.rb index 49ac6bb09..6ddd9914b 100644 --- a/lib/webhooks/trigger.rb +++ b/lib/webhooks/trigger.rb @@ -1,14 +1,16 @@ class Webhooks::Trigger def self.execute(url, payload) - RestClient::Request.execute( + response = RestClient::Request.execute( method: :post, url: url, payload: payload.to_json, headers: { content_type: :json, accept: :json }, timeout: 5 ) + Rails.logger.info "Performed Request: Code - #{response.code}" rescue *ExceptionList::REST_CLIENT_EXCEPTIONS => e - Rails.logger.info "Exception: invalid webhook url #{url} : #{e.message}" + Rails.logger.error "Exception: invalid webhook url #{url} : #{e.message}" rescue StandardError => e + Rails.logger.error "Exception: invalid webhook url #{url} : #{e.message}" Sentry.capture_exception(e) end end diff --git a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb index 6148c5148..c1b8b7416 100644 --- a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb @@ -324,7 +324,7 @@ RSpec.describe 'Inboxes API', type: :request do context 'when it is an authenticated user' do let(:admin) { create(:user, account: account, role: :administrator) } - let(:valid_params) { { enable_auto_assignment: false, channel: { website_url: 'test.com' } } } + let(:valid_params) { { name: 'new test inbox', enable_auto_assignment: false } } it 'will not update inbox for agent' do agent = create(:user, account: account, role: :agent) @@ -345,6 +345,7 @@ RSpec.describe 'Inboxes API', type: :request do expect(response).to have_http_status(:success) expect(inbox.reload.enable_auto_assignment).to be_falsey + expect(JSON.parse(response.body)['name']).to eq 'new test inbox' end it 'updates api inbox when administrator' do @@ -384,7 +385,6 @@ RSpec.describe 'Inboxes API', type: :request do headers: admin.create_new_auth_token expect(response).to have_http_status(:success) - expect(response.body).to include('test.com') inbox.reload expect(inbox.avatar.attached?).to eq(true) end