diff --git a/app/controllers/api/v1/accounts/agents_controller.rb b/app/controllers/api/v1/accounts/agents_controller.rb index 221c96b85..eff9975f7 100644 --- a/app/controllers/api/v1/accounts/agents_controller.rb +++ b/app/controllers/api/v1/accounts/agents_controller.rb @@ -49,6 +49,11 @@ class Api::V1::Accounts::AgentsController < Api::V1::Accounts::BaseController Rails.logger.info "[Agent#bulk_create] ignoring email #{email}, errors: #{e.record.errors}" end end + + # This endpoint is used to bulk create agents during onboarding + # onboarding_step key in present in Current account custom attributes, since this is a one time operation + Current.account.custom_attributes.delete('onboarding_step') + Current.account.save! head :ok end diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 481594ee4..c0dac6d9e 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -46,6 +46,7 @@ class Api::V1::AccountsController < Api::BaseController def update @account.assign_attributes(account_params.slice(:name, :locale, :domain, :support_email, :auto_resolve_duration)) @account.custom_attributes.merge!(custom_attributes_params) + @account.custom_attributes['onboarding_step'] = 'invite_team' if @account.custom_attributes['onboarding_step'] == 'account_update' @account.save! end diff --git a/app/controllers/api/v2/accounts_controller.rb b/app/controllers/api/v2/accounts_controller.rb index ea19727ad..45faca3d0 100644 --- a/app/controllers/api/v2/accounts_controller.rb +++ b/app/controllers/api/v2/accounts_controller.rb @@ -22,6 +22,7 @@ class Api::V2::AccountsController < Api::BaseController ).perform fetch_account_and_user_info + update_account_info if @account.present? if @user send_auth_headers(@user) @@ -33,6 +34,18 @@ class Api::V2::AccountsController < Api::BaseController private + def account_attributes + { + custom_attributes: @account.custom_attributes.merge({ 'onboarding_step' => 'profile_update' }) + } + end + + def update_account_info + @account.update!( + account_attributes + ) + end + def fetch_account_and_user_info; end def fetch_account diff --git a/app/views/api/v1/models/_account.json.jbuilder b/app/views/api/v1/models/_account.json.jbuilder index 9f89a5510..5e9d9048a 100644 --- a/app/views/api/v1/models/_account.json.jbuilder +++ b/app/views/api/v1/models/_account.json.jbuilder @@ -6,6 +6,11 @@ if resource.custom_attributes.present? json.subscribed_quantity resource.custom_attributes['subscribed_quantity'] json.subscription_status resource.custom_attributes['subscription_status'] json.subscription_ends_on resource.custom_attributes['subscription_ends_on'] + json.industry resource.custom_attributes['industry'] if resource.custom_attributes['industry'].present? + json.company_size resource.custom_attributes['company_size'] if resource.custom_attributes['company_size'].present? + json.timezone resource.custom_attributes['timezone'] if resource.custom_attributes['timezone'].present? + json.logo resource.custom_attributes['logo'] if resource.custom_attributes['logo'].present? + json.onboarding_step resource.custom_attributes['onboarding_step'] if resource.custom_attributes['onboarding_step'].present? end end json.domain @account.domain diff --git a/enterprise/app/controllers/enterprise/api/v2/accounts_controller.rb b/enterprise/app/controllers/enterprise/api/v2/accounts_controller.rb index d0d76c1ae..3edefbb5f 100644 --- a/enterprise/app/controllers/enterprise/api/v2/accounts_controller.rb +++ b/enterprise/app/controllers/enterprise/api/v2/accounts_controller.rb @@ -2,12 +2,11 @@ module Enterprise::Api::V2::AccountsController private def fetch_account_and_user_info - data = fetch_from_clearbit + @data = fetch_from_clearbit - return if data.blank? + return if @data.blank? - update_user_info(data) - update_account_info(data) + update_user_info end def fetch_from_clearbit @@ -17,19 +16,25 @@ module Enterprise::Api::V2::AccountsController nil end - def update_user_info(data) - @user.update!(name: data[:name]) + def update_user_info + @user.update!(name: @data[:name]) if @data[:name].present? end - def update_account_info(data) - @account.update!( - name: data[:company_name], - custom_attributes: @account.custom_attributes.merge( - 'industry' => data[:industry], - 'company_size' => data[:company_size], - 'timezone' => data[:timezone], - 'logo' => data[:logo] - ) + def data_from_clearbit + return {} if @data.blank? + + { name: @data[:company_name], + custom_attributes: { + 'industry' => @data[:industry], + 'company_size' => @data[:company_size], + 'timezone' => @data[:timezone], + 'logo' => @data[:logo] + } } + end + + def account_attributes + super.deep_merge( + data_from_clearbit ) end end diff --git a/spec/controllers/api/v1/accounts_controller_spec.rb b/spec/controllers/api/v1/accounts_controller_spec.rb index 95fa2ae7f..bd604f650 100644 --- a/spec/controllers/api/v1/accounts_controller_spec.rb +++ b/spec/controllers/api/v1/accounts_controller_spec.rb @@ -213,6 +213,25 @@ RSpec.describe 'Accounts API', type: :request do end end + it 'updates onboarding step to invite_team if onboarding step is present in account custom attributes' do + account.update(custom_attributes: { onboarding_step: 'account_update' }) + put "/api/v1/accounts/#{account.id}", + params: params, + headers: admin.create_new_auth_token, + as: :json + + expect(account.reload.custom_attributes['onboarding_step']).to eq('invite_team') + end + + it 'will not update onboarding step if onboarding step is not present in account custom attributes' do + put "/api/v1/accounts/#{account.id}", + params: params, + headers: admin.create_new_auth_token, + as: :json + + expect(account.reload.custom_attributes['onboarding_step']).to be_nil + end + it 'Throws error 422' do params[:name] = 'test' * 999 diff --git a/spec/controllers/api/v2/accounts_controller_spec.rb b/spec/controllers/api/v2/accounts_controller_spec.rb index 82693ed2b..182ebadac 100644 --- a/spec/controllers/api/v2/accounts_controller_spec.rb +++ b/spec/controllers/api/v2/accounts_controller_spec.rb @@ -30,6 +30,20 @@ RSpec.describe 'Accounts API', type: :request do end end + it 'updates the onboarding step in custom attributes' do + with_modified_env ENABLE_ACCOUNT_SIGNUP: 'true' do + allow(account_builder).to receive(:perform).and_return([user, account]) + + params = { email: email, user: nil, locale: nil, password: 'Password1!' } + + post api_v2_accounts_url, + params: params, + as: :json + + expect(account.reload.custom_attributes['onboarding_step']).to eq('profile_update') + end + end + it 'calls ChatwootCaptcha' do with_modified_env ENABLE_ACCOUNT_SIGNUP: 'true' do captcha = double diff --git a/spec/enterprise/controllers/api/v1/accounts/agents_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/agents_controller_spec.rb index 97e76a6cd..e5a8a5b7d 100644 --- a/spec/enterprise/controllers/api/v1/accounts/agents_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/agents_controller_spec.rb @@ -41,5 +41,15 @@ RSpec.describe 'Agents API', type: :request do expect(response.body).to include('Account limit exceeded. Please purchase more licenses') end end + + context 'when onboarding step is present in account custom attributes' do + it 'removes onboarding step from account custom attributes' do + account.update(custom_attributes: { onboarding_step: 'completed' }) + + post "/api/v1/accounts/#{account.id}/agents/bulk_create", params: bulk_create_params, headers: admin.create_new_auth_token + + expect(account.reload.custom_attributes).not_to include('onboarding_step') + end + end end end diff --git a/spec/enterprise/controllers/enterprise/api/v2/accounts_controller_spec.rb b/spec/enterprise/controllers/enterprise/api/v2/accounts_controller_spec.rb index 8ad75b42f..c74a2bcb5 100644 --- a/spec/enterprise/controllers/enterprise/api/v2/accounts_controller_spec.rb +++ b/spec/enterprise/controllers/enterprise/api/v2/accounts_controller_spec.rb @@ -51,6 +51,22 @@ RSpec.describe Enterprise::Api::V2::AccountsController, type: :request do end end + it 'updates the onboarding step in custom attributes' do + with_modified_env ENABLE_ACCOUNT_SIGNUP: 'true' do + allow(account_builder).to receive(:perform).and_return([user, account]) + + params = { email: email, user: nil, locale: nil, password: 'Password1!' } + + post api_v2_accounts_url, + params: params, + as: :json + + custom_attributes = account.custom_attributes + + expect(custom_attributes['onboarding_step']).to eq('profile_update') + end + end + it 'handles errors when fetching data from clearbit' do with_modified_env ENABLE_ACCOUNT_SIGNUP: 'true' do allow(account_builder).to receive(:perform).and_return([user, account])