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:
HariniKrishnan
2021-08-12 01:28:07 +05:30
committed by GitHub
parent 4907489ea8
commit a065165bcb
5 changed files with 47 additions and 10 deletions

View File

@@ -38,17 +38,30 @@ class ContactBuilder
end
def find_contact
contact = nil
contact = account.contacts.find_by(identifier: contact_attributes[:identifier]) if contact_attributes[:identifier].present?
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 = find_contact_by_identifier(contact_attributes[:identifier])
contact ||= find_contact_by_email(contact_attributes[:email])
contact ||= find_contact_by_phone_number(contact_attributes[:phone_number])
contact
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
ActiveRecord::Base.transaction do
contact = find_contact || create_contact
@@ -57,6 +70,7 @@ class ContactBuilder
contact_inbox
rescue StandardError => e
Rails.logger.info e
raise e
end
end
end

View File

@@ -77,6 +77,11 @@ class MailPresenter < SimpleDelegator
}
end
def from
# changing to downcase to avoid case mismatch while finding contact
@mail.from.map(&:downcase)
end
private
# forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers