feat: Add the ability for a super admin to define account limits (#3946)
Fixes: #3773
This commit is contained in:
parent
edd93ca4a3
commit
3d73d8935a
9 changed files with 71 additions and 14 deletions
|
@ -2,6 +2,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
||||||
include Api::V1::InboxesHelper
|
include Api::V1::InboxesHelper
|
||||||
before_action :fetch_inbox, except: [:index, :create]
|
before_action :fetch_inbox, except: [:index, :create]
|
||||||
before_action :fetch_agent_bot, only: [:set_agent_bot]
|
before_action :fetch_agent_bot, only: [:set_agent_bot]
|
||||||
|
before_action :validate_limit, only: [:create]
|
||||||
# we are already handling the authorization in fetch inbox
|
# we are already handling the authorization in fetch inbox
|
||||||
before_action :check_authorization, except: [:show]
|
before_action :check_authorization, except: [:show]
|
||||||
|
|
||||||
|
@ -143,4 +144,10 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -33,11 +33,11 @@ class SuperAdmin::AccountsController < SuperAdmin::ApplicationController
|
||||||
# empty values into nil values. It uses other APIs such as `resource_class`
|
# empty values into nil values. It uses other APIs such as `resource_class`
|
||||||
# and `dashboard`:
|
# and `dashboard`:
|
||||||
#
|
#
|
||||||
# def resource_params
|
def resource_params
|
||||||
# params.require(resource_class.model_name.param_key).
|
permitted_params = super
|
||||||
# permit(dashboard.permitted_attributes).
|
permitted_params[:limits] = permitted_params[:limits].to_h.compact
|
||||||
# transform_values { |value| value == "" ? nil : value }
|
permitted_params
|
||||||
# end
|
end
|
||||||
|
|
||||||
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
|
# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
|
||||||
# for more information
|
# for more information
|
||||||
|
|
|
@ -7,6 +7,8 @@ class AccountDashboard < Administrate::BaseDashboard
|
||||||
# Each different type represents an Administrate::Field object,
|
# Each different type represents an Administrate::Field object,
|
||||||
# which determines how the attribute is displayed
|
# which determines how the attribute is displayed
|
||||||
# on pages throughout the dashboard.
|
# on pages throughout the dashboard.
|
||||||
|
|
||||||
|
enterprise_attribute_types = ChatwootApp.enterprise? ? { limits: Enterprise::AccountLimitsField } : {}
|
||||||
ATTRIBUTE_TYPES = {
|
ATTRIBUTE_TYPES = {
|
||||||
id: Field::Number,
|
id: Field::Number,
|
||||||
name: Field::String,
|
name: Field::String,
|
||||||
|
@ -16,7 +18,7 @@ class AccountDashboard < Administrate::BaseDashboard
|
||||||
conversations: CountField,
|
conversations: CountField,
|
||||||
locale: Field::Select.with_options(collection: LANGUAGES_CONFIG.map { |_x, y| y[:iso_639_1_code] }),
|
locale: Field::Select.with_options(collection: LANGUAGES_CONFIG.map { |_x, y| y[:iso_639_1_code] }),
|
||||||
account_users: Field::HasMany
|
account_users: Field::HasMany
|
||||||
}.freeze
|
}.merge(enterprise_attribute_types).freeze
|
||||||
|
|
||||||
# COLLECTION_ATTRIBUTES
|
# COLLECTION_ATTRIBUTES
|
||||||
# an array of attributes that will be displayed on the model's index page.
|
# 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
|
# SHOW_PAGE_ATTRIBUTES
|
||||||
# an array of attributes that will be displayed on the model's show page.
|
# 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
|
id
|
||||||
name
|
name
|
||||||
created_at
|
created_at
|
||||||
|
@ -41,15 +44,16 @@ class AccountDashboard < Administrate::BaseDashboard
|
||||||
locale
|
locale
|
||||||
conversations
|
conversations
|
||||||
account_users
|
account_users
|
||||||
].freeze
|
] + enterprise_show_page_attributes).freeze
|
||||||
|
|
||||||
# FORM_ATTRIBUTES
|
# FORM_ATTRIBUTES
|
||||||
# an array of attributes that will be displayed
|
# an array of attributes that will be displayed
|
||||||
# on the model's form (`new` and `edit`) pages.
|
# on the model's form (`new` and `edit`) pages.
|
||||||
FORM_ATTRIBUTES = %i[
|
enterprise_form_attributes = ChatwootApp.enterprise? ? %i[limits] : []
|
||||||
|
FORM_ATTRIBUTES = (%i[
|
||||||
name
|
name
|
||||||
locale
|
locale
|
||||||
].freeze
|
] + enterprise_form_attributes).freeze
|
||||||
|
|
||||||
# COLLECTION_FILTERS
|
# COLLECTION_FILTERS
|
||||||
# a hash that defines filters that can be used while searching via the search
|
# 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)
|
def display_resource(account)
|
||||||
"##{account.id} #{account.name}"
|
"##{account.id} #{account.name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def permitted_attributes
|
||||||
|
super + [limits: {}]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
7
app/fields/enterprise/account_limits_field.rb
Normal file
7
app/fields/enterprise/account_limits_field.rb
Normal 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
|
|
@ -77,6 +77,7 @@ class Account < ApplicationRecord
|
||||||
|
|
||||||
enum locale: LANGUAGES_CONFIG.map { |key, val| [val[:iso_639_1_code], key] }.to_h
|
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
|
after_create_commit :notify_creation
|
||||||
|
|
||||||
def agents
|
def agents
|
||||||
|
@ -114,8 +115,8 @@ class Account < ApplicationRecord
|
||||||
|
|
||||||
def usage_limits
|
def usage_limits
|
||||||
{
|
{
|
||||||
agents: ChatwootApp.max_limit,
|
agents: ChatwootApp.max_limit.to_i,
|
||||||
inboxes: ChatwootApp.max_limit
|
inboxes: ChatwootApp.max_limit.to_i
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -132,4 +133,8 @@ class Account < ApplicationRecord
|
||||||
trigger.name('camp_dpid_before_insert').after(:insert).for_each(:row) do
|
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);"
|
"execute format('create sequence IF NOT EXISTS camp_dpid_seq_%s', NEW.id);"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_limit_keys
|
||||||
|
# method overridden in enterprise module
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
9
app/views/fields/account_limits_field/_form.html.erb
Normal file
9
app/views/fields/account_limits_field/_form.html.erb
Normal 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>
|
1
app/views/fields/account_limits_field/_index.html.erb
Normal file
1
app/views/fields/account_limits_field/_index.html.erb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<%= field.to_s %>
|
3
app/views/fields/account_limits_field/_show.html.erb
Normal file
3
app/views/fields/account_limits_field/_show.html.erb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<% JSON.parse(field.to_s).each do |k,v| %>
|
||||||
|
<%= k %>: <%= v %> </br>
|
||||||
|
<% end %>
|
|
@ -1,8 +1,8 @@
|
||||||
module Enterprise::Account
|
module Enterprise::Account
|
||||||
def usage_limits
|
def usage_limits
|
||||||
{
|
{
|
||||||
agents: get_limits(:agents),
|
agents: get_limits(:agents).to_i,
|
||||||
inboxes: get_limits(:inboxes)
|
inboxes: get_limits(:inboxes).to_i
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,4 +12,21 @@ module Enterprise::Account
|
||||||
config_name = "ACCOUNT_#{limit_name.to_s.upcase}_LIMIT"
|
config_name = "ACCOUNT_#{limit_name.to_s.upcase}_LIMIT"
|
||||||
self[:limits][limit_name.to_s] || GlobalConfig.get(config_name)[config_name] || ChatwootApp.max_limit
|
self[:limits][limit_name.to_s] || GlobalConfig.get(config_name)[config_name] || ChatwootApp.max_limit
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue