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