From 4a299a9441d338cd2ff8286df9213da49da1b455 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Sat, 22 Oct 2022 04:42:35 +0530 Subject: [PATCH] fix: Add an action cable events for label updates (#5694) Co-authored-by: Pranav Raj S --- .../store/modules/conversations/actions.js | 6 ++++++ app/models/conversation.rb | 2 ++ .../conversations/event_data_presenter.rb | 1 + spec/listeners/action_cable_listener_spec.rb | 15 +++++++++++++++ spec/models/conversation_spec.rb | 18 ++++++++++++++++-- .../conversations/event_data_presenter_spec.rb | 1 + 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/javascript/dashboard/store/modules/conversations/actions.js b/app/javascript/dashboard/store/modules/conversations/actions.js index 22c6721c1..e38f1a7b2 100644 --- a/app/javascript/dashboard/store/modules/conversations/actions.js +++ b/app/javascript/dashboard/store/modules/conversations/actions.js @@ -252,6 +252,12 @@ const actions = { meta: { sender }, } = conversation; commit(types.UPDATE_CONVERSATION, conversation); + + dispatch('conversationLabels/setConversationLabel', { + id: conversation.id, + data: conversation.labels, + }); + dispatch('contacts/setContact', sender); }, diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 29eab8433..3a1abf335 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -262,6 +262,8 @@ class Conversation < ApplicationRecord previous_labels, current_labels = previous_changes[:label_list] return unless (previous_labels.is_a? Array) && (current_labels.is_a? Array) + dispatcher_dispatch(CONVERSATION_UPDATED, previous_changes) + create_label_added(user_name, current_labels - previous_labels) create_label_removed(user_name, previous_labels - current_labels) end diff --git a/app/presenters/conversations/event_data_presenter.rb b/app/presenters/conversations/event_data_presenter.rb index 5981af051..d7e170bfa 100644 --- a/app/presenters/conversations/event_data_presenter.rb +++ b/app/presenters/conversations/event_data_presenter.rb @@ -8,6 +8,7 @@ class Conversations::EventDataPresenter < SimpleDelegator id: display_id, inbox_id: inbox_id, messages: push_messages, + labels: label_list, meta: push_meta, status: status, custom_attributes: custom_attributes, diff --git a/spec/listeners/action_cable_listener_spec.rb b/spec/listeners/action_cable_listener_spec.rb index b0c14c4d0..fc8c7f589 100644 --- a/spec/listeners/action_cable_listener_spec.rb +++ b/spec/listeners/action_cable_listener_spec.rb @@ -133,6 +133,10 @@ describe ActionCableListener do let(:event_name) { :'conversation.updated' } let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) } + before do + conversation.add_labels(['support']) + end + it 'sends update to inbox members' do expect(conversation.inbox.reload.inbox_members.count).to eq(1) @@ -143,5 +147,16 @@ describe ActionCableListener do ) listener.conversation_updated(event) end + + it 'broadcast event with label data' do + expect(conversation.push_event_data[:labels]).to eq(conversation.label_list) + + expect(ActionCableBroadcastJob).to receive(:perform_later).with( + [agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], + 'conversation.updated', + conversation.push_event_data.merge(account_id: account.id) + ) + listener.conversation_updated(event) + end end end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 3271bdf19..309b28afe 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -109,12 +109,25 @@ RSpec.describe Conversation, type: :model do Current.user = old_assignee end + it 'sends conversation updated event if labels are updated' do + conversation.update(label_list: [label.title]) + changed_attributes = conversation.previous_changes + expect(Rails.configuration.dispatcher).to have_received(:dispatch) + .with( + described_class::CONVERSATION_UPDATED, + kind_of(Time), + conversation: conversation, + notifiable_assignee_change: false, + changed_attributes: changed_attributes, + performed_by: nil + ) + end + it 'runs after_update callbacks' do conversation.update( status: :resolved, contact_last_seen_at: Time.now, - assignee: new_assignee, - label_list: [label.title] + assignee: new_assignee ) status_change = conversation.status_change changed_attributes = conversation.previous_changes @@ -433,6 +446,7 @@ RSpec.describe Conversation, type: :model do }, id: conversation.display_id, messages: [], + labels: [], inbox_id: conversation.inbox_id, status: conversation.status, contact_inbox: conversation.contact_inbox, diff --git a/spec/presenters/conversations/event_data_presenter_spec.rb b/spec/presenters/conversations/event_data_presenter_spec.rb index c883597a8..534d4b8fc 100644 --- a/spec/presenters/conversations/event_data_presenter_spec.rb +++ b/spec/presenters/conversations/event_data_presenter_spec.rb @@ -19,6 +19,7 @@ RSpec.describe Conversations::EventDataPresenter do id: conversation.display_id, messages: [], inbox_id: conversation.inbox_id, + labels: [], status: conversation.status, contact_inbox: conversation.contact_inbox, can_reply: conversation.can_reply?,