diff --git a/app/services/automation_rules/action_service.rb b/app/services/automation_rules/action_service.rb index e88ca06dd..12bb95cb9 100644 --- a/app/services/automation_rules/action_service.rb +++ b/app/services/automation_rules/action_service.rb @@ -22,6 +22,8 @@ class AutomationRules::ActionService private def send_attachment(blob_ids) + return if conversation_a_tweet? + return unless @rule.files.attached? blob = ActiveStorage::Blob.find(blob_ids) @@ -61,6 +63,8 @@ class AutomationRules::ActionService end def send_message(message) + return if conversation_a_tweet? + params = { content: message[0], private: false, content_attributes: { automation_rule_id: @rule.id } } mb = Messages::MessageBuilder.new(nil, @conversation, params) mb.perform @@ -101,4 +105,10 @@ class AutomationRules::ActionService def team_belongs_to_account?(team_ids) @account.team_ids.include?(team_ids[0]) end + + def conversation_a_tweet? + return false if @conversation.additional_attributes.blank? + + @conversation.additional_attributes['type'] == 'tweet' + end end diff --git a/spec/listeners/automation_rule_listener_spec.rb b/spec/listeners/automation_rule_listener_spec.rb index 6213e4450..6ab931554 100644 --- a/spec/listeners/automation_rule_listener_spec.rb +++ b/spec/listeners/automation_rule_listener_spec.rb @@ -4,9 +4,8 @@ describe AutomationRuleListener do let!(:account) { create(:account) } let(:inbox) { create(:inbox, account: account) } let(:contact) { create(:contact, account: account, identifier: '123') } - let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) } - let(:conversation) { create(:conversation, contact_inbox: contact_inbox, inbox: inbox, account: account) } - let(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') } + let(:conversation) { create(:conversation, inbox: inbox, account: account) } + let!(:automation_rule) { create(:automation_rule, account: account, name: 'Test Automation Rule') } let(:team) { create(:team, account: account) } let(:user_1) { create(:user, role: 0) } let(:user_2) { create(:user, role: 0) } @@ -214,30 +213,23 @@ describe AutomationRuleListener do context 'when rule matches' do it 'triggers automation rule to assign team' do expect(conversation.team_id).not_to eq(team.id) - - automation_rule listener.conversation_updated(event) - conversation.reload + expect(conversation.team_id).to eq(team.id) end it 'triggers automation rule to add label' do expect(conversation.labels).to eq([]) - - automation_rule listener.conversation_updated(event) - conversation.reload + expect(conversation.labels.pluck(:name)).to contain_exactly('support', 'priority_customer') end it 'triggers automation rule to assign best agents' do expect(conversation.assignee).to be_nil - - automation_rule listener.conversation_updated(event) - conversation.reload expect(conversation.assignee).to eq(user_1) @@ -245,21 +237,14 @@ describe AutomationRuleListener do it 'triggers automation rule send email transcript to the mentioned email' do mailer = double - - automation_rule - expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) - listener.conversation_updated(event) - conversation.reload allow(mailer).to receive(:conversation_transcript) end it 'triggers automation rule send email to the team' do - automation_rule - expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) listener.conversation_updated(event) @@ -267,13 +252,8 @@ describe AutomationRuleListener do it 'triggers automation rule send message to the contacts' do expect(conversation.messages).to be_empty - - automation_rule - expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) - listener.conversation_updated(event) - conversation.reload expect(conversation.messages.first.content).to eq('Send this message.') @@ -299,38 +279,26 @@ describe AutomationRuleListener do context 'when rule matches' do it 'triggers automation rule to assign team' do expect(conversation.team_id).not_to eq(team.id) - - automation_rule - expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) - listener.message_created(event) - conversation.reload + expect(conversation.team_id).to eq(team.id) end it 'triggers automation rule to add label' do expect(conversation.labels).to eq([]) - - automation_rule - expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) - listener.message_created(event) - conversation.reload + expect(conversation.labels.pluck(:name)).to contain_exactly('support', 'priority_customer') end it 'triggers automation rule to assign best agent' do expect(conversation.assignee).to be_nil - automation_rule - expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) - listener.message_created(event) - conversation.reload expect(conversation.assignee).to eq(user_1) @@ -338,13 +306,8 @@ describe AutomationRuleListener do it 'triggers automation rule send email transcript to the mentioned email' do mailer = double - - automation_rule - expect(TeamNotifications::AutomationNotificationMailer).to receive(:conversation_creation) - listener.message_created(event) - conversation.reload allow(mailer).to receive(:conversation_transcript) @@ -381,21 +344,18 @@ describe AutomationRuleListener do context 'when rule matches' do it 'triggers automation rule send email transcript to the mentioned email' do mailer = double - - automation_rule + allow(ConversationReplyMailer).to receive(:with).and_return(mailer) + allow(mailer).to receive(:conversation_transcript) listener.message_created(event) - conversation.reload - allow(mailer).to receive(:conversation_transcript) + expect(mailer).to have_received(:conversation_transcript).with(conversation, 'new_agent@example.com') end it 'triggers automation rule send message to the contacts' do expect(conversation.messages.count).to eq(1) - listener.message_created(event) - conversation.reload expect(conversation.messages.count).to eq(2) @@ -416,9 +376,7 @@ describe AutomationRuleListener do it 'triggers automation rule but wont send message' do expect(conversation.messages.count).to eq(1) - listener.message_created(event) - conversation.reload expect(conversation.messages.count).to eq(1) @@ -455,14 +413,14 @@ describe AutomationRuleListener do context 'when rule matches' do it 'triggers automation rule send email transcript to the mentioned email' do mailer = double - - automation_rule + allow(ConversationReplyMailer).to receive(:with).and_return(mailer) + allow(mailer).to receive(:conversation_transcript) listener.conversation_created(event) conversation.reload - allow(mailer).to receive(:conversation_transcript) + expect(mailer).to have_received(:conversation_transcript).with(conversation, 'new_agent@example.com') end it 'triggers automation rule send message to the contacts' do @@ -501,4 +459,48 @@ describe AutomationRuleListener do end end end + + describe '#message_created for tweet events' do + before do + automation_rule.update!( + event_name: 'message_created', + name: 'Call actions message created', + description: 'Send Message in the conversation', + conditions: [ + { attribute_key: 'status', filter_operator: 'equal_to', values: ['open'], query_operator: nil }.with_indifferent_access + ], + actions: [ + { 'action_name' => 'send_message', 'action_params' => ['Send this message.'] }, + { 'action_name' => 'send_attachment', 'action_params' => [123] }, + { 'action_name' => 'send_email_transcript', 'action_params' => ['new_agent@example.com'] } + ] + ) + end + + context 'when rule matches' do + let(:tweet) { create(:conversation, additional_attributes: { type: 'tweet' }, inbox: inbox, account: account) } + let(:event) { Events::Base.new('message_created', Time.zone.now, { conversation: tweet, message: message }) } + let!(:message) { create(:message, account: account, conversation: tweet, message_type: 'incoming') } + + it 'triggers automation rule except send_message and send_attachment' do + mailer = double + allow(ConversationReplyMailer).to receive(:with).and_return(mailer) + allow(mailer).to receive(:conversation_transcript) + + listener.message_created(event) + expect(mailer).to have_received(:conversation_transcript).with(tweet, 'new_agent@example.com') + end + + it 'does not triggers automation rule send message or send attachment' do + expect(tweet.messages.count).to eq(1) + + listener.message_created(event) + + tweet.reload + + expect(tweet.messages.count).to eq(1) + expect(tweet.messages.last.content).to eq(message.content) + end + end + end end