From e85f998a08114d5a8da67d28a229fc26f692e9fa Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Thu, 17 Nov 2022 12:30:47 +0530 Subject: [PATCH] feat: Remove labels in macro (#5875) --- .../dashboard/mixins/macrosMixin.js | 1 + .../conversation/Macros/MacroPreview.vue | 1 + .../dashboard/settings/macros/constants.js | 5 +++++ app/models/macro.rb | 2 +- app/services/action_service.rb | 7 +++++++ .../api/v1/accounts/macros_controller_spec.rb | 20 ++++++++++++++++++- 6 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/javascript/dashboard/mixins/macrosMixin.js b/app/javascript/dashboard/mixins/macrosMixin.js index add06b7e1..b7124ff68 100644 --- a/app/javascript/dashboard/mixins/macrosMixin.js +++ b/app/javascript/dashboard/mixins/macrosMixin.js @@ -8,6 +8,7 @@ export default { case 'assign_agent': return this.agents; case 'add_label': + case 'remove_label': return this.labels.map(i => { return { id: i.title, diff --git a/app/javascript/dashboard/routes/dashboard/conversation/Macros/MacroPreview.vue b/app/javascript/dashboard/routes/dashboard/conversation/Macros/MacroPreview.vue index 081d18e1b..c9f4258dd 100644 --- a/app/javascript/dashboard/routes/dashboard/conversation/Macros/MacroPreview.vue +++ b/app/javascript/dashboard/routes/dashboard/conversation/Macros/MacroPreview.vue @@ -49,6 +49,7 @@ export default { const actionsMap = { assign_team: resolveTeamIds(this.teams, params), add_label: resolveLabels(this.labels, params), + remove_label: resolveLabels(this.labels, params), assign_agent: resolveAgents(this.agents, params), mute_conversation: null, snooze_conversation: null, diff --git a/app/javascript/dashboard/routes/dashboard/settings/macros/constants.js b/app/javascript/dashboard/routes/dashboard/settings/macros/constants.js index b13a9bbd9..713e47a92 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/macros/constants.js +++ b/app/javascript/dashboard/routes/dashboard/settings/macros/constants.js @@ -14,6 +14,11 @@ export const MACRO_ACTION_TYPES = [ label: 'Add a label', inputType: 'multi_select', }, + { + key: 'remove_label', + label: 'Remove a label', + inputType: 'multi_select', + }, { key: 'send_email_transcript', label: 'Send an email transcript', diff --git a/app/models/macro.rb b/app/models/macro.rb index e6f3425ac..d9275ca8f 100644 --- a/app/models/macro.rb +++ b/app/models/macro.rb @@ -30,7 +30,7 @@ class Macro < ApplicationRecord validate :json_actions_format - ACTIONS_ATTRS = %w[send_message add_label assign_team assign_agent mute_conversation change_status + ACTIONS_ATTRS = %w[send_message add_label assign_team assign_agent mute_conversation change_status remove_label resolve_conversation snooze_conversation send_email_transcript send_attachment add_private_note].freeze def set_visibility(user, params) diff --git a/app/services/action_service.rb b/app/services/action_service.rb index ce57971cf..5cde001bb 100644 --- a/app/services/action_service.rb +++ b/app/services/action_service.rb @@ -33,6 +33,13 @@ class ActionService @conversation.update!(assignee_id: @agent.id) if @agent.present? end + def remove_label(labels) + return if labels.empty? + + labels = @conversation.label_list - labels + @conversation.update(label_list: labels) + end + def assign_team(team_ids = []) return unless team_belongs_to_account?(team_ids) diff --git a/spec/controllers/api/v1/accounts/macros_controller_spec.rb b/spec/controllers/api/v1/accounts/macros_controller_spec.rb index 3b791d002..6ab8424bd 100644 --- a/spec/controllers/api/v1/accounts/macros_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/macros_controller_spec.rb @@ -72,7 +72,7 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do context 'when it is an authenticated user' do let(:params) do { - 'name': 'Add label, send message and close the chat', + 'name': 'Add label, send message and close the chat, remove label', 'actions': [ { 'action_name': :add_label, @@ -84,6 +84,10 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do }, { 'action_name': :resolve_conversation + }, + { + 'action_name': :remove_label, + 'action_params': %w[support] } ], visibility: 'global', @@ -348,6 +352,20 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do expect(conversation.reload.status).to eql('snoozed') end + it 'Remove selected label' do + macro.update!(actions: [{ 'action_name' => 'remove_label', 'action_params' => ['support'] }]) + conversation.add_labels(%w[support priority_customer]) + expect(conversation.label_list).to match_array(%w[support priority_customer]) + + perform_enqueued_jobs do + post "/api/v1/accounts/#{account.id}/macros/#{macro.id}/execute", + params: { conversation_ids: [conversation.display_id] }, + headers: administrator.create_new_auth_token + end + + expect(conversation.reload.label_list).to match_array(%w[priority_customer]) + end + it 'Adds the private note' do expect(conversation.messages).to be_empty