Files
7bf76057c2 fix: SLA handling for blocked contacts (#14861)
# Pull Request Template

## Description

Blocked contacts are now excluded from SLA assignment, processing,
reports, and conversation SLA UI while they remain blocked. Existing SLA
records are preserved, and SLA behavior resumes if the contact is
unblocked.

Fixes
https://linear.app/chatwoot/issue/CW-7435/sla-should-not-trigger-for-blocked-contacts

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

- `bundle exec rspec spec/enterprise/models/conversation_spec.rb
spec/enterprise/models/applied_sla_spec.rb
spec/enterprise/services/enterprise/action_service_spec.rb
spec/enterprise/services/sla/evaluate_applied_sla_service_spec.rb
spec/enterprise/jobs/sla/process_account_applied_slas_job_spec.rb
spec/enterprise/controllers/api/v1/accounts/applied_slas_controller_spec.rb
spec/enterprise/controllers/api/v1/accounts/conversations_controller_spec.rb
spec/enterprise/controllers/enterprise/api/v1/accounts/conversations_controller_spec.rb
spec/enterprise/presenters/conversations/event_data_presenter_spec.rb` —
78 examples, 0 failures
- `bundle exec rubocop
enterprise/app/controllers/api/v1/accounts/applied_slas_controller.rb
enterprise/app/jobs/sla/process_account_applied_slas_job.rb
enterprise/app/models/applied_sla.rb
enterprise/app/models/enterprise/concerns/conversation.rb
enterprise/app/presenters/enterprise/conversations/event_data_presenter.rb
enterprise/app/services/enterprise/action_service.rb
enterprise/app/services/sla/evaluate_applied_sla_service.rb
lib/tasks/apply_sla.rake
spec/enterprise/controllers/api/v1/accounts/applied_slas_controller_spec.rb
spec/enterprise/controllers/api/v1/accounts/conversations_controller_spec.rb
spec/enterprise/controllers/enterprise/api/v1/accounts/conversations_controller_spec.rb
spec/enterprise/jobs/sla/process_account_applied_slas_job_spec.rb
spec/enterprise/models/applied_sla_spec.rb
spec/enterprise/models/conversation_spec.rb
spec/enterprise/presenters/conversations/event_data_presenter_spec.rb
spec/enterprise/services/enterprise/action_service_spec.rb
spec/enterprise/services/sla/evaluate_applied_sla_service_spec.rb` — no
offenses
- `pnpm exec vitest --no-watch --no-cache --no-coverage
app/javascript/dashboard/components/widgets/conversation/specs/ConversationCard.spec.js`
— 2 tests passed
- `pnpm exec eslint
app/javascript/dashboard/components-next/Conversation/ConversationCard/CardMessagePreviewWithMeta.vue
app/javascript/dashboard/components-next/Conversation/ConversationCard/ConversationCardExpanded.vue
app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue
app/javascript/dashboard/components/widgets/conversation/ConversationHeader.vue
app/javascript/dashboard/components/widgets/conversation/specs/ConversationCard.spec.js`
— passed with existing raw-text warnings in `ConversationHeader.vue`
- `git diff --cached --check` — clean

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

---------

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2026-07-02 14:16:29 +05:30

169 lines
6.6 KiB
Ruby

require 'rails_helper'
RSpec.describe AppliedSla, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:sla_policy) }
it { is_expected.to belong_to(:account) }
it { is_expected.to belong_to(:conversation) }
end
describe 'push_event_data' do
it 'returns the correct hash' do
applied_sla = create(:applied_sla)
expect(applied_sla.push_event_data).to eq(
{
id: applied_sla.id,
sla_id: applied_sla.sla_policy_id,
sla_status: applied_sla.sla_status,
created_at: applied_sla.created_at.to_i,
updated_at: applied_sla.updated_at.to_i,
sla_description: applied_sla.sla_policy.description,
sla_name: applied_sla.sla_policy.name,
sla_first_response_time_threshold: applied_sla.sla_policy.first_response_time_threshold,
sla_next_response_time_threshold: applied_sla.sla_policy.next_response_time_threshold,
sla_only_during_business_hours: applied_sla.sla_policy.only_during_business_hours,
sla_resolution_time_threshold: applied_sla.sla_policy.resolution_time_threshold,
sla_frt_due_at: applied_sla.frt_due_at,
sla_nrt_due_at: applied_sla.nrt_due_at,
sla_rt_due_at: applied_sla.rt_due_at
}
)
end
it 'shares the working hours cache while serializing due times' do
account = create(:account)
inbox = create(:inbox, account: account, working_hours_enabled: true, timezone: 'UTC')
sla_policy = create(
:sla_policy,
account: account,
first_response_time_threshold: 1.hour,
next_response_time_threshold: 30.minutes,
resolution_time_threshold: 2.hours,
only_during_business_hours: true
)
start_time = Time.zone.parse('2024-01-17 10:00:00')
conversation = create(
:conversation,
account: account,
inbox: inbox,
created_at: start_time,
waiting_since: start_time + 1.hour
)
conversation.update!(waiting_since: start_time + 1.hour)
applied_sla = create(:applied_sla, account: account, conversation: conversation, sla_policy: sla_policy)
working_hours = inbox.working_hours
expect(working_hours).to receive(:index_by).once.and_call_original
expect(applied_sla.push_event_data).to include(
sla_frt_due_at: Time.zone.parse('2024-01-17 11:00:00').to_i,
sla_nrt_due_at: Time.zone.parse('2024-01-17 11:30:00').to_i,
sla_rt_due_at: Time.zone.parse('2024-01-17 12:00:00').to_i
)
end
end
describe 'validates_factory' do
it 'creates valid applied sla policy object' do
applied_sla = create(:applied_sla)
expect(applied_sla.sla_status).to eq 'active'
end
end
describe '.with_sla_applicable_conversation' do
it 'excludes blocked contacts and keeps conversations with missing contacts' do
applied_sla = create(:applied_sla)
blocked_applied_sla = create(:applied_sla)
missing_contact_applied_sla = create(:applied_sla)
blocked_applied_sla.conversation.contact.update!(blocked: true)
missing_contact_applied_sla.conversation.update_columns(contact_id: nil, contact_inbox_id: nil) # rubocop:disable Rails/SkipsModelValidations
expect(described_class.with_sla_applicable_conversation).to include(applied_sla, missing_contact_applied_sla)
expect(described_class.with_sla_applicable_conversation).not_to include(blocked_applied_sla)
end
end
describe '#frt_due_at' do
it 'returns nil when first_response_time_threshold is blank' do
applied_sla = create(:applied_sla)
applied_sla.sla_policy.update!(first_response_time_threshold: nil)
expect(applied_sla.frt_due_at).to be_nil
end
it 'returns deadline based on conversation created_at' do
applied_sla = create(:applied_sla)
applied_sla.sla_policy.update!(first_response_time_threshold: 3600, only_during_business_hours: false)
expected_deadline = applied_sla.conversation.created_at.to_i + 3600
expect(applied_sla.frt_due_at).to eq(expected_deadline)
end
end
describe '#nrt_due_at' do
it 'returns nil when next_response_time_threshold is blank' do
applied_sla = create(:applied_sla)
applied_sla.sla_policy.update!(next_response_time_threshold: nil)
expect(applied_sla.nrt_due_at).to be_nil
end
it 'returns nil when waiting_since is blank' do
applied_sla = create(:applied_sla)
applied_sla.sla_policy.update!(next_response_time_threshold: 1800)
applied_sla.conversation.update!(waiting_since: nil)
expect(applied_sla.nrt_due_at).to be_nil
end
it 'returns deadline based on waiting_since' do
applied_sla = create(:applied_sla)
waiting_since = 2.hours.ago
applied_sla.sla_policy.update!(next_response_time_threshold: 1800, only_during_business_hours: false)
applied_sla.conversation.update!(waiting_since: waiting_since)
expected_deadline = waiting_since.to_i + 1800
expect(applied_sla.nrt_due_at).to eq(expected_deadline)
end
end
describe '#rt_due_at' do
it 'returns nil when resolution_time_threshold is blank' do
applied_sla = create(:applied_sla)
applied_sla.sla_policy.update!(resolution_time_threshold: nil)
expect(applied_sla.rt_due_at).to be_nil
end
it 'returns deadline based on conversation created_at' do
applied_sla = create(:applied_sla)
applied_sla.sla_policy.update!(resolution_time_threshold: 7200, only_during_business_hours: false)
expected_deadline = applied_sla.conversation.created_at.to_i + 7200
expect(applied_sla.rt_due_at).to eq(expected_deadline)
end
end
describe '#calculate_due_at' do
it 'uses BusinessHoursService when only_during_business_hours is true' do
account = create(:account)
inbox = create(:inbox, account: account, working_hours_enabled: true)
sla_policy = create(:sla_policy, account: account, first_response_time_threshold: 3600, only_during_business_hours: true)
conversation = create(:conversation, account: account, inbox: inbox)
applied_sla = create(:applied_sla, sla_policy: sla_policy, conversation: conversation, account: account)
expect(Sla::BusinessHoursService).to receive(:new).and_call_original
applied_sla.frt_due_at
end
it 'does not use BusinessHoursService when only_during_business_hours is false' do
applied_sla = create(:applied_sla)
applied_sla.sla_policy.update!(first_response_time_threshold: 3600, only_during_business_hours: false)
expect(Sla::BusinessHoursService).not_to receive(:new)
applied_sla.frt_due_at
end
end
end