diff --git a/app/views/api/v1/accounts/show.json.jbuilder b/app/views/api/v1/accounts/show.json.jbuilder index 2f1bbcbc6..cb8c27c92 100644 --- a/app/views/api/v1/accounts/show.json.jbuilder +++ b/app/views/api/v1/accounts/show.json.jbuilder @@ -1,2 +1,3 @@ json.partial! 'api/v1/models/account', formats: [:json], resource: @account json.latest_chatwoot_version @latest_chatwoot_version +json.partial! 'enterprise/api/v1/accounts/partials/account', account: @account if ChatwootApp.enterprise? diff --git a/config/installation_config.yml b/config/installation_config.yml index 9db527832..80bfbfd61 100644 --- a/config/installation_config.yml +++ b/config/installation_config.yml @@ -135,8 +135,13 @@ description: 'The Chatwoot Inbox HMAC Key for Contact Support in Cloud' locked: false - name: CHATWOOT_CLOUD_PLANS + display_title: 'Cloud Plans' value: description: 'Config to store stripe plans for cloud' +- name: CHATWOOT_CLOUD_PLAN_FEATURES + display_title: 'Planwise Features List' + value: + description: 'Config to features and their associated plans' - name: DEPLOYMENT_ENV value: self-hosted description: 'The deployment environment of the installation, to differentiate between Chatwoot cloud and self-hosted' diff --git a/enterprise/app/models/enterprise/account.rb b/enterprise/app/models/enterprise/account.rb index 2089247a6..bc07e1171 100644 --- a/enterprise/app/models/enterprise/account.rb +++ b/enterprise/app/models/enterprise/account.rb @@ -6,8 +6,19 @@ module Enterprise::Account } end + def subscribed_features + plan_features = InstallationConfig.find_by(name: 'CHATWOOT_CLOUD_PLAN_FEATURES')&.value + return [] if plan_features.blank? + + plan_features[plan_name] + end + private + def plan_name + custom_attributes['plan_name'] + end + def agent_limits subscribed_quantity = custom_attributes['subscribed_quantity'] subscribed_quantity || get_limits(:agents) diff --git a/enterprise/app/views/enterprise/api/v1/accounts/partials/_account.json.jbuilder b/enterprise/app/views/enterprise/api/v1/accounts/partials/_account.json.jbuilder new file mode 100644 index 000000000..bbb49621d --- /dev/null +++ b/enterprise/app/views/enterprise/api/v1/accounts/partials/_account.json.jbuilder @@ -0,0 +1 @@ +json.subscribed_features @account.subscribed_features diff --git a/spec/enterprise/models/account_spec.rb b/spec/enterprise/models/account_spec.rb index 87e4ac5c2..67397a066 100644 --- a/spec/enterprise/models/account_spec.rb +++ b/spec/enterprise/models/account_spec.rb @@ -91,4 +91,45 @@ RSpec.describe Account do ) end end + + describe 'subscribed_features' do + let(:account) { create(:account) } + let(:plan_features) do + { + 'hacker' => %w[feature1 feature2], + 'startups' => %w[feature1 feature2 feature3 feature4] + } + end + + before do + InstallationConfig.where(name: 'CHATWOOT_CLOUD_PLAN_FEATURES').first_or_create(value: plan_features) + end + + context 'when plan_name is hacker' do + it 'returns the features for the hacker plan' do + account.custom_attributes = { 'plan_name': 'hacker' } + account.save! + + expect(account.subscribed_features).to eq(%w[feature1 feature2]) + end + end + + context 'when plan_name is startups' do + it 'returns the features for the startups plan' do + account.custom_attributes = { 'plan_name': 'startups' } + account.save! + + expect(account.subscribed_features).to eq(%w[feature1 feature2 feature3 feature4]) + end + end + + context 'when plan_features is blank' do + it 'returns an empty array' do + account.custom_attributes = {} + account.save! + + expect(account.subscribed_features).to be_nil + end + end + end end