Files
chatwoot/app/models/concerns/priority_activity_message_handler.rb
Shivam Mishra a34729c153 feat: add activity message for priority change (#6933)
* feat: add priority const

* feat: add toggle priority method

* feat: update controller route and specs

* refactor: status change method

* refactor: abstract label change and mute activity

* feat: add priority change_activity

* fix: interpolation for previous_changes

* refactor: reduce cognitive complexity of priority_change_activity

* refactor: move priority activity message handler to a separate module

* refactor: move typing logic to a service

* refactor: tests to reduce complexity

* fix: typo

* fix: constants

* fix: priority conditions

* fix: add a response

* fix: argument destructuring in I18n.t

---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2023-04-20 16:41:53 +05:30

34 lines
1.2 KiB
Ruby

module PriorityActivityMessageHandler
extend ActiveSupport::Concern
private
def priority_change_activity(user_name)
old_priority, new_priority = previous_changes.values_at('priority')[0]
return unless priority_change?(old_priority, new_priority)
user = Current.executed_by.instance_of?(AutomationRule) ? 'Automation System' : user_name
content = build_priority_change_content(user, old_priority, new_priority)
::Conversations::ActivityMessageJob.perform_later(self, activity_message_params(content)) if content
end
def priority_change?(old_priority, new_priority)
old_priority.present? || new_priority.present?
end
def build_priority_change_content(user_name, old_priority = nil, new_priority = nil)
change_type = get_priority_change_type(old_priority, new_priority)
I18n.t("conversations.activity.priority.#{change_type}", user_name: user_name, new_priority: new_priority, old_priority: old_priority)
end
def get_priority_change_type(old_priority, new_priority)
case [old_priority.present?, new_priority.present?]
when [true, true] then 'updated'
when [false, true] then 'added'
when [true, false] then 'removed'
end
end
end