diff --git a/app/builders/contact_inbox_with_contact_builder.rb b/app/builders/contact_inbox_with_contact_builder.rb index 2c0e6087e..2ba9179ed 100644 --- a/app/builders/contact_inbox_with_contact_builder.rb +++ b/app/builders/contact_inbox_with_contact_builder.rb @@ -50,7 +50,7 @@ class ContactInboxWithContactBuilder def create_contact account.contacts.create!( - name: contact_attributes[:name] || ::Haikunator.haikunate(1000), + name: contact_name, phone_number: contact_attributes[:phone_number], email: contact_attributes[:email], identifier: contact_attributes[:identifier], @@ -59,6 +59,11 @@ class ContactInboxWithContactBuilder ) end + def contact_name + name = contact_attributes[:name] || ::Haikunator.haikunate(1000) + name.truncate(ApplicationRecord::MAX_STRING_COLUMN_LENGTH, omission: '') + end + def find_contact contact = find_contact_by_identifier(contact_attributes[:identifier]) contact ||= find_contact_by_email(contact_attributes[:email]) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index c6877c883..0cb3f7de2 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,4 +1,7 @@ class ApplicationRecord < ActiveRecord::Base + MAX_STRING_COLUMN_LENGTH = 255 + MAX_TEXT_COLUMN_LENGTH = 20_000 + include Events::Types self.abstract_class = true @@ -37,7 +40,7 @@ class ApplicationRecord < ActiveRecord::Base end def validate_content_length(column) - max_length = column.type == :text ? 20_000 : 255 + max_length = column.type == :text ? MAX_TEXT_COLUMN_LENGTH : MAX_STRING_COLUMN_LENGTH return if self[column.name].nil? || self[column.name].length <= max_length errors.add(column.name.to_sym, "is too long (maximum is #{max_length} characters)") diff --git a/spec/builders/contact_inbox_with_contact_builder_spec.rb b/spec/builders/contact_inbox_with_contact_builder_spec.rb index 60f0834af..58502539f 100644 --- a/spec/builders/contact_inbox_with_contact_builder_spec.rb +++ b/spec/builders/contact_inbox_with_contact_builder_spec.rb @@ -39,6 +39,21 @@ describe ContactInboxWithContactBuilder do expect(contact_inbox.inbox_id).to eq(inbox.id) end + it 'truncates long contact names before creating the contact' do + long_name = 'a' * 300 + + contact_inbox = described_class.new( + source_id: '123456', + inbox: inbox, + contact_attributes: { + name: long_name, + email: 'testemail@example.com' + } + ).perform + + expect(contact_inbox.contact.name).to eq(long_name.first(ApplicationRecord::MAX_STRING_COLUMN_LENGTH)) + end + it 'doesnot create contact if it already exist with identifier' do contact_inbox = described_class.new( source_id: '123456',