Restructure omni services and add Chatwoot research snapshot

This commit is contained in:
Ruslan Bakiev
2026-02-21 11:11:27 +07:00
parent edea7a0034
commit b73babbbf6
7732 changed files with 978203 additions and 32 deletions

View File

@@ -0,0 +1,22 @@
require 'rails_helper'
RSpec.describe Conversations::ReopenSnoozedConversationsJob do
let!(:snoozed_till_5_minutes_ago) { create(:conversation, status: :snoozed, snoozed_until: 5.minutes.ago) }
let!(:snoozed_till_tomorrow) { create(:conversation, status: :snoozed, snoozed_until: 1.day.from_now) }
let!(:snoozed_indefinitely) { create(:conversation, status: :snoozed) }
it 'enqueues the job' do
expect { described_class.perform_later }.to have_enqueued_job(described_class)
.on_queue('low')
end
context 'when called' do
it 'reopens snoozed conversations whose snooze until has passed' do
described_class.perform_now
expect(snoozed_till_5_minutes_ago.reload.status).to eq 'open'
expect(snoozed_till_tomorrow.reload.status).to eq 'snoozed'
expect(snoozed_indefinitely.reload.status).to eq 'snoozed'
end
end
end

View File

@@ -0,0 +1,83 @@
require 'rails_helper'
RSpec.describe Conversations::ResolutionJob do
subject(:job) { described_class.perform_later(account: account) }
let!(:account) { create(:account) }
let(:label) { create(:label, title: 'auto-resolved', account: account) }
let!(:conversation) { create(:conversation, account: account) }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(account: account)
.on_queue('low')
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
context 'when auto_resolve_ignore_waiting is true' do
it 'resolves non-waiting conversations if time of inactivity is more than auto resolve duration' do
account.update(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: true) # 10 days in minutes
conversation.update(last_activity_at: 13.days.ago, waiting_since: nil)
described_class.perform_now(account: account)
expect(conversation.reload.status).to eq('resolved')
end
it 'does not resolve waiting conversations even if time of inactivity is more than auto resolve duration' do
account.update(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: true) # 10 days in minutes
conversation.update(last_activity_at: 13.days.ago, waiting_since: 13.days.ago)
described_class.perform_now(account: account)
expect(conversation.reload.status).to eq('open')
end
end
context 'when auto_resolve_ignore_waiting is false' do
it 'resolves all conversations if time of inactivity is more than auto resolve duration' do
account.update(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: false) # 10 days in minutes
# Create one waiting conversation and one non-waiting conversation
waiting_conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: 13.days.ago)
non_waiting_conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: nil)
described_class.perform_now(account: account)
expect(waiting_conversation.reload.status).to eq('resolved')
expect(non_waiting_conversation.reload.status).to eq('resolved')
end
end
# When a contact is deleted, there's a brief window (~50-150ms) where contact_id becomes nil
# but conversations still exist. If ResolutionJob runs during this window, muted? can crash
# trying to call blocked? on nil. Fixes # (issue).
it 'skips orphan conversations without a contact' do
account.update(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: false) # 10 days in minutes
orphan_conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: nil)
orphan_conversation.update_columns(contact_id: nil, contact_inbox_id: nil) # rubocop:disable Rails/SkipsModelValidations
resolvable_conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: nil)
described_class.perform_now(account: account)
expect(orphan_conversation.reload.status).to eq('open')
expect(resolvable_conversation.reload.status).to eq('resolved')
end
it 'adds a label after resolution' do
account.update(auto_resolve_label: 'auto-resolved', auto_resolve_after: 14_400)
conversation = create(:conversation, account: account, last_activity_at: 13.days.ago, waiting_since: 13.days.ago)
described_class.perform_now(account: account)
expect(conversation.reload.status).to eq('resolved')
expect(conversation.reload.label_list).to include('auto-resolved')
end
it 'resolves only a limited number of conversations in a single execution' do
stub_const('Limits::BULK_ACTIONS_LIMIT', 2)
account.update(auto_resolve_after: 14_400, auto_resolve_ignore_waiting: false) # 10 days in minutes
create_list(:conversation, 3, account: account, last_activity_at: 13.days.ago)
described_class.perform_now(account: account)
expect(account.conversations.resolved.count).to eq(Limits::BULK_ACTIONS_LIMIT)
end
end

View File

@@ -0,0 +1,80 @@
require 'rails_helper'
RSpec.describe Conversations::UpdateMessageStatusJob do
subject(:job) { described_class.perform_later(conversation.id, conversation.contact_last_seen_at, :read) }
let!(:account) { create(:account) }
let!(:conversation) { create(:conversation, account: account, contact_last_seen_at: DateTime.now.utc) }
let!(:message) { create(:message, conversation: conversation, message_type: 'outgoing', status: 'sent', created_at: 1.day.ago) }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(conversation.id, conversation.contact_last_seen_at, :read)
.on_queue('deferred')
end
context 'when called' do
it 'marks all sent messages in a conversation as read' do
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at)
message.reload
end.to change(message, :status).from('sent').to('read')
end
it 'marks all sent messages in a conversation as delivered if specified' do
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at, :delivered)
message.reload
end.to change(message, :status).from('sent').to('delivered')
end
it 'marks all delivered messages in a conversation as read' do
message.update!(status: 'delivered')
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at)
message.reload
end.to change(message, :status).from('delivered').to('read')
end
it 'marks all templates messages in a conversation as read' do
message.update!(status: 'delivered', message_type: 'template')
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at)
message.reload
end.to change(message, :status).from('delivered').to('read')
end
it 'does not mark failed messages as read' do
message.update!(status: 'failed')
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at)
end.not_to change(message.reload, :status)
end
it 'does not mark incoming messages as read' do
message.update!(message_type: 'incoming')
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at)
end.not_to change(message.reload, :status)
end
it 'does not mark messages created after the contact last seen time as read' do
message.update!(created_at: DateTime.now.utc)
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at)
end.not_to change(message.reload, :status)
end
it 'does not run the job if the conversation does not exist' do
expect do
described_class.perform_now(1212, conversation.contact_last_seen_at)
end.not_to change(message.reload, :status)
end
it 'does not run the job if the status is failed' do
expect do
described_class.perform_now(conversation.id, conversation.contact_last_seen_at, :failed)
end.not_to change(message.reload, :status)
end
end
end