Skip to content

Commit

Permalink
feat: allow feature plan map in super admin (#9318)
Browse files Browse the repository at this point in the history
- Add subscribed_features method in models/enterprise/account and include it in the JSON response
  • Loading branch information
scmmishra committed May 9, 2024
1 parent 4eec0aa commit eff24c0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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?
5 changes: 5 additions & 0 deletions config/installation_config.yml
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions enterprise/app/models/enterprise/account.rb
Expand Up @@ -6,8 +6,19 @@ def usage_limits
}
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)
Expand Down
@@ -0,0 +1 @@
json.subscribed_features @account.subscribed_features
41 changes: 41 additions & 0 deletions spec/enterprise/models/account_spec.rb
Expand Up @@ -91,4 +91,45 @@
)
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

0 comments on commit eff24c0

Please sign in to comment.