feat: Auto resolve conversations after n days of inactivity (#1308)

fixes: #418
This commit is contained in:
Akash Srivastava
2020-11-01 12:53:25 +05:30
committed by GitHub
parent 65ed4c78a4
commit 074084b258
79 changed files with 358 additions and 22 deletions

View File

@@ -130,6 +130,8 @@ RSpec.describe 'Accounts API', type: :request do
context 'when it is an authenticated user' do
it 'shows an account' do
account.update(auto_resolve_duration: 30)
get "/api/v1/accounts/#{account.id}",
headers: admin.create_new_auth_token,
as: :json
@@ -139,6 +141,7 @@ RSpec.describe 'Accounts API', type: :request do
expect(response.body).to include(account.locale)
expect(response.body).to include(account.domain)
expect(response.body).to include(account.support_email)
expect(response.body).to include(account.auto_resolve_duration.to_s)
end
end
end
@@ -169,7 +172,8 @@ RSpec.describe 'Accounts API', type: :request do
name: 'New Name',
locale: 'en',
domain: 'example.com',
support_email: 'care@example.com'
support_email: 'care@example.com',
auto_resolve_duration: 40
}
it 'modifies an account' do
@@ -183,6 +187,7 @@ RSpec.describe 'Accounts API', type: :request do
expect(account.reload.locale).to eq(params[:locale])
expect(account.reload.domain).to eq(params[:domain])
expect(account.reload.support_email).to eq(params[:support_email])
expect(account.reload.auto_resolve_duration).to eq(params[:auto_resolve_duration])
end
end
end

View File

@@ -0,0 +1,34 @@
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

View File

@@ -4,6 +4,7 @@ require 'rails_helper'
RSpec.describe Account do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_numericality_of(:auto_resolve_duration).is_greater_than_or_equal_to(1) }
it { is_expected.to have_many(:users).through(:account_users) }
it { is_expected.to have_many(:account_users) }

View File

@@ -44,6 +44,19 @@ 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
@@ -94,6 +107,26 @@ RSpec.describe Conversation, type: :model do
expect(conversation.messages.pluck(:content)).to include("Assigned to #{new_assignee.name} by #{old_assignee.name}")
expect(conversation.messages.pluck(:content)).to include("#{old_assignee.name} added #{label.title}")
end
it 'adds a message for system auto resolution if marked resolved by system' do
conversation2 = create(:conversation, status: 'open', account: account, assignee: old_assignee)
account.update(auto_resolve_duration: 40)
Current.user = nil
conversation2.update(status: :resolved)
system_resolved_message = "Conversation was marked resolved by system due to #{account.auto_resolve_duration} days of inactivity"
expect(conversation2.messages.pluck(:content)).to include(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)
expect { conversation.update(status: :open) }
.to have_enqueued_job(AutoResolveConversationsJob).with(conversation.id)
end
end
describe '#round robin' do