Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow feature plan map in super admin #9318

Merged
merged 17 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/views/api/v1/accounts/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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?
11 changes: 9 additions & 2 deletions app/views/super_admin/app_configs/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@
<div class="field-unit__label">
<%= form.label "app_config[#{key}]", @installation_configs[key]&.dig('display_title') || key %>
</div>
<div class="field-unit__field -mt-2 ">
<div class="-mt-2 field-unit__field ">
<% if @installation_configs[key]&.dig('type') == 'boolean' %>
<%= form.select "app_config[#{key}]",
[["True", true], ["False", false]],
{ selected: ActiveModel::Type::Boolean.new.cast(@app_config[key]) },
class: "mt-2 border border-slate-100 p-1 rounded-md"
%>
<% elsif @installation_configs[key]&.dig('type') == 'json' %>
scmmishra marked this conversation as resolved.
Show resolved Hide resolved
<%= form.text_area "app_config[#{key}]",
disabled: ActiveModel::Type::Boolean.new.cast(@installation_configs[key]&.dig('locked')),
value: @app_config[key] ? JSON.pretty_generate(@app_config[key]) : nil,
rows: 12,
class: "mt-2 font-mono border border-slate-100 p-1 rounded-md"
%>
<% else %>
<%= form.text_field "app_config[#{key}]", value: @app_config[key] %>
<% end %>
<%if @installation_configs[key]&.dig('description').present? %>
<p class="text-slate-400 text-xs italic pt-2">
<p class="pt-2 text-xs italic text-slate-400">
<%= @installation_configs[key]&.dig('description') %>
</p>
<% end %>
Expand Down
9 changes: 9 additions & 0 deletions config/installation_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,17 @@
description: 'The Chatwoot Inbox HMAC Key for Contact Support in Cloud'
locked: false
- name: CHATWOOT_CLOUD_PLANS
display_title: 'Cloud Plans'
type: json
value:
locked: true
description: 'Config to store stripe plans for cloud'
- name: CHATWOOT_CLOUD_PLAN_FEATURES
display_title: 'Planwise Features List'
type: json
value:
locked: true
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ def custom_branding_options
end

def internal_config_options
%w[CHATWOOT_INBOX_TOKEN CHATWOOT_INBOX_HMAC_KEY ANALYTICS_TOKEN CLEARBIT_API_KEY]
%w[CHATWOOT_INBOX_TOKEN CHATWOOT_INBOX_HMAC_KEY ANALYTICS_TOKEN CLEARBIT_API_KEY CHATWOOT_CLOUD_PLANS CHATWOOT_CLOUD_PLAN_FEATURES]
end
end
11 changes: 11 additions & 0 deletions enterprise/app/models/enterprise/account.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.subscribed_features @account.subscribed_features
44 changes: 44 additions & 0 deletions spec/models/enterprise/account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'rails_helper'

RSpec.describe Enterprise::Account do
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