fix: Reauthorize when channel settings updated (#7134)
Fixes: https://linear.app/chatwoot/issue/CW-1803/bug-inbox-doesnt-update-reauthorisation-required-automattically
This commit is contained in:
@@ -44,26 +44,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
||||
def update
|
||||
@inbox.update!(permitted_params.except(:channel))
|
||||
update_inbox_working_hours
|
||||
channel_attributes = get_channel_attributes(@inbox.channel_type)
|
||||
|
||||
# Inbox update doesn't necessarily need channel attributes
|
||||
return if permitted_params(channel_attributes)[:channel].blank?
|
||||
|
||||
if @inbox.inbox_type == 'Email'
|
||||
begin
|
||||
validate_email_channel(channel_attributes)
|
||||
rescue StandardError => e
|
||||
render json: { message: e }, status: :unprocessable_entity and return
|
||||
end
|
||||
@inbox.channel.reauthorized!
|
||||
end
|
||||
|
||||
@inbox.channel.update!(permitted_params(channel_attributes)[:channel])
|
||||
update_channel_feature_flags
|
||||
end
|
||||
|
||||
def update_inbox_working_hours
|
||||
@inbox.update_working_hours(params.permit(working_hours: Inbox::OFFISABLE_ATTRS)[:working_hours]) if params[:working_hours]
|
||||
update_channel if channel_update_required?
|
||||
end
|
||||
|
||||
def agent_bot
|
||||
@@ -103,6 +84,35 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
||||
account_channels_method.create!(permitted_params(channel_type_from_params::EDITABLE_ATTRS)[:channel].except(:type))
|
||||
end
|
||||
|
||||
def update_inbox_working_hours
|
||||
@inbox.update_working_hours(params.permit(working_hours: Inbox::OFFISABLE_ATTRS)[:working_hours]) if params[:working_hours]
|
||||
end
|
||||
|
||||
def update_channel
|
||||
channel_attributes = get_channel_attributes(@inbox.channel_type)
|
||||
return if permitted_params(channel_attributes)[:channel].blank?
|
||||
|
||||
validate_and_update_email_channel(channel_attributes) if @inbox.inbox_type == 'Email'
|
||||
|
||||
reauthorize_and_update_channel(channel_attributes)
|
||||
update_channel_feature_flags
|
||||
end
|
||||
|
||||
def channel_update_required?
|
||||
permitted_params(get_channel_attributes(@inbox.channel_type))[:channel].present?
|
||||
end
|
||||
|
||||
def validate_and_update_email_channel(channel_attributes)
|
||||
validate_email_channel(channel_attributes)
|
||||
rescue StandardError => e
|
||||
render json: { message: e }, status: :unprocessable_entity and return
|
||||
end
|
||||
|
||||
def reauthorize_and_update_channel(channel_attributes)
|
||||
@inbox.channel.reauthorized! if @inbox.channel.respond_to?(:reauthorized!)
|
||||
@inbox.channel.update!(permitted_params(channel_attributes)[:channel])
|
||||
end
|
||||
|
||||
def update_channel_feature_flags
|
||||
return unless @inbox.web_widget?
|
||||
return unless permitted_params(Channel::WebWidget::EDITABLE_ATTRS)[:channel].key? :selected_feature_flags
|
||||
|
||||
@@ -469,17 +469,37 @@ RSpec.describe 'Inboxes API', type: :request do
|
||||
expect(api_channel.reload.webhook_url).to eq('webhook.test')
|
||||
end
|
||||
|
||||
it 'updates twitter inbox when administrator' do
|
||||
api_channel = create(:channel_twitter_profile, account: account, tweets_enabled: true)
|
||||
api_inbox = create(:inbox, channel: api_channel, account: account)
|
||||
it 'updates whatsapp inbox when administrator' do
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/configs/webhook').to_return(status: 200, body: '', headers: {})
|
||||
stub_request(:get, 'https://waba.360dialog.io/v1/configs/templates').to_return(status: 200, body: '', headers: {})
|
||||
whatsapp_channel = create(:channel_whatsapp, account: account)
|
||||
whatsapp_inbox = create(:inbox, channel: whatsapp_channel, account: account)
|
||||
whatsapp_channel.prompt_reauthorization!
|
||||
|
||||
patch "/api/v1/accounts/#{account.id}/inboxes/#{api_inbox.id}",
|
||||
expect(whatsapp_channel).to be_reauthorization_required
|
||||
|
||||
patch "/api/v1/accounts/#{account.id}/inboxes/#{whatsapp_inbox.id}",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { enable_auto_assignment: false, channel: { provider_config: { api_key: 'new_key' } } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(whatsapp_inbox.reload.enable_auto_assignment).to be_falsey
|
||||
expect(whatsapp_channel.reload.provider_config['api_key']).to eq('new_key')
|
||||
expect(whatsapp_channel.reload).not_to be_reauthorization_required
|
||||
end
|
||||
|
||||
it 'updates twitter inbox when administrator' do
|
||||
twitter_channel = create(:channel_twitter_profile, account: account, tweets_enabled: true)
|
||||
twitter_inbox = create(:inbox, channel: twitter_channel, account: account)
|
||||
|
||||
patch "/api/v1/accounts/#{account.id}/inboxes/#{twitter_inbox.id}",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { channel: { tweets_enabled: false } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(api_channel.reload.tweets_enabled).to be(false)
|
||||
expect(twitter_channel.reload.tweets_enabled).to be(false)
|
||||
end
|
||||
|
||||
it 'updates email inbox when administrator' do
|
||||
|
||||
Reference in New Issue
Block a user