feat: Activity message for label addition & deletion (#1291)

Fixes: #1022
This commit is contained in:
Abhishek 2020-10-02 19:03:59 +08:00 committed by GitHub
parent 98cb09104e
commit 70f08103aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 1 deletions

View file

@ -156,6 +156,7 @@ class Conversation < ApplicationRecord
create_status_change_message(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 activity_message_params(content)
@ -216,6 +217,32 @@ class Conversation < ApplicationRecord
messages.create(activity_message_params(content))
end
def create_label_change(user_name)
previous_labels, current_labels = previous_changes[:label_list]
return unless (previous_labels.is_a? Array) && (current_labels.is_a? Array)
create_label_added(user_name, current_labels - previous_labels)
create_label_removed(user_name, previous_labels - current_labels)
end
def create_label_added(user_name, labels = [])
return unless labels.size.positive?
params = { user_name: user_name, labels: labels.join(', ') }
content = I18n.t('conversations.activity.labels.added', **params)
messages.create(activity_message_params(content))
end
def create_label_removed(user_name, labels = [])
return unless labels.size.positive?
params = { user_name: user_name, labels: labels.join(', ') }
content = I18n.t('conversations.activity.labels.removed', **params)
messages.create(activity_message_params(content))
end
def mute_key
format('CONVERSATION::%<id>d::MUTED', id: id)
end

View file

@ -51,6 +51,9 @@ en:
self_assigned: "%{user_name} self-assigned this conversation"
assigned: "Assigned to %{assignee_name} by %{user_name}"
removed: "Conversation unassigned by %{user_name}"
labels:
added: "%{user_name} added %{labels}"
removed: "%{user_name} removed %{labels}"
templates:
greeting_message_body: "%{account_name} typically replies in a few hours."
ways_to_reach_you_message_body: "Give the team a way to reach you."

View file

@ -58,6 +58,7 @@ RSpec.describe Conversation, type: :model do
create(:user, email: 'agent2@example.com', account: account, role: :agent)
end
let(:assignment_mailer) { double(deliver: true) }
let(:label) { create(:label, account: account) }
before do
conversation
@ -70,7 +71,8 @@ RSpec.describe Conversation, type: :model do
status: :resolved,
locked: true,
contact_last_seen_at: Time.now,
assignee: new_assignee
assignee: new_assignee,
label_list: [label.title]
)
end
@ -90,6 +92,7 @@ RSpec.describe Conversation, type: :model do
# create_activity
expect(conversation.messages.pluck(:content)).to include("Conversation was marked resolved by #{old_assignee.available_name}")
expect(conversation.messages.pluck(:content)).to include("Assigned to #{new_assignee.available_name} by #{old_assignee.available_name}")
expect(conversation.messages.pluck(:content)).to include("#{old_assignee.available_name} added #{label.title}")
end
end
@ -195,6 +198,48 @@ RSpec.describe Conversation, type: :model do
end
end
describe '#update_labels' do
let(:account) { create(:account) }
let(:conversation) { create(:conversation, account: account) }
let(:agent) do
create(:user, email: 'agent@example.com', account: account, role: :agent)
end
let(:first_label) { create(:label, account: account) }
let(:second_label) { create(:label, account: account) }
let(:third_label) { create(:label, account: account) }
let(:fourth_label) { create(:label, account: account) }
before do
conversation
Current.user = agent
first_label
second_label
third_label
fourth_label
end
it 'adds one label to conversation' do
labels = [first_label].map(&:title)
expect(conversation.update_labels(labels)).to eq(true)
expect(conversation.label_list).to match_array(labels)
expect(conversation.messages.pluck(:content)).to include("#{agent.available_name} added #{labels.join(', ')}")
end
it 'adds and removes previously added labels' do
labels = [first_label, fourth_label].map(&:title)
expect(conversation.update_labels(labels)).to eq(true)
expect(conversation.label_list).to match_array(labels)
expect(conversation.messages.pluck(:content)).to include("#{agent.available_name} added #{labels.join(', ')}")
updated_labels = [second_label, third_label].map(&:title)
expect(conversation.update_labels(updated_labels)).to eq(true)
expect(conversation.label_list).to match_array(updated_labels)
expect(conversation.messages.pluck(:content)).to include("#{agent.available_name} added #{updated_labels.join(', ')}")
expect(conversation.messages.pluck(:content)).to include("#{agent.available_name} removed #{labels.join(', ')}")
end
end
describe '#toggle_status' do
subject(:toggle_status) { conversation.toggle_status }