feat: Auto resolve conversations after n days of inactivity (#1308)
fixes: #418
This commit is contained in:
@@ -54,11 +54,13 @@ class Conversation < ApplicationRecord
|
||||
# wanted to change this to after_update commit. But it ended up creating a loop
|
||||
# reinvestigate in future and identity the implications
|
||||
after_update :notify_status_change, :create_activity
|
||||
after_create_commit :notify_conversation_creation
|
||||
after_create_commit :notify_conversation_creation, :queue_conversation_auto_resolution_job
|
||||
after_save :run_round_robin
|
||||
|
||||
acts_as_taggable_on :labels
|
||||
|
||||
delegate :auto_resolve_duration, to: :account
|
||||
|
||||
def can_reply?
|
||||
return true unless inbox&.channel&.has_24_hour_messaging_window?
|
||||
|
||||
@@ -145,6 +147,12 @@ class Conversation < ApplicationRecord
|
||||
dispatcher_dispatch(CONVERSATION_CREATED)
|
||||
end
|
||||
|
||||
def queue_conversation_auto_resolution_job
|
||||
return unless auto_resolve_duration
|
||||
|
||||
AutoResolveConversationsJob.set(wait_until: (last_activity_at || created_at) + auto_resolve_duration.days).perform_later(id)
|
||||
end
|
||||
|
||||
def self_assign?(assignee_id)
|
||||
assignee_id.present? && Current.user&.id == assignee_id
|
||||
end
|
||||
@@ -157,15 +165,17 @@ class Conversation < ApplicationRecord
|
||||
end
|
||||
|
||||
def create_activity
|
||||
return unless Current.user
|
||||
|
||||
user_name = Current.user.name
|
||||
|
||||
create_status_change_message(user_name) if saved_change_to_status?
|
||||
user_name = Current.user.name if Current.user.present?
|
||||
status_change_activity(user_name) if saved_change_to_status?
|
||||
create_assignee_change(user_name) if saved_change_to_assignee_id?
|
||||
create_label_change(user_name) if saved_change_to_label_list?
|
||||
end
|
||||
|
||||
def status_change_activity(user_name)
|
||||
create_status_change_message(user_name)
|
||||
queue_conversation_auto_resolution_job if open?
|
||||
end
|
||||
|
||||
def activity_message_params(content)
|
||||
{ account_id: account_id, inbox_id: inbox_id, message_type: :activity, content: content }
|
||||
end
|
||||
@@ -210,12 +220,18 @@ class Conversation < ApplicationRecord
|
||||
end
|
||||
|
||||
def create_status_change_message(user_name)
|
||||
content = I18n.t("conversations.activity.status.#{status}", user_name: user_name)
|
||||
content = if user_name
|
||||
I18n.t("conversations.activity.status.#{status}", user_name: user_name)
|
||||
elsif resolved?
|
||||
I18n.t('conversations.activity.status.auto_resolved', duration: auto_resolve_duration)
|
||||
end
|
||||
|
||||
messages.create(activity_message_params(content))
|
||||
messages.create(activity_message_params(content)) if content
|
||||
end
|
||||
|
||||
def create_assignee_change(user_name)
|
||||
return unless user_name
|
||||
|
||||
params = { assignee_name: assignee.name, user_name: user_name }.compact
|
||||
key = assignee_id ? 'assigned' : 'removed'
|
||||
key = 'self_assigned' if self_assign? assignee_id
|
||||
@@ -225,6 +241,8 @@ class Conversation < ApplicationRecord
|
||||
end
|
||||
|
||||
def create_label_change(user_name)
|
||||
return unless user_name
|
||||
|
||||
previous_labels, current_labels = previous_changes[:label_list]
|
||||
return unless (previous_labels.is_a? Array) && (current_labels.is_a? Array)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user