fix: undefined method contact in support mailbox (#2678)
if chatwoot receives an email of already existing contact with a different case say "Care@example.com", before this fix, it will throw an error. Now it will retrieve existing contact Fixes: #2553 Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
parent
4907489ea8
commit
a065165bcb
5 changed files with 47 additions and 10 deletions
|
@ -38,17 +38,30 @@ class ContactBuilder
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_contact
|
def find_contact
|
||||||
contact = nil
|
contact = find_contact_by_identifier(contact_attributes[:identifier])
|
||||||
|
contact ||= find_contact_by_email(contact_attributes[:email])
|
||||||
contact = account.contacts.find_by(identifier: contact_attributes[:identifier]) if contact_attributes[:identifier].present?
|
contact ||= find_contact_by_phone_number(contact_attributes[:phone_number])
|
||||||
|
|
||||||
contact ||= account.contacts.find_by(email: contact_attributes[:email]) if contact_attributes[:email].present?
|
|
||||||
|
|
||||||
contact ||= account.contacts.find_by(phone_number: contact_attributes[:phone_number]) if contact_attributes[:phone_number].present?
|
|
||||||
|
|
||||||
contact
|
contact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_contact_by_identifier(identifier)
|
||||||
|
return if identifier.blank?
|
||||||
|
|
||||||
|
account.contacts.find_by(identifier: identifier)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_contact_by_email(email)
|
||||||
|
return if email.blank?
|
||||||
|
|
||||||
|
account.contacts.find_by(email: email.downcase)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_contact_by_phone_number(phone_number)
|
||||||
|
return if phone_number.blank?
|
||||||
|
|
||||||
|
account.contacts.find_by(phone_number: phone_number)
|
||||||
|
end
|
||||||
|
|
||||||
def build_contact_inbox
|
def build_contact_inbox
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
contact = find_contact || create_contact
|
contact = find_contact || create_contact
|
||||||
|
@ -57,6 +70,7 @@ class ContactBuilder
|
||||||
contact_inbox
|
contact_inbox
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
Rails.logger.info e
|
Rails.logger.info e
|
||||||
|
raise e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,6 +77,11 @@ class MailPresenter < SimpleDelegator
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def from
|
||||||
|
# changing to downcase to avoid case mismatch while finding contact
|
||||||
|
@mail.from.map(&:downcase)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers
|
# forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers
|
||||||
|
|
|
@ -66,6 +66,20 @@ describe ::ContactBuilder do
|
||||||
expect(contact_inbox.contact.id).to be(contact.id)
|
expect(contact_inbox.contact.id).to be(contact.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'doesnot create contact when an uppercase email is passed for an already existing contact email' do
|
||||||
|
contact_inbox = described_class.new(
|
||||||
|
source_id: '123456',
|
||||||
|
inbox: inbox,
|
||||||
|
contact_attributes: {
|
||||||
|
name: 'Contact',
|
||||||
|
phone_number: '+1234567890',
|
||||||
|
email: contact.email.upcase
|
||||||
|
}
|
||||||
|
).perform
|
||||||
|
|
||||||
|
expect(contact_inbox.contact.id).to be(contact.id)
|
||||||
|
end
|
||||||
|
|
||||||
it 'doesnot create contact if it already exist with phone number' do
|
it 'doesnot create contact if it already exist with phone number' do
|
||||||
contact_inbox = described_class.new(
|
contact_inbox = described_class.new(
|
||||||
source_id: '123456',
|
source_id: '123456',
|
||||||
|
|
4
spec/fixtures/files/welcome.eml
vendored
4
spec/fixtures/files/welcome.eml
vendored
|
@ -1,4 +1,4 @@
|
||||||
From: Sony Mathew <sony@chatwoot.com>
|
From: Sony Mathew <Sony@chatwoot.com>
|
||||||
Mime-Version: 1.0 (Apple Message framework v1244.3)
|
Mime-Version: 1.0 (Apple Message framework v1244.3)
|
||||||
Content-Type: multipart/alternative; boundary="Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74"
|
Content-Type: multipart/alternative; boundary="Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74"
|
||||||
Subject: Discussion: Let's debate these attachments
|
Subject: Discussion: Let's debate these attachments
|
||||||
|
@ -628,4 +628,4 @@ r/Fxp3Y0d2/4tvsR95f/AAH7Gvwmn9rUHsOgUCg//9k=
|
||||||
|
|
||||||
--Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1--
|
--Apple-Mail=_83444AF4-343C-4F75-AF8F-14E1E7434FC1--
|
||||||
|
|
||||||
--Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74--
|
--Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74--
|
||||||
|
|
|
@ -52,5 +52,9 @@ RSpec.describe MailPresenter do
|
||||||
expect(data[:multipart]).to eq(true)
|
expect(data[:multipart]).to eq(true)
|
||||||
expect(data[:subject]).to eq(decorated_mail.subject)
|
expect(data[:subject]).to eq(decorated_mail.subject)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'give email from in downcased format' do
|
||||||
|
expect(decorated_mail.from.first.eql?(mail.from.first.downcase)).to eq true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue