Files
chatwoot/enterprise/app/services/sla/evaluate_applied_sla_service.rb
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

105 lines
2.7 KiB
Ruby

class Sla::EvaluateAppliedSlaService
pattr_initialize [:applied_sla!]
def perform
return unless conversation.sla_applicable?
check_frt
check_nrt
check_rt
return unless conversation.resolved?
handle_hit_sla
end
private
delegate :conversation, :sla_policy, to: :applied_sla
def check_frt
return if sla_policy.first_response_time_threshold.blank?
return if frt_was_hit?
return if within_threshold?(applied_sla.frt_due_at)
handle_missed_sla('frt')
end
def check_nrt
return if sla_policy.next_response_time_threshold.blank?
return if conversation.first_reply_created_at.blank?
return if conversation.waiting_since.blank?
return if within_threshold?(applied_sla.nrt_due_at)
handle_missed_sla('nrt')
end
def check_rt
return if sla_policy.resolution_time_threshold.blank?
return if conversation.resolved?
return if within_threshold?(applied_sla.rt_due_at)
handle_missed_sla('rt')
end
def within_threshold?(due_at)
Time.zone.now.to_i < due_at
end
def frt_was_hit?
return false if applied_sla.frt_due_at.blank?
return false if conversation.first_reply_created_at.blank?
conversation.first_reply_created_at.to_i <= applied_sla.frt_due_at
end
def handle_missed_sla(type)
meta = type == 'nrt' ? { message_id: last_incoming_message_id } : {}
return if already_missed?(type, meta)
create_sla_event(type, meta)
log_miss(type)
applied_sla.update!(sla_status: 'active_with_misses') unless applied_sla.active_with_misses?
end
def handle_hit_sla
if applied_sla.active?
applied_sla.update!(sla_status: 'hit')
log_result('hit')
else
applied_sla.update!(sla_status: 'missed')
log_result('missed')
end
end
def already_missed?(type, meta)
SlaEvent.exists?(applied_sla: applied_sla, event_type: type, meta: meta)
end
def last_incoming_message_id
Message.where(account_id: conversation.account_id, conversation_id: conversation.id, message_type: :incoming).last&.id
end
def create_sla_event(event_type, meta)
SlaEvent.create!(
applied_sla: applied_sla,
conversation: conversation,
event_type: event_type,
meta: meta,
account: applied_sla.account,
inbox: conversation.inbox,
sla_policy: sla_policy
)
end
def log_miss(type)
Rails.logger.warn "SLA #{type} missed for conversation #{conversation.id} " \
"in account #{applied_sla.account_id} for sla_policy #{sla_policy.id}"
end
def log_result(result)
Rails.logger.info "SLA #{result} for conversation #{conversation.id} " \
"in account #{applied_sla.account_id} for sla_policy #{sla_policy.id}"
end
end