Skip to content

Commit

Permalink
Merge pull request #44 from DigitalTolk/1103-fix-merged-contact
Browse files Browse the repository at this point in the history
fix: INT-1103 merged contact
  • Loading branch information
jderecho authored Jul 2, 2024
2 parents 104b17c + 51ca862 commit 79787bd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app/mailboxes/imap/imap_mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def find_conversation_by_in_reply_to

message = @inbox.messages.find_by(source_id: in_reply_to)
if message.nil?
@inbox.conversations.unclosed.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).first
@inbox.conversations.unclosed.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).where(contact: @contact).first
else
@inbox.conversations.unclosed.find_by(id: message.conversation_id)
@inbox.conversations.unclosed.find_by(id: message.conversation_id, contact: @contact)
end
end

Expand All @@ -68,7 +68,7 @@ def find_conversation_by_reference_ids

return if message.nil?

@inbox.conversations.unclosed.find_by(id: message.conversation_id)
@inbox.conversations.unclosed.find_by(id: message.conversation_id, contact: @contact)
end

def in_reply_to
Expand Down
16 changes: 16 additions & 0 deletions spec/fixtures/files/references-same.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
From: Sony Mathew <[email protected]>
Mime-Version: 1.0 (Apple Message framework v1244.3)
Content-Type: multipart/alternative; boundary="Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74"
Subject: Discussion: Let's debate these attachments
Date: Tue, 20 Apr 2020 04:20:20 -0400
In-Reply-To: <[email protected]>
References: <[email protected]> <test-reference-id>
Message-Id: <[email protected]>
X-Mailer: Apple Mail (2.1244.3)

--Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=utf-8
References Email
45 changes: 38 additions & 7 deletions spec/mailboxes/imap/imap_mailbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
end

context 'when a reply for existing email conversation' do
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent) }
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent, contact: contact) }
let(:reply_mail) do
create_inbound_email_from_mail(from: '[email protected]', to: '[email protected]', subject: 'Hello!', in_reply_to: 'test-in-reply-to')
end
Expand Down Expand Up @@ -101,6 +101,28 @@
end
end

context 'when a reply is from a different contact' do
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent, contact: contact) }
let(:references_email) { create_inbound_email_from_fixture('references.eml') }

it 'creates a new conversation' do
message = create(
:message,
content: 'Incoming Message',
message_type: 'incoming',
inbox: inbox,
account: account,
conversation: conversation
)

conversation = message.conversation
conversation.update_column(:contact_id, contact.id)
expect(conversation.messages.size).to eq(1)

expect { class_instance.process(references_email.mail, inbox.channel) }.to change { Conversation.count }.by(1)
end
end

context 'when a new conversation with nil in_reply_to' do
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent) }
let(:reply_mail) do
Expand Down Expand Up @@ -142,6 +164,7 @@
create_inbound_email_from_mail(from: '[email protected]', to: '[email protected]', subject: 'Hello!', in_reply_to: 'test-in-reply-to')
end
let(:references_email) { create_inbound_email_from_fixture('references.eml') }
let(:references_same_email) { create_inbound_email_from_fixture('references-same.eml') }

it 'creates new email conversation with incoming in-reply-to' do
class_instance.process(reply_mail.mail, channel)
Expand All @@ -160,16 +183,18 @@
conversation: conversation
)
conversation = message.conversation
conversation.update_column(:contact_id, contact.id)

expect(conversation.messages.size).to eq(1)

class_instance.process(references_email.mail, inbox.channel)
class_instance.process(references_same_email.mail, inbox.channel)

expect(conversation.messages.size).to eq(2)
expect(conversation.messages.last.content).to eq('References Email')
expect(references_email.mail.references).to include('test-reference-id')
expect(references_same_email.mail.references).to include('test-reference-id')
end


it 'append email to conversation with reference id string' do
inbox = Inbox.last
message = create(
Expand All @@ -182,15 +207,21 @@
conversation: conversation
)
conversation = message.conversation

conversation.update_column(:contact_id, contact.id)
expect(conversation.messages.size).to eq(1)

references_email.mail.references = 'test-reference-id-2'
class_instance.process(references_email.mail, inbox.channel)
references_same_email.mail.references = 'test-reference-id-2'
class_instance.process(references_same_email.mail, inbox.channel)

expect(conversation.messages.size).to eq(2)
expect(conversation.messages.last.content).to eq('References Email')
expect(references_email.mail.references).to include('test-reference-id-2')
expect(references_same_email.mail.references).to include('test-reference-id-2')
end

context 'when message is from a different contact' do
it 'creates a new conversation' do
expect { class_instance.process(references_email.mail, inbox.channel) }.to change { Conversation.count }.by(1)
end
end
end

Expand Down

0 comments on commit 79787bd

Please sign in to comment.