Chore: Improve conversation auto resolution (#3405)

Fixes: #1583
This commit is contained in:
Shivam Chahar
2021-11-27 09:26:18 +05:30
committed by GitHub
parent 89f9f39e6e
commit a4ee8ee239
11 changed files with 75 additions and 85 deletions

View File

@@ -0,0 +1,9 @@
class Account::ConversationsResolutionSchedulerJob < ApplicationJob
queue_as :scheduled_jobs
def perform
Account.where.not(auto_resolve_duration: nil).all.each do |account|
Conversations::ResolutionJob.perform_later(account: account)
end
end
end

View File

@@ -1,16 +0,0 @@
class AutoResolveConversationsJob < ApplicationJob
queue_as :medium
def perform(conversation_id)
conversation = Conversation.find_by(id: conversation_id)
return unless conversation&.auto_resolve_duration && conversation&.open?
time_since_last_activity = Time.zone.now.to_i - conversation.last_activity_at.to_i
time_left_to_auto_resolve = conversation.auto_resolve_duration.days.to_i - time_since_last_activity
if time_left_to_auto_resolve.positive?
AutoResolveConversationsJob.set(wait_until: time_left_to_auto_resolve.seconds.from_now).perform_later(conversation_id)
else
conversation.toggle_status
end
end
end

View File

@@ -0,0 +1,8 @@
class Conversations::ResolutionJob < ApplicationJob
queue_as :medium
def perform(account:)
resolvable_conversations = account.conversations.resolvable(account.auto_resolve_duration)
resolvable_conversations.each(&:toggle_status)
end
end

View File

@@ -9,5 +9,8 @@ class TriggerScheduledItemsJob < ApplicationJob
# Job to reopen snoozed conversations
Conversations::ReopenSnoozedConversationsJob.perform_later
# Job to auto-resolve conversations
Account::ConversationsResolutionSchedulerJob.perform_later
end
end

View File

@@ -11,7 +11,6 @@ module ActivityMessageHandler
def status_change_activity(user_name)
create_status_change_message(user_name)
queue_conversation_auto_resolution_job if open?
end
def activity_message_params(content)

View File

@@ -57,6 +57,11 @@ class Conversation < ApplicationRecord
scope :unassigned, -> { where(assignee_id: nil) }
scope :assigned, -> { where.not(assignee_id: nil) }
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
scope :resolvable, lambda { |auto_resolve_duration|
return [] if auto_resolve_duration.to_i.zero?
open.where('last_activity_at < ? ', Time.now.utc - auto_resolve_duration.days)
}
belongs_to :account
belongs_to :inbox
@@ -74,7 +79,7 @@ class Conversation < ApplicationRecord
before_create :mark_conversation_pending_if_bot
after_update_commit :execute_after_update_commit_callbacks
after_create_commit :notify_conversation_creation, :queue_conversation_auto_resolution_job
after_create_commit :notify_conversation_creation
after_commit :set_display_id, unless: :display_id?
delegate :auto_resolve_duration, to: :account
@@ -175,14 +180,6 @@ class Conversation < ApplicationRecord
dispatcher_dispatch(CONVERSATION_CREATED)
end
def queue_conversation_auto_resolution_job
# FIXME: Move this to one cronjob that iterates over accounts and enqueue resolution jobs
# Similar to how we handle campaigns
return unless auto_resolve_duration
AutoResolveConversationsJob.set(wait_until: (last_activity_at || created_at) + auto_resolve_duration.days).perform_later(id)
end
def self_assign?(assignee_id)
assignee_id.present? && Current.user&.id == assignee_id
end