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:
@@ -1,18 +1,18 @@
|
||||
id,first_name,last_name,email,gender,ip_address,identifier,phone_number,company
|
||||
1,Clarice,Uzzell,cuzzell0@mozilla.org,Genderfluid,70.61.11.201,bb4e11cd-0f23-49da-a123-dcc1fec6852c,+918080808080,My Company Name
|
||||
1,Clarice,Uzzell,cuzzell0@mozilla.org,Genderfluid,70.61.11.201,bb4e11cd-0f23-49da-a123-dcc1fec6852c,918080808080,My Company Name
|
||||
2,Marieann,Creegan,mcreegan1@cornell.edu,Genderfluid,168.186.4.241,e60bab4c-9fbb-47eb-8f75-42025b789c47,+918080808081
|
||||
3,Nancey,Windibank,nwindibank2@bluehost.com,Agender,73.44.41.59,f793e813-4210-4bf3-a812-711418de25d2,+918080808082
|
||||
4,Sibel,Stennine,sstennine3@yellowbook.com,Genderqueer,115.249.27.155,d6e35a2d-d093-4437-a577-7df76316b937,+918080808083
|
||||
5,Tina,O'Lunney,tolunney4@si.edu,Bigender,219.181.212.8,3540d40a-5567-4f28-af98-5583a7ddbc56,+918080808084
|
||||
6,Quinn,Neve,qneve5@army.mil,Genderfluid,231.210.115.166,ba0e1bf0-c74b-41ce-8a2d-0b08fa0e5aa5,+918080808085
|
||||
6,Quinn,Neve,qneve5@army.mil,Genderfluid,231.210.115.166,ba0e1bf0-c74b-41ce-8a2d-0b08fa0e5aa5,918080808085
|
||||
7,Karylin,Gaunson,kgaunson6@tripod.com,Polygender,160.189.41.11,d24cac79-c81b-4b84-a33e-0441b7c6a981,+918080808086
|
||||
8,Jamison,Shenton,jshenton7@upenn.edu,Agender,53.94.18.201,29a7a8c0-c7f7-4af9-852f-761b1a784a7a,+918080808087
|
||||
9,Gavan,Threlfall,gthrelfall8@spotify.com,Genderfluid,18.87.247.249,847d4943-ddb5-47cc-8008-ed5092c675c5,+918080808088
|
||||
10,Katina,Hemmingway,khemmingway9@ameblo.jp,Non-binary,25.191.96.124,8f0b5efd-b6a8-4f1e-a1e3-b0ea8c9e3048,+918080808089
|
||||
11,Jillian,Deinhard,jdeinharda@canalblog.com,Female,11.211.174.93,bd952787-1b05-411f-9975-b916ec0950cc,+918080808090
|
||||
12,Blake,Finden,bfindenb@wsj.com,Female,47.26.205.153,12c95613-e49d-4fa2-86fb-deabb6ebe600,+918080808091
|
||||
13,Liane,Maxworthy,lmaxworthyc@un.org,Non-binary,157.196.34.166,36b68e4c-40d6-4e09-bf59-7db3b27b18f0,+918080808092
|
||||
14,Martynne,Ledley,mledleyd@sourceforge.net,Polygender,109.231.152.148,1856bceb-cb36-415c-8ffc-0527f3f750d8,+918080808093
|
||||
13,Liane,Maxworthy,lmaxworthyc@un.org,Non-binary,157.196.34.166,36b68e4c-40d6-4e09-bf59-7db3b27b18f0,918080808092
|
||||
14,Martynne,Ledley,mledleyd@sourceforge.net,Polygender,109.231.152.148,1856bceb-cb36-415c-8ffc-0527f3f750d8,918080808093
|
||||
15,Katharina,Ruffli,krufflie@huffingtonpost.com,Genderfluid,20.43.146.179,604de5c9-b154-4279-8978-41fb71f0f773,+918080808094
|
||||
16,Tucker,Simmance,tsimmancef@bbc.co.uk,Bigender,179.76.226.171,0a8fc3a7-4986-4a51-a503-6c7f974c90ad,+918080808095
|
||||
17,Wenona,Martinson,wmartinsong@census.gov,Genderqueer,92.243.194.160,0e5ea6e3-6824-4e78-a6f5-672847eafa17,+918080808096
|
||||
|
||||
|
3
spec/fixtures/data_import/invalid_bytes.csv
vendored
Normal file
3
spec/fixtures/data_import/invalid_bytes.csv
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
name,email
|
||||
Jöhn Döe,john.doe@example.com
|
||||
Jane Smith,jane.smith@example.com
|
||||
|
@@ -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
|
||||
|
||||
|
||||
@@ -57,6 +57,24 @@ RSpec.describe AdministratorNotifications::ChannelNotificationsMailer do
|
||||
end
|
||||
end
|
||||
|
||||
describe 'contact_import_complete' do
|
||||
let!(:data_import) { build(:data_import, total_records: 10, processed_records: 10) }
|
||||
let(:mail) { described_class.with(account: account).contact_import_complete(data_import).deliver_now }
|
||||
|
||||
it 'renders the subject' do
|
||||
expect(mail.subject).to eq('Contact Import Completed')
|
||||
end
|
||||
|
||||
it 'renders the processed records' do
|
||||
expect(mail.body.encoded).to match('Number of records imported: 10')
|
||||
expect(mail.body.encoded).to match('Number of records failed: 0')
|
||||
end
|
||||
|
||||
it 'renders the receiver email' do
|
||||
expect(mail.to).to eq([administrator.email])
|
||||
end
|
||||
end
|
||||
|
||||
describe 'contact_export_complete' do
|
||||
let!(:file_url) { Rails.application.routes.url_helpers.rails_blob_url(account.contacts_export) }
|
||||
let(:mail) { described_class.with(account: account).contact_export_complete(file_url).deliver_now }
|
||||
|
||||
Reference in New Issue
Block a user