feat: setup document usage

This commit is contained in:
Shivam Mishra
2025-01-21 13:59:07 +05:30
parent 637fc2c9ac
commit e6ca17d134
2 changed files with 16 additions and 2 deletions
@@ -36,6 +36,8 @@ class Captain::Document < ApplicationRecord
}
after_create_commit :enqueue_crawl_job
after_create_commit :update_document_usage
after_destroy :update_document_usage
after_commit :enqueue_response_builder_job
scope :ordered, -> { order(created_at: :desc) }
@@ -56,6 +58,10 @@ class Captain::Document < ApplicationRecord
Captain::Documents::ResponseBuilderJob.perform_later(self)
end
def update_document_usage
account.update_document_usage
end
def ensure_account_id
self.account_id = assistant&.account_id
end
+10 -2
View File
@@ -1,5 +1,6 @@
module Enterprise::Account
CAPTAIN_RESPONSES = 'captain_responses'.freeze
CAPTAIN_DOCUMENTS = 'captain_documents'.freeze
def usage_limits
{
@@ -23,6 +24,12 @@ module Enterprise::Account
save
end
def update_document_usage
# this will ensure that the document count is always accurate
self[:limits][CAPTAIN_DOCUMENTS] = captain_documents.count
save
end
def subscribed_features
plan_features = InstallationConfig.find_by(name: 'CHATWOOT_CLOUD_PLAN_FEATURES')&.value
return [] if plan_features.blank?
@@ -47,7 +54,7 @@ module Enterprise::Account
total_count = captain_monthly_limit[type.to_s].to_i
consumed = if type == :documents
captain_documents.count
self[:limits][CAPTAIN_DOCUMENTS].to_i || 0
else
self[:limits][CAPTAIN_RESPONSES].to_i || 0
end
@@ -88,7 +95,8 @@ module Enterprise::Account
'properties' => {
'inboxes' => { 'type': 'number' },
'agents' => { 'type': 'number' },
'captain_responses' => { 'type': 'number' }
'captain_responses' => { 'type': 'number' },
'captain_documents' => { 'type': 'number' }
},
'required' => [],
'additionalProperties' => false