feat: refactor SLA evaluation logic (#9133)

* feat: update SLA evaluation logic

* chore: handle nrt

* chore: handle applied_sla status

* chore: refactor spec to bring down expecations in a single block

* chore: fix process_account_applied_sla spec

* chore: add spec to test multiple nrt misses

* feat: persist sla notifications

* feat: revert persist sla notifications

* chore: refactor sla_status to include active_with_misses

* chore: refactor spec

* Update evaluate_applied_sla_service.rb

* minor refactors

* clean up

* move notification related spec

* chore: refactor notifications spec to sla_event model

---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Vishnu Narayanan
2024-03-29 02:01:43 +11:00
committed by GitHub
parent 9a1c54a82d
commit 6956436a76
7 changed files with 184 additions and 80 deletions

View File

@@ -27,7 +27,7 @@ class AppliedSla < ApplicationRecord
validates :account_id, uniqueness: { scope: %i[sla_policy_id conversation_id] }
before_validation :ensure_account_id
enum sla_status: { active: 0, hit: 1, missed: 2 }
enum sla_status: { active: 0, hit: 1, missed: 2, active_with_misses: 3 }
scope :filter_by_date_range, ->(range) { where(created_at: range) if range.present? }
scope :filter_by_inbox_id, ->(inbox_id) { where(inbox_id: inbox_id) if inbox_id.present? }

View File

@@ -32,6 +32,8 @@ class SlaEvent < ApplicationRecord
before_validation :ensure_applied_sla_id, :ensure_account_id, :ensure_inbox_id, :ensure_sla_policy_id
after_create_commit :create_notifications
private
def ensure_applied_sla_id
@@ -49,4 +51,28 @@ class SlaEvent < ApplicationRecord
def ensure_sla_policy_id
self.sla_policy_id ||= applied_sla&.sla_policy_id
end
def create_notifications
notify_users = conversation.conversation_participants.map(&:user)
# Add all admins from the account to notify list
notify_users += account.administrators
# Ensure conversation assignee is notified
notify_users += [conversation.assignee] if conversation.assignee.present?
notification_type = {
'frt' => 'sla_missed_first_response',
'nrt' => 'sla_missed_next_response',
'rt' => 'sla_missed_resolution'
}[event_type]
notify_users.uniq.each do |user|
NotificationBuilder.new(
notification_type: notification_type,
user: user,
account: account,
primary_actor: conversation,
secondary_actor: sla_policy
).perform
end
end
end