feat: Add the ability for a super admin to define account limits (#3946)

Fixes: #3773
This commit is contained in:
Sojan Jose 2022-02-14 15:55:08 +05:30 committed by GitHub
parent edd93ca4a3
commit 3d73d8935a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 71 additions and 14 deletions

View file

@ -2,6 +2,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
include Api::V1::InboxesHelper
before_action :fetch_inbox, except: [:index, :create]
before_action :fetch_agent_bot, only: [:set_agent_bot]
before_action :validate_limit, only: [:create]
# we are already handling the authorization in fetch inbox
before_action :check_authorization, except: [:show]
@ -143,4 +144,10 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
[]
end
end
def validate_limit
return unless Current.account.inboxes.count >= Current.account.usage_limits[:inboxes]
render_payment_required('Account limit exceeded. Upgrade to a higher plan')
end
end

View file

@ -33,11 +33,11 @@ class SuperAdmin::AccountsController < SuperAdmin::ApplicationController
# empty values into nil values. It uses other APIs such as `resource_class`
# and `dashboard`:
#
# def resource_params
# params.require(resource_class.model_name.param_key).
# permit(dashboard.permitted_attributes).
# transform_values { |value| value == "" ? nil : value }
# end
def resource_params
permitted_params = super
permitted_params[:limits] = permitted_params[:limits].to_h.compact
permitted_params
end
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information

View file

@ -7,6 +7,8 @@ class AccountDashboard < Administrate::BaseDashboard
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
enterprise_attribute_types = ChatwootApp.enterprise? ? { limits: Enterprise::AccountLimitsField } : {}
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
@ -16,7 +18,7 @@ class AccountDashboard < Administrate::BaseDashboard
conversations: CountField,
locale: Field::Select.with_options(collection: LANGUAGES_CONFIG.map { |_x, y| y[:iso_639_1_code] }),
account_users: Field::HasMany
}.freeze
}.merge(enterprise_attribute_types).freeze
# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
@ -33,7 +35,8 @@ class AccountDashboard < Administrate::BaseDashboard
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
enterprise_show_page_attributes = ChatwootApp.enterprise? ? %i[limits] : []
SHOW_PAGE_ATTRIBUTES = (%i[
id
name
created_at
@ -41,15 +44,16 @@ class AccountDashboard < Administrate::BaseDashboard
locale
conversations
account_users
].freeze
] + enterprise_show_page_attributes).freeze
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
enterprise_form_attributes = ChatwootApp.enterprise? ? %i[limits] : []
FORM_ATTRIBUTES = (%i[
name
locale
].freeze
] + enterprise_form_attributes).freeze
# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
@ -69,4 +73,8 @@ class AccountDashboard < Administrate::BaseDashboard
def display_resource(account)
"##{account.id} #{account.name}"
end
def permitted_attributes
super + [limits: {}]
end
end

View file

@ -0,0 +1,7 @@
require 'administrate/field/base'
class Enterprise::AccountLimitsField < Administrate::Field::Base
def to_s
data.present? ? data.to_json : { agents: nil, inboxes: nil }.to_json
end
end

View file

@ -77,6 +77,7 @@ class Account < ApplicationRecord
enum locale: LANGUAGES_CONFIG.map { |key, val| [val[:iso_639_1_code], key] }.to_h
before_validation :validate_limit_keys
after_create_commit :notify_creation
def agents
@ -114,8 +115,8 @@ class Account < ApplicationRecord
def usage_limits
{
agents: ChatwootApp.max_limit,
inboxes: ChatwootApp.max_limit
agents: ChatwootApp.max_limit.to_i,
inboxes: ChatwootApp.max_limit.to_i
}
end
@ -132,4 +133,8 @@ class Account < ApplicationRecord
trigger.name('camp_dpid_before_insert').after(:insert).for_each(:row) do
"execute format('create sequence IF NOT EXISTS camp_dpid_seq_%s', NEW.id);"
end
def validate_limit_keys
# method overridden in enterprise module
end
end

View file

@ -0,0 +1,9 @@
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<% JSON.parse(field.to_s).each do |key,val| %>
<%= key %>: <%= number_field "account[limits]", key, value: val %> </br>
<% end %>
</div>

View file

@ -0,0 +1 @@
<%= field.to_s %>

View file

@ -0,0 +1,3 @@
<% JSON.parse(field.to_s).each do |k,v| %>
<%= k %>: <%= v %> </br>
<% end %>

View file

@ -1,8 +1,8 @@
module Enterprise::Account
def usage_limits
{
agents: get_limits(:agents),
inboxes: get_limits(:inboxes)
agents: get_limits(:agents).to_i,
inboxes: get_limits(:inboxes).to_i
}
end
@ -12,4 +12,21 @@ module Enterprise::Account
config_name = "ACCOUNT_#{limit_name.to_s.upcase}_LIMIT"
self[:limits][limit_name.to_s] || GlobalConfig.get(config_name)[config_name] || ChatwootApp.max_limit
end
def validate_limit_keys
errors.add(:limits, ': Invalid data') unless self[:limits].is_a? Hash
self[:limits] = {} if self[:limits].blank?
limit_schema = {
'type' => 'object',
'properties' => {
'inboxes' => { 'type': 'number' },
'agents' => { 'type': 'number' }
},
'required' => [],
'additionalProperties' => false
}
errors.add(:limits, ': Invalid data') unless JSONSchemer.schema(limit_schema).valid?(self[:limits])
end
end