chore: Update data format for platform API feature management (#5718)

Updated JSON data format for Platform API for account creation update API endpoints features flags to accept false values on each feature. 

fixes: #5717
This commit is contained in:
smartdev58 2022-11-08 03:49:45 +02:00 committed by GitHub
parent 48373628a1
commit b50890d1b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 27 deletions

View file

@ -1,6 +1,7 @@
class Platform::Api::V1::AccountsController < PlatformController class Platform::Api::V1::AccountsController < PlatformController
def create def create
@resource = Account.new(account_params) @resource = Account.new(account_params)
update_resource_features
@resource.save! @resource.save!
@platform_app.platform_app_permissibles.find_or_create_by(permissible: @resource) @platform_app.platform_app_permissibles.find_or_create_by(permissible: @resource)
end end
@ -8,7 +9,9 @@ class Platform::Api::V1::AccountsController < PlatformController
def show; end def show; end
def update def update
@resource.update!(account_params) @resource.assign_attributes(account_params)
update_resource_features
@resource.save!
end end
def destroy def destroy
@ -23,14 +26,18 @@ class Platform::Api::V1::AccountsController < PlatformController
end end
def account_params def account_params
if permitted_params[:enabled_features] permitted_params.except(:features)
return permitted_params.except(:enabled_features).merge(selected_feature_flags: permitted_params[:enabled_features].map(&:to_sym))
end end
permitted_params def update_resource_features
return if permitted_params[:features].blank?
permitted_params[:features].each do |key, value|
value.present? ? @resource.enable_features(key) : @resource.disable_features(key)
end
end end
def permitted_params def permitted_params
params.permit(:name, :locale, enabled_features: [], limits: {}) params.permit(:name, :locale, :domain, :support_email, :status, features: {}, limits: {}, custom_attributes: {})
end end
end end

View file

@ -16,7 +16,7 @@ module Featurable
include FlagShihTzu include FlagShihTzu
has_flags FEATURES.merge(column: 'feature_flags').merge(QUERY_MODE) has_flags FEATURES.merge(column: 'feature_flags').merge(QUERY_MODE)
before_create :enable_default_features after_initialize :enable_default_features
end end
def enable_features(*names) def enable_features(*names)

View file

@ -42,29 +42,28 @@ RSpec.describe 'Platform Accounts API', type: :request do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['name']).to eq('Test Account') expect(json_response['name']).to eq('Test Account')
expect(json_response['locale']).to eq('es') expect(json_response['locale']).to eq('es')
expect(json_response['enabled_features']).to eq( expect(json_response['enabled_features']['agent_management']).to be(true)
{
'agent_management' => true
}
)
end end
it 'creates an account with feature flags' do it 'creates an account with feature flags' do
InstallationConfig.where(name: 'ACCOUNT_LEVEL_FEATURE_DEFAULTS').first_or_create!(value: [{ 'name' => 'inbox_management', InstallationConfig.where(name: 'ACCOUNT_LEVEL_FEATURE_DEFAULTS').first_or_create!(value: [{ 'name' => 'inbox_management',
'enabled' => true },
{ 'name' => 'disable_branding',
'enabled' => true }]) 'enabled' => true }])
post '/platform/api/v1/accounts', params: { name: 'Test Account', enabled_features: %w[feature_ip_lookup feature_help_center] }, post '/platform/api/v1/accounts', params: { name: 'Test Account', features: {
headers: { api_access_token: platform_app.access_token.token }, as: :json ip_lookup: true,
help_center: true,
disable_branding: false
} }, headers: { api_access_token: platform_app.access_token.token }, as: :json
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['name']).to include('Test Account') expect(json_response['name']).to include('Test Account')
expect(json_response['enabled_features']).to eq( expect(json_response['enabled_features']['inbox_management']).to be(true)
{ expect(json_response['enabled_features']['ip_lookup']).to be(true)
'inbox_management' => true, expect(json_response['enabled_features']['help_center']).to be(true)
'ip_lookup' => true, expect(json_response['enabled_features']['disable_branding']).to be_nil
'help_center' => true
}
)
end end
it 'creates an account with limits settings' do it 'creates an account with limits settings' do
@ -141,19 +140,25 @@ RSpec.describe 'Platform Accounts API', type: :request do
it 'updates an account when its permissible object' do it 'updates an account when its permissible object' do
create(:platform_app_permissible, platform_app: platform_app, permissible: account) create(:platform_app_permissible, platform_app: platform_app, permissible: account)
account.enable_features!('inbox_management', 'channel_facebook')
patch "/platform/api/v1/accounts/#{account.id}", params: { patch "/platform/api/v1/accounts/#{account.id}", params: {
name: 'Test Account', name: 'Test Account',
enabled_features: %w[feature_ip_lookup feature_help_center], features: {
ip_lookup: true,
help_center: true,
channel_facebook: false
},
limits: { agents: 5, inboxes: 10 } limits: { agents: 5, inboxes: 10 }
}, headers: { api_access_token: platform_app.access_token.token }, as: :json }, headers: { api_access_token: platform_app.access_token.token }, as: :json
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(account.reload.name).to eq('Test Account') account.reload
expect(account.reload.enabled_features['ip_lookup']).to be(true) expect(account.name).to eq('Test Account')
expect(account.reload.enabled_features['help_center']).to be(true) expect(account.enabled_features.keys).to match_array(%w[inbox_management ip_lookup help_center])
expect(account.reload.limits['agents']).to eq(5) expect(account.enabled_features['channel_facebook']).to be_nil
expect(account.reload.limits['inboxes']).to eq(10) expect(account.limits['agents']).to eq(5)
expect(account.limits['inboxes']).to eq(10)
end end
end end
end end