feat: allow feature plan map in super admin (#9318)

- Add subscribed_features method in models/enterprise/account and include it in the JSON response
This commit is contained in:
Shivam Mishra
2024-05-09 11:28:46 +05:30
committed by GitHub
parent 4eec0aa11a
commit eff24c0d71
5 changed files with 59 additions and 0 deletions

View File

@@ -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?

View File

@@ -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'

View File

@@ -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)

View File

@@ -0,0 +1 @@
json.subscribed_features @account.subscribed_features

View File

@@ -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