From 3d73d8935a3739f38446b08357a9e760f6cc98d8 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Mon, 14 Feb 2022 15:55:08 +0530 Subject: [PATCH] feat: Add the ability for a super admin to define account limits (#3946) Fixes: #3773 --- .../api/v1/accounts/inboxes_controller.rb | 7 +++++++ .../super_admin/accounts_controller.rb | 10 ++++----- app/dashboards/account_dashboard.rb | 18 +++++++++++----- app/fields/enterprise/account_limits_field.rb | 7 +++++++ app/models/account.rb | 9 ++++++-- .../account_limits_field/_form.html.erb | 9 ++++++++ .../account_limits_field/_index.html.erb | 1 + .../account_limits_field/_show.html.erb | 3 +++ enterprise/app/models/enterprise/account.rb | 21 +++++++++++++++++-- 9 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 app/fields/enterprise/account_limits_field.rb create mode 100644 app/views/fields/account_limits_field/_form.html.erb create mode 100644 app/views/fields/account_limits_field/_index.html.erb create mode 100644 app/views/fields/account_limits_field/_show.html.erb diff --git a/app/controllers/api/v1/accounts/inboxes_controller.rb b/app/controllers/api/v1/accounts/inboxes_controller.rb index 62a471cda..e0c963d25 100644 --- a/app/controllers/api/v1/accounts/inboxes_controller.rb +++ b/app/controllers/api/v1/accounts/inboxes_controller.rb @@ -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 diff --git a/app/controllers/super_admin/accounts_controller.rb b/app/controllers/super_admin/accounts_controller.rb index 4d35fa1d8..28cae96b0 100644 --- a/app/controllers/super_admin/accounts_controller.rb +++ b/app/controllers/super_admin/accounts_controller.rb @@ -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 diff --git a/app/dashboards/account_dashboard.rb b/app/dashboards/account_dashboard.rb index 3cc35d0d3..408589597 100644 --- a/app/dashboards/account_dashboard.rb +++ b/app/dashboards/account_dashboard.rb @@ -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 diff --git a/app/fields/enterprise/account_limits_field.rb b/app/fields/enterprise/account_limits_field.rb new file mode 100644 index 000000000..5833952ad --- /dev/null +++ b/app/fields/enterprise/account_limits_field.rb @@ -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 diff --git a/app/models/account.rb b/app/models/account.rb index da0dd062b..ee595902a 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -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 diff --git a/app/views/fields/account_limits_field/_form.html.erb b/app/views/fields/account_limits_field/_form.html.erb new file mode 100644 index 000000000..8c932a704 --- /dev/null +++ b/app/views/fields/account_limits_field/_form.html.erb @@ -0,0 +1,9 @@ +
+ <%= f.label field.attribute %> +
+
+ + <% JSON.parse(field.to_s).each do |key,val| %> + <%= key %>: <%= number_field "account[limits]", key, value: val %>
+ <% end %> +
diff --git a/app/views/fields/account_limits_field/_index.html.erb b/app/views/fields/account_limits_field/_index.html.erb new file mode 100644 index 000000000..6d9dbc907 --- /dev/null +++ b/app/views/fields/account_limits_field/_index.html.erb @@ -0,0 +1 @@ +<%= field.to_s %> diff --git a/app/views/fields/account_limits_field/_show.html.erb b/app/views/fields/account_limits_field/_show.html.erb new file mode 100644 index 000000000..d464212d6 --- /dev/null +++ b/app/views/fields/account_limits_field/_show.html.erb @@ -0,0 +1,3 @@ +<% JSON.parse(field.to_s).each do |k,v| %> +<%= k %>: <%= v %>
+<% end %> diff --git a/enterprise/app/models/enterprise/account.rb b/enterprise/app/models/enterprise/account.rb index 84d72e0b3..99defb6ea 100644 --- a/enterprise/app/models/enterprise/account.rb +++ b/enterprise/app/models/enterprise/account.rb @@ -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