This commit is contained in:
aakashb95
2026-01-12 13:09:14 +05:30
parent 6debfbffe9
commit 2299bee182
2 changed files with 26 additions and 22 deletions
@@ -0,0 +1,22 @@
module Enterprise::Account::LimitValidation
extend ActiveSupport::Concern
LIMIT_SCHEMA = {
'type' => 'object',
'properties' => {
'inboxes' => { 'type': 'number' },
'agents' => { 'type': 'number' },
'captain_responses' => { 'type': 'number' },
'captain_documents' => { 'type': 'number' }
},
'required' => [],
'additionalProperties' => false
}.freeze
def validate_limit_keys
errors.add(:limits, ': Invalid data') unless self[:limits].is_a? Hash
self[:limits] = {} if self[:limits].blank?
errors.add(:limits, ': Invalid data') unless JSONSchemer.schema(LIMIT_SCHEMA).valid?(self[:limits])
end
end
@@ -1,4 +1,6 @@
module Enterprise::Account::PlanUsageAndLimits
include Enterprise::Account::LimitValidation
CAPTAIN_RESPONSES = 'captain_responses'.freeze
CAPTAIN_DOCUMENTS = 'captain_documents'.freeze
CAPTAIN_RESPONSES_USAGE = 'captain_responses_usage'.freeze
@@ -17,7 +19,6 @@ module Enterprise::Account::PlanUsageAndLimits
def increment_response_usage
increment_sql = <<~SQL.squish
custom_attributes = jsonb_set(
custom_attributes,
'{#{CAPTAIN_RESPONSES_USAGE}}',
@@ -25,8 +26,8 @@ module Enterprise::Account::PlanUsageAndLimits
true
)
SQL
updated = self.class.where(id: id).update_all(increment_sql)
# Skipping validations is safe: no validations on custom_attributes,
updated = self.class.where(id: id).update_all(increment_sql) # rubocop:disable Rails/SkipsModelValidations
reload if updated.positive?
end
@@ -117,23 +118,4 @@ module Enterprise::Account::PlanUsageAndLimits
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' },
'captain_responses' => { 'type': 'number' },
'captain_documents' => { 'type': 'number' }
},
'required' => [],
'additionalProperties' => false
}
errors.add(:limits, ': Invalid data') unless JSONSchemer.schema(limit_schema).valid?(self[:limits])
end
end