diff --git a/app/controllers/api/v1/accounts/portals_controller.rb b/app/controllers/api/v1/accounts/portals_controller.rb index 6d3332d08..27c4126a6 100644 --- a/app/controllers/api/v1/accounts/portals_controller.rb +++ b/app/controllers/api/v1/accounts/portals_controller.rb @@ -18,7 +18,7 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController @portal = Current.account.portals.build(portal_params.merge(live_chat_widget_params)) @portal.custom_domain = parsed_custom_domain @portal.save! - process_attached_logo + process_attached_logo if params[:blob_id].present? end def update @@ -98,6 +98,8 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController end def parsed_custom_domain + return @portal.custom_domain if @portal.custom_domain.blank? + domain = URI.parse(@portal.custom_domain) domain.is_a?(URI::HTTP) ? domain.host : @portal.custom_domain end diff --git a/spec/controllers/api/v1/accounts/portals_controller_spec.rb b/spec/controllers/api/v1/accounts/portals_controller_spec.rb index 4c45e4f0f..81240c557 100644 --- a/spec/controllers/api/v1/accounts/portals_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/portals_controller_spec.rb @@ -100,6 +100,41 @@ RSpec.describe 'Api::V1::Accounts::Portals', type: :request do expect(json_response['name']).to eql('test_portal') expect(json_response['custom_domain']).to eql('support.chatwoot.dev') end + + it 'creates portal when custom_domain is omitted from request body' do + portal_params = { + portal: { + name: 'test_portal_no_domain', + slug: 'test_kbase_no_domain' + } + } + post "/api/v1/accounts/#{account.id}/portals", + params: portal_params, + headers: admin.create_new_auth_token + + expect(response).to have_http_status(:success) + json_response = response.parsed_body + expect(json_response['name']).to eql('test_portal_no_domain') + expect(json_response['custom_domain']).to be_nil + end + + it 'creates portal when custom_domain is blank' do + portal_params = { + portal: { + name: 'test_portal_blank_domain', + slug: 'test_kbase_blank_domain', + custom_domain: '' + } + } + post "/api/v1/accounts/#{account.id}/portals", + params: portal_params, + headers: admin.create_new_auth_token + + expect(response).to have_http_status(:success) + json_response = response.parsed_body + expect(json_response['name']).to eql('test_portal_blank_domain') + expect(json_response['custom_domain']).to be_blank + end end end