diff --git a/app/jobs/account/conversations_resolution_scheduler_job.rb b/app/jobs/account/conversations_resolution_scheduler_job.rb new file mode 100644 index 000000000..d98604c27 --- /dev/null +++ b/app/jobs/account/conversations_resolution_scheduler_job.rb @@ -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 diff --git a/app/jobs/auto_resolve_conversations_job.rb b/app/jobs/auto_resolve_conversations_job.rb deleted file mode 100644 index 08db549da..000000000 --- a/app/jobs/auto_resolve_conversations_job.rb +++ /dev/null @@ -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 diff --git a/app/jobs/conversations/resolution_job.rb b/app/jobs/conversations/resolution_job.rb new file mode 100644 index 000000000..957c8bace --- /dev/null +++ b/app/jobs/conversations/resolution_job.rb @@ -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 diff --git a/app/jobs/trigger_scheduled_items_job.rb b/app/jobs/trigger_scheduled_items_job.rb index 16094dc4a..5d1b89727 100644 --- a/app/jobs/trigger_scheduled_items_job.rb +++ b/app/jobs/trigger_scheduled_items_job.rb @@ -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 diff --git a/app/models/concerns/activity_message_handler.rb b/app/models/concerns/activity_message_handler.rb index 80ccae38a..90eb2f52d 100644 --- a/app/models/concerns/activity_message_handler.rb +++ b/app/models/concerns/activity_message_handler.rb @@ -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) diff --git a/app/models/conversation.rb b/app/models/conversation.rb index c5cd30629..1e5945431 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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 diff --git a/spec/jobs/account/conversations_resolution_scheduler_job_spec.rb b/spec/jobs/account/conversations_resolution_scheduler_job_spec.rb new file mode 100644 index 000000000..78baae779 --- /dev/null +++ b/spec/jobs/account/conversations_resolution_scheduler_job_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe Account::ConversationsResolutionSchedulerJob, type: :job do + subject(:job) { described_class.perform_later } + + let!(:account) { create(:account) } + + it 'enqueues the job' do + expect { job }.to have_enqueued_job(described_class) + .on_queue('scheduled_jobs') + end + + it 'enqueues Conversations::ResolutionJob' do + account.update(auto_resolve_duration: 10) + expect(Conversations::ResolutionJob).to receive(:perform_later).with(account: account).once + described_class.perform_now + end +end diff --git a/spec/jobs/auto_resolve_conversations_job_spec.rb b/spec/jobs/auto_resolve_conversations_job_spec.rb deleted file mode 100644 index 9ecea3b42..000000000 --- a/spec/jobs/auto_resolve_conversations_job_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'rails_helper' - -RSpec.describe AutoResolveConversationsJob, type: :job do - subject(:job) { described_class.perform_later(conversation.id) } - - let!(:account) { create(:account) } - let!(:conversation) { create(:conversation, account: account) } - - it 'queues the job' do - expect { job }.to have_enqueued_job(described_class) - .with(conversation.id) - .on_queue('medium') - end - - it 'does nothing when there is no auto resolve duration' do - described_class.perform_now(conversation.id) - expect(conversation.reload.status).to eq('open') - end - - it 're-queues the job if there is still time left to allow inactivity' do - account.update(auto_resolve_duration: 10) - conversation.update(last_activity_at: 3.days.ago) - expect { described_class.perform_now(conversation.id) }.to have_enqueued_job(described_class) - .with(conversation.id) - .on_queue('medium') - end - - it 'resolves the issue if time of inactivity is more than the auto resolve duration' do - account.update(auto_resolve_duration: 10) - conversation.update(last_activity_at: 13.days.ago) - described_class.perform_now(conversation.id) - expect(conversation.reload.status).to eq('resolved') - end -end diff --git a/spec/jobs/conversations/resolution_job_spec.rb b/spec/jobs/conversations/resolution_job_spec.rb new file mode 100644 index 000000000..0482f8bbf --- /dev/null +++ b/spec/jobs/conversations/resolution_job_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe Conversations::ResolutionJob, type: :job do + subject(:job) { described_class.perform_later(account) } + + let!(:account) { create(:account) } + let!(:conversation) { create(:conversation, account: account) } + + it 'enqueues the job' do + expect { job }.to have_enqueued_job(described_class) + .with(account) + .on_queue('medium') + end + + it 'does nothing when there is no auto resolve duration' do + described_class.perform_now(account: account) + expect(conversation.reload.status).to eq('open') + end + + it 'resolves the issue if time of inactivity is more than the auto resolve duration' do + account.update(auto_resolve_duration: 10) + conversation.update(last_activity_at: 13.days.ago) + described_class.perform_now(account: account) + expect(conversation.reload.status).to eq('resolved') + end +end diff --git a/spec/jobs/trigger_scheduled_items_job_spec.rb b/spec/jobs/trigger_scheduled_items_job_spec.rb index a7c3cffef..f94371e8b 100644 --- a/spec/jobs/trigger_scheduled_items_job_spec.rb +++ b/spec/jobs/trigger_scheduled_items_job_spec.rb @@ -25,5 +25,10 @@ RSpec.describe TriggerScheduledItemsJob, type: :job do expect(Conversations::ReopenSnoozedConversationsJob).to receive(:perform_later).once described_class.perform_now end + + it 'triggers Account::ConversationsResolutionSchedulerJob' do + expect(Account::ConversationsResolutionSchedulerJob).to receive(:perform_later).once + described_class.perform_now + end end end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 6fa916e54..c305909da 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -56,19 +56,6 @@ RSpec.describe Conversation, type: :model do expect(Rails.configuration.dispatcher).to have_received(:dispatch) .with(described_class::CONVERSATION_CREATED, kind_of(Time), conversation: conversation) end - - it 'queues AutoResolveConversationsJob post creation if auto resolve duration present' do - account.update(auto_resolve_duration: 30) - expect do - create( - :conversation, - account: account, - contact: create(:contact, account: account), - inbox: inbox, - assignee: nil - ) - end.to have_enqueued_job(AutoResolveConversationsJob) - end end describe '.after_update' do @@ -138,18 +125,6 @@ RSpec.describe Conversation, type: :model do .with(conversation2, { account_id: conversation2.account_id, inbox_id: conversation2.inbox_id, message_type: :activity, content: system_resolved_message }) end - - it 'does not trigger AutoResolutionJob if conversation reopened and account does not have auto resolve duration' do - expect { conversation.update(status: :open) } - .not_to have_enqueued_job(AutoResolveConversationsJob).with(conversation.id) - end - - it 'does trigger AutoResolutionJob if conversation reopened and account has auto resolve duration' do - account.update(auto_resolve_duration: 40) - conversation.resolved! - conversation.reload.update(status: :open) - expect(AutoResolveConversationsJob).to have_been_enqueued.with(conversation.id) - end end describe '#update_labels' do