From ac2adfbd572e0a2ce6f22d6b78be80e9bf50a1df Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 5 Jul 2022 17:49:41 +0200 Subject: [PATCH] chore: Append incoming SMS to last conversation (#4906) - Append incoming SMS to the last conversation created for a contact --- app/services/sms/incoming_message_service.rb | 2 +- .../sms/incoming_message_service_spec.rb | 71 +++++++++---------- .../whatsapp/incoming_message_service_spec.rb | 17 +++++ 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/app/services/sms/incoming_message_service.rb b/app/services/sms/incoming_message_service.rb index a21f76b58..ec9cf0a21 100644 --- a/app/services/sms/incoming_message_service.rb +++ b/app/services/sms/incoming_message_service.rb @@ -57,7 +57,7 @@ class Sms::IncomingMessageService end def set_conversation - @conversation = @contact_inbox.conversations.first + @conversation = @contact_inbox.conversations.last return if @conversation @conversation = ::Conversation.create!(conversation_params) diff --git a/spec/services/sms/incoming_message_service_spec.rb b/spec/services/sms/incoming_message_service_spec.rb index c5f576b4a..6dd8c56d5 100644 --- a/spec/services/sms/incoming_message_service_spec.rb +++ b/spec/services/sms/incoming_message_service_spec.rb @@ -3,55 +3,54 @@ require 'rails_helper' describe Sms::IncomingMessageService do describe '#perform' do let!(:sms_channel) { create(:channel_sms) } + let(:params) do + { + + 'id': '3232420-2323-234324', + 'owner': sms_channel.phone_number, + 'applicationId': '2342349-324234d-32432432', + 'time': '2022-02-02T23:14:05.262Z', + 'segmentCount': 1, + 'direction': 'in', + 'to': [ + sms_channel.phone_number + ], + 'from': '+14234234234', + 'text': 'test message' + + }.with_indifferent_access + end context 'when valid text message params' do it 'creates appropriate conversations, message and contacts' do - params = { - - 'id': '3232420-2323-234324', - 'owner': sms_channel.phone_number, - 'applicationId': '2342349-324234d-32432432', - 'time': '2022-02-02T23:14:05.262Z', - 'segmentCount': 1, - 'direction': 'in', - 'to': [ - sms_channel.phone_number - ], - 'from': '+14234234234', - 'text': 'test message' - - }.with_indifferent_access described_class.new(inbox: sms_channel.inbox, params: params).perform expect(sms_channel.inbox.conversations.count).not_to eq(0) expect(Contact.all.first.name).to eq('+1 423-423-4234') - expect(sms_channel.inbox.messages.first.content).to eq('test message') + expect(sms_channel.inbox.messages.first.content).to eq(params[:text]) + end + + it 'appends to last conversation when if conversation already exisits' do + contact_inbox = create(:contact_inbox, inbox: sms_channel.inbox, source_id: params[:from]) + 2.times.each { create(:conversation, inbox: sms_channel.inbox, contact_inbox: contact_inbox) } + last_conversation = create(:conversation, inbox: sms_channel.inbox, contact_inbox: contact_inbox) + described_class.new(inbox: sms_channel.inbox, params: params).perform + # no new conversation should be created + expect(sms_channel.inbox.conversations.count).to eq(3) + # message appended to the last conversation + expect(last_conversation.messages.last.content).to eq('test message') end it 'creates attachment messages and ignores .smil files' do stub_request(:get, 'http://test.com/test.png').to_return(status: 200, body: File.read('spec/assets/sample.png'), headers: {}) stub_request(:get, 'http://test.com/test2.png').to_return(status: 200, body: File.read('spec/assets/sample.png'), headers: {}) - params = { + media_params = { 'media': [ + 'http://test.com/test.smil', + 'http://test.com/test.png', + 'http://test.com/test2.png' + ] }.with_indifferent_access - 'id': '3232420-2323-234324', - 'owner': sms_channel.phone_number, - 'applicationId': '2342349-324234d-32432432', - 'time': '2022-02-02T23:14:05.262Z', - 'segmentCount': 1, - 'direction': 'in', - 'to': [ - sms_channel.phone_number - ], - 'media': [ - 'http://test.com/test.smil', - 'http://test.com/test.png', - 'http://test.com/test2.png' - ], - 'from': '+14234234234', - 'text': 'test message' - - }.with_indifferent_access - described_class.new(inbox: sms_channel.inbox, params: params).perform + described_class.new(inbox: sms_channel.inbox, params: params.merge(media_params)).perform expect(sms_channel.inbox.conversations.count).not_to eq(0) expect(Contact.all.first.name).to eq('+1 423-423-4234') expect(sms_channel.inbox.messages.first.content).to eq('test message') diff --git a/spec/services/whatsapp/incoming_message_service_spec.rb b/spec/services/whatsapp/incoming_message_service_spec.rb index efe9949f5..e59dc2a95 100644 --- a/spec/services/whatsapp/incoming_message_service_spec.rb +++ b/spec/services/whatsapp/incoming_message_service_spec.rb @@ -20,6 +20,23 @@ describe Whatsapp::IncomingMessageService do expect(Contact.all.first.name).to eq('Sojan Jose') expect(whatsapp_channel.inbox.messages.first.content).to eq('Test') end + + it 'appends to last conversation when if conversation already exisits' do + params = { + 'contacts' => [{ 'profile' => { 'name' => 'Sojan Jose' }, 'wa_id' => '2423423243' }], + 'messages' => [{ 'from' => '2423423243', 'id' => 'SDFADSf23sfasdafasdfa', 'text' => { 'body' => 'Test' }, + 'timestamp' => '1633034394', 'type' => 'text' }] + }.with_indifferent_access + + contact_inbox = create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: params[:messages].first[:from]) + 2.times.each { create(:conversation, inbox: whatsapp_channel.inbox, contact_inbox: contact_inbox) } + last_conversation = create(:conversation, inbox: whatsapp_channel.inbox, contact_inbox: contact_inbox) + described_class.new(inbox: whatsapp_channel.inbox, params: params).perform + # no new conversation should be created + expect(whatsapp_channel.inbox.conversations.count).to eq(3) + # message appended to the last conversation + expect(last_conversation.messages.last.content).to eq(params[:messages].first[:text][:body]) + end end context 'when valid interactive message params' do