chore: Contact import improvements (#7787)

- Ensure existing contact information is updated on data import
- Refactor the existing job to make it more readable
- Fixes issues with import files in the wrong encoding
fixes: #7307
This commit is contained in:
Sojan Jose
2023-08-23 23:24:47 -07:00
committed by GitHub
parent 44f6a9ec3c
commit acb7debd3f
11 changed files with 174 additions and 100 deletions

View File

@@ -47,15 +47,29 @@ RSpec.describe DataImportJob do
expect(invalid_data_import.reload.total_records).to eq(csv_length)
expect(invalid_data_import.reload.processed_records).to eq(csv_length)
end
it 'will not throw error for non utf-8 characters' do
invalid_data_import = create(:data_import,
import_file: Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/data_import/invalid_bytes.csv'),
'text/csv'))
csv_data = CSV.parse(invalid_data_import.import_file.download, headers: true)
csv_length = csv_data.length
described_class.perform_now(invalid_data_import)
expect(invalid_data_import.account.contacts.count).to eq(csv_length)
expect(invalid_data_import.account.contacts.first.name).to eq(csv_data[0]['name'].encode('UTF-8', 'binary', invalid: :replace,
undef: :replace, replace: ''))
end
end
context 'when the data contains existing records' do
let(:existing_data) do
[
%w[id first_name last_name email phone_number],
['1', 'Clarice', 'Uzzell', 'cuzzell0@mozilla.org', '+918080808080'],
['2', 'Marieann', 'Creegan', 'mcreegan1@cornell.edu', '+918080808081'],
['3', 'Nancey', 'Windibank', 'nwindibank2@bluehost.com', '+918080808082']
%w[id name email phone_number company],
['1', 'Clarice Uzzell', 'cuzzell0@mozilla.org', '918080808080', 'Acmecorp'],
['2', 'Marieann Creegan', 'mcreegan1@cornell.edu', '+918080808081', 'Acmecorp'],
['3', 'Nancey Windibank', 'nwindibank2@bluehost.com', '+918080808082', 'Acmecorp']
]
end
let(:existing_data_import) { create(:data_import, import_file: generate_csv_file(existing_data)) }
@@ -70,8 +84,11 @@ RSpec.describe DataImportJob do
described_class.perform_now(existing_data_import)
expect(existing_data_import.account.contacts.count).to eq(csv_length)
expect(Contact.find_by(email: csv_data[0]['email']).phone_number).to eq(csv_data[0]['phone_number'])
expect(Contact.where(email: csv_data[0]['email']).count).to eq(1)
contact = Contact.find_by(email: csv_data[0]['email'])
expect(contact).to be_present
expect(contact.phone_number).to eq("+#{csv_data[0]['phone_number']}")
expect(contact.name).to eq((csv_data[0]['name']).to_s)
expect(contact.additional_attributes['company']).to eq((csv_data[0]['company']).to_s)
end
end
@@ -84,8 +101,11 @@ RSpec.describe DataImportJob do
described_class.perform_now(existing_data_import)
expect(existing_data_import.account.contacts.count).to eq(csv_length)
expect(Contact.find_by(phone_number: csv_data[1]['phone_number']).email).to eq(csv_data[1]['email'])
expect(Contact.where(phone_number: csv_data[1]['phone_number']).count).to eq(1)
contact = Contact.find_by(phone_number: "+#{csv_data[0]['phone_number']}")
expect(contact).to be_present
expect(contact.email).to eq(csv_data[0]['email'])
expect(contact.name).to eq((csv_data[0]['name']).to_s)
expect(contact.additional_attributes['company']).to eq((csv_data[0]['company']).to_s)
end
end