Compare commits

...
8 Commits
3 changed files with 83 additions and 28 deletions
+13 -2
View File
@@ -61,8 +61,19 @@ class DataImport::ContactManager
def update_contact_attributes(params, contact)
contact.name = params[:name] if params[:name].present?
contact.additional_attributes ||= {}
contact.additional_attributes[:company_name] = params[:company_name] if params[:company_name].present?
contact.additional_attributes[:city] = params[:city] if params[:city].present?
assign_additional_attributes(params, contact)
contact.assign_attributes(custom_attributes: contact.custom_attributes.merge(params.except(:identifier, :email, :name, :phone_number)))
end
def assign_additional_attributes(params, contact)
contact.additional_attributes[:company_name] = params[:company_name] if params[:company_name].present?
contact.additional_attributes[:city] = params[:city] if params[:city].present?
assign_country_attributes(params, contact)
end
def assign_country_attributes(params, contact)
contact.additional_attributes['country'] = params[:country] if params[:country].present?
contact.additional_attributes['country_code'] = params[:country_code] if params[:country_code].present?
contact.country_code = params[:country] if params[:country].present?
end
end
+26 -26
View File
@@ -1,26 +1,26 @@
id,name,email,identifier,phone_number,labels,ip_address,company_name,custom_attribute_1,custom_attribute_2
1,Clarice Uzzell,cuzzell0@mozilla.org,bb4e11cd-0f23-49da-a123-dcc1fec6852c,+498963648018,,70.61.11.201,Acme Inc,Random-value-1,Random-value-1
2,Marieann Creegan,mcreegan1@cornell.edu,e60bab4c-9fbb-47eb-8f75-42025b789c47,+15417543010,,168.186.4.241,Acme Inc,Random-value0,Random-value0
3,Nancey Windibank,nwindibank2@bluehost.com,f793e813-4210-4bf3-a812-711418de25d2,+15417543011,,73.44.41.59,Acme Inc,Random-value1,Random-value1
4,Sibel Stennine,sstennine3@yellowbook.com,d6e35a2d-d093-4437-a577-7df76316b937,+15417543011,,115.249.27.155,Acme Inc,Random-value2,Random-value2
5,Tina O'Lunney,tolunney4@si.edu,3540d40a-5567-4f28-af98-5583a7ddbc56,+15417543011,,219.181.212.8,Acme Inc,Random-value3,Random-value3
6,Quinn Neve,qneve5@army.mil,ba0e1bf0-c74b-41ce-8a2d-0b08fa0e5aa5,+15417543011,,231.210.115.166,Acme Inc,Random-value4,Random-value4
7,Karylin Gaunson,kgaunson6@tripod.com,d24cac79-c81b-4b84-a33e-0441b7c6a981,+15417543011,,160.189.41.11,Acme Inc,Random-value5,Random-value5
8,Jamison Shenton,jshenton7@upenn.edu,29a7a8c0-c7f7-4af9-852f-761b1a784a7a,+15417543011,,53.94.18.201,Acme Inc,Random-value6,Random-value6
9,Gavan Threlfall,gthrelfall8@spotify.com,847d4943-ddb5-47cc-8008-ed5092c675c5,+15417543011,,18.87.247.249,Acme Inc,Random-value7,Random-value7
10,Katina Hemmingway,khemmingway9@ameblo.jp,8f0b5efd-b6a8-4f1e-a1e3-b0ea8c9e3048,+15417543011,,25.191.96.124,Acme Inc,Random-value8,Random-value8
11,Jillian Deinhard,jdeinharda@canalblog.com,bd952787-1b05-411f-9975-b916ec0950cc,+15417543011,,11.211.174.93,Acme Inc,Random-value9,Random-value9
12,Blake Finden,bfindenb@wsj.com,12c95613-e49d-4fa2-86fb-deabb6ebe600,+15417543011,,47.26.205.153,Acme Inc,Random-value10,Random-value10
13,Liane Maxworthy,lmaxworthyc@un.org,36b68e4c-40d6-4e09-bf59-7db3b27b18f0,+15417543011,,157.196.34.166,Acme Inc,Random-value11,Random-value11
14,Martynne Ledley,mledleyd@sourceforge.net,1856bceb-cb36-415c-8ffc-0527f3f750d8,+15417543011,,109.231.152.148,Acme Inc,Random-value12,Random-value12
15,Katharina Ruffli,krufflie@huffingtonpost.com,604de5c9-b154-4279-8978-41fb71f0f773,+15417543011,,20.43.146.179,Acme Inc,Random-value13,Random-value13
16,Tucker Simmance,tsimmancef@bbc.co.uk,0a8fc3a7-4986-4a51-a503-6c7f974c90ad,+15417543011,,179.76.226.171,Acme Inc,Random-value14,Random-value14
17,Wenona Martinson,wmartinsong@census.gov,0e5ea6e3-6824-4e78-a6f5-672847eafa17,+15417543011,,92.243.194.160,Acme Inc,Random-value15,Random-value15
18,Gretna Vedyasov,gvedyasovh@lycos.com,6becf55b-a7b5-48f6-8788-b89cae85b066,+15417543011,,25.22.86.101,Acme Inc,Random-value16,Random-value16
19,Lurline Abdon,labdoni@archive.org,afa9429f-9034-4b06-9efa-980e01906ebf,+15417543011,,150.249.116.118,Acme Inc,Random-value17,Random-value17
20,Fiann Norcliff,fnorcliffj@istockphoto.com,59f72dec-14ba-4d6e-b17c-0d962e69ffac,+15417543011,,237.167.197.197,Acme Inc,Random-value18,Random-value18
21,Zed Linn,zlinnk@phoca.cz,95f7bc56-be92-4c9c-ad58-eff3e63c7bea,+15417543011,,88.102.64.113,Acme Inc,Random-value19,Random-value19
22,Averyl Simyson,asimysonl@livejournal.com,bde1fe59-c9bd-440c-bb39-79fe61dac1d1,+15417543011,,141.248.89.29,Acme Inc,Random-value20,Random-value20
23,Camella Blackadder,cblackadderm@nifty.com,0c981752-5857-487c-b9b5-5d0253df740a,+15417543011,,118.123.138.115,Acme Inc,Random-value21,Random-value21
24,Aurie Spatig,aspatign@printfriendly.com,4cf22bfb-2c3f-41d1-9993-6e3758e457ba,+15417543011,,157.45.102.235,Acme Inc,Random-value22,Random-value22
25,Adrienne Bellard,abellardo@cnn.com,f10f9b8d-38ac-4e17-8a7d-d2e6a055f944,+15417543011,,170.73.198.47,Acme Inc,Random-value23,Random-value23
id,name,email,identifier,phone_number,labels,ip_address,company_name,country,country_code,custom_attribute_1,custom_attribute_2
1,Clarice Uzzell,cuzzell0@mozilla.org,bb4e11cd-0f23-49da-a123-dcc1fec6852c,+498963648018,,70.61.11.201,Acme Inc,Germany,DE,Random-value-1,Random-value-1
2,Marieann Creegan,mcreegan1@cornell.edu,e60bab4c-9fbb-47eb-8f75-42025b789c47,+15417543010,,168.186.4.241,Acme Inc,United States,US,Random-value0,Random-value0
3,Nancey Windibank,nwindibank2@bluehost.com,f793e813-4210-4bf3-a812-711418de25d2,+15417543011,,73.44.41.59,Acme Inc,United States,US,Random-value1,Random-value1
4,Sibel Stennine,sstennine3@yellowbook.com,d6e35a2d-d093-4437-a577-7df76316b937,+15417543011,,115.249.27.155,Acme Inc,India,IN,Random-value2,Random-value2
5,Tina O'Lunney,tolunney4@si.edu,3540d40a-5567-4f28-af98-5583a7ddbc56,+15417543011,,219.181.212.8,Acme Inc,Japan,JP,Random-value3,Random-value3
6,Quinn Neve,qneve5@army.mil,ba0e1bf0-c74b-41ce-8a2d-0b08fa0e5aa5,+15417543011,,231.210.115.166,Acme Inc,United Kingdom,GB,Random-value4,Random-value4
7,Karylin Gaunson,kgaunson6@tripod.com,d24cac79-c81b-4b84-a33e-0441b7c6a981,+15417543011,,160.189.41.11,Acme Inc,Canada,CA,Random-value5,Random-value5
8,Jamison Shenton,jshenton7@upenn.edu,29a7a8c0-c7f7-4af9-852f-761b1a784a7a,+15417543011,,53.94.18.201,Acme Inc,United States,US,Random-value6,Random-value6
9,Gavan Threlfall,gthrelfall8@spotify.com,847d4943-ddb5-47cc-8008-ed5092c675c5,+15417543011,,18.87.247.249,Acme Inc,Sweden,SE,Random-value7,Random-value7
10,Katina Hemmingway,khemmingway9@ameblo.jp,8f0b5efd-b6a8-4f1e-a1e3-b0ea8c9e3048,+15417543011,,25.191.96.124,Acme Inc,Japan,JP,Random-value8,Random-value8
11,Jillian Deinhard,jdeinharda@canalblog.com,bd952787-1b05-411f-9975-b916ec0950cc,+15417543011,,11.211.174.93,Acme Inc,France,FR,Random-value9,Random-value9
12,Blake Finden,bfindenb@wsj.com,12c95613-e49d-4fa2-86fb-deabb6ebe600,+15417543011,,47.26.205.153,Acme Inc,United States,US,Random-value10,Random-value10
13,Liane Maxworthy,lmaxworthyc@un.org,36b68e4c-40d6-4e09-bf59-7db3b27b18f0,+15417543011,,157.196.34.166,Acme Inc,Switzerland,CH,Random-value11,Random-value11
14,Martynne Ledley,mledleyd@sourceforge.net,1856bceb-cb36-415c-8ffc-0527f3f750d8,+15417543011,,109.231.152.148,Acme Inc,United Kingdom,GB,Random-value12,Random-value12
15,Katharina Ruffli,krufflie@huffingtonpost.com,604de5c9-b154-4279-8978-41fb71f0f773,+15417543011,,20.43.146.179,Acme Inc,Germany,DE,Random-value13,Random-value13
16,Tucker Simmance,tsimmancef@bbc.co.uk,0a8fc3a7-4986-4a51-a503-6c7f974c90ad,+15417543011,,179.76.226.171,Acme Inc,United Kingdom,GB,Random-value14,Random-value14
17,Wenona Martinson,wmartinsong@census.gov,0e5ea6e3-6824-4e78-a6f5-672847eafa17,+15417543011,,92.243.194.160,Acme Inc,United States,US,Random-value15,Random-value15
18,Gretna Vedyasov,gvedyasovh@lycos.com,6becf55b-a7b5-48f6-8788-b89cae85b066,+15417543011,,25.22.86.101,Acme Inc,Russia,RU,Random-value16,Random-value16
19,Lurline Abdon,labdoni@archive.org,afa9429f-9034-4b06-9efa-980e01906ebf,+15417543011,,150.249.116.118,Acme Inc,United States,US,Random-value17,Random-value17
20,Fiann Norcliff,fnorcliffj@istockphoto.com,59f72dec-14ba-4d6e-b17c-0d962e69ffac,+15417543011,,237.167.197.197,Acme Inc,Ireland,IE,Random-value18,Random-value18
21,Zed Linn,zlinnk@phoca.cz,95f7bc56-be92-4c9c-ad58-eff3e63c7bea,+15417543011,,88.102.64.113,Acme Inc,Czechia,CZ,Random-value19,Random-value19
22,Averyl Simyson,asimysonl@livejournal.com,bde1fe59-c9bd-440c-bb39-79fe61dac1d1,+15417543011,,141.248.89.29,Acme Inc,United States,US,Random-value20,Random-value20
23,Camella Blackadder,cblackadderm@nifty.com,0c981752-5857-487c-b9b5-5d0253df740a,+15417543011,,118.123.138.115,Acme Inc,Japan,JP,Random-value21,Random-value21
24,Aurie Spatig,aspatign@printfriendly.com,4cf22bfb-2c3f-41d1-9993-6e3758e457ba,+15417543011,,157.45.102.235,Acme Inc,India,IN,Random-value22,Random-value22
25,Adrienne Bellard,abellardo@cnn.com,f10f9b8d-38ac-4e17-8a7d-d2e6a055f944,+15417543011,,170.73.198.47,Acme Inc,United States,US,Random-value23,Random-value23
1 id name email identifier phone_number labels ip_address company_name country country_code custom_attribute_1 custom_attribute_2
2 1 Clarice Uzzell cuzzell0@mozilla.org bb4e11cd-0f23-49da-a123-dcc1fec6852c +498963648018 70.61.11.201 Acme Inc Germany DE Random-value-1 Random-value-1
3 2 Marieann Creegan mcreegan1@cornell.edu e60bab4c-9fbb-47eb-8f75-42025b789c47 +15417543010 168.186.4.241 Acme Inc United States US Random-value0 Random-value0
4 3 Nancey Windibank nwindibank2@bluehost.com f793e813-4210-4bf3-a812-711418de25d2 +15417543011 73.44.41.59 Acme Inc United States US Random-value1 Random-value1
5 4 Sibel Stennine sstennine3@yellowbook.com d6e35a2d-d093-4437-a577-7df76316b937 +15417543011 115.249.27.155 Acme Inc India IN Random-value2 Random-value2
6 5 Tina O'Lunney tolunney4@si.edu 3540d40a-5567-4f28-af98-5583a7ddbc56 +15417543011 219.181.212.8 Acme Inc Japan JP Random-value3 Random-value3
7 6 Quinn Neve qneve5@army.mil ba0e1bf0-c74b-41ce-8a2d-0b08fa0e5aa5 +15417543011 231.210.115.166 Acme Inc United Kingdom GB Random-value4 Random-value4
8 7 Karylin Gaunson kgaunson6@tripod.com d24cac79-c81b-4b84-a33e-0441b7c6a981 +15417543011 160.189.41.11 Acme Inc Canada CA Random-value5 Random-value5
9 8 Jamison Shenton jshenton7@upenn.edu 29a7a8c0-c7f7-4af9-852f-761b1a784a7a +15417543011 53.94.18.201 Acme Inc United States US Random-value6 Random-value6
10 9 Gavan Threlfall gthrelfall8@spotify.com 847d4943-ddb5-47cc-8008-ed5092c675c5 +15417543011 18.87.247.249 Acme Inc Sweden SE Random-value7 Random-value7
11 10 Katina Hemmingway khemmingway9@ameblo.jp 8f0b5efd-b6a8-4f1e-a1e3-b0ea8c9e3048 +15417543011 25.191.96.124 Acme Inc Japan JP Random-value8 Random-value8
12 11 Jillian Deinhard jdeinharda@canalblog.com bd952787-1b05-411f-9975-b916ec0950cc +15417543011 11.211.174.93 Acme Inc France FR Random-value9 Random-value9
13 12 Blake Finden bfindenb@wsj.com 12c95613-e49d-4fa2-86fb-deabb6ebe600 +15417543011 47.26.205.153 Acme Inc United States US Random-value10 Random-value10
14 13 Liane Maxworthy lmaxworthyc@un.org 36b68e4c-40d6-4e09-bf59-7db3b27b18f0 +15417543011 157.196.34.166 Acme Inc Switzerland CH Random-value11 Random-value11
15 14 Martynne Ledley mledleyd@sourceforge.net 1856bceb-cb36-415c-8ffc-0527f3f750d8 +15417543011 109.231.152.148 Acme Inc United Kingdom GB Random-value12 Random-value12
16 15 Katharina Ruffli krufflie@huffingtonpost.com 604de5c9-b154-4279-8978-41fb71f0f773 +15417543011 20.43.146.179 Acme Inc Germany DE Random-value13 Random-value13
17 16 Tucker Simmance tsimmancef@bbc.co.uk 0a8fc3a7-4986-4a51-a503-6c7f974c90ad +15417543011 179.76.226.171 Acme Inc United Kingdom GB Random-value14 Random-value14
18 17 Wenona Martinson wmartinsong@census.gov 0e5ea6e3-6824-4e78-a6f5-672847eafa17 +15417543011 92.243.194.160 Acme Inc United States US Random-value15 Random-value15
19 18 Gretna Vedyasov gvedyasovh@lycos.com 6becf55b-a7b5-48f6-8788-b89cae85b066 +15417543011 25.22.86.101 Acme Inc Russia RU Random-value16 Random-value16
20 19 Lurline Abdon labdoni@archive.org afa9429f-9034-4b06-9efa-980e01906ebf +15417543011 150.249.116.118 Acme Inc United States US Random-value17 Random-value17
21 20 Fiann Norcliff fnorcliffj@istockphoto.com 59f72dec-14ba-4d6e-b17c-0d962e69ffac +15417543011 237.167.197.197 Acme Inc Ireland IE Random-value18 Random-value18
22 21 Zed Linn zlinnk@phoca.cz 95f7bc56-be92-4c9c-ad58-eff3e63c7bea +15417543011 88.102.64.113 Acme Inc Czechia CZ Random-value19 Random-value19
23 22 Averyl Simyson asimysonl@livejournal.com bde1fe59-c9bd-440c-bb39-79fe61dac1d1 +15417543011 141.248.89.29 Acme Inc United States US Random-value20 Random-value20
24 23 Camella Blackadder cblackadderm@nifty.com 0c981752-5857-487c-b9b5-5d0253df740a +15417543011 118.123.138.115 Acme Inc Japan JP Random-value21 Random-value21
25 24 Aurie Spatig aspatign@printfriendly.com 4cf22bfb-2c3f-41d1-9993-6e3758e457ba +15417543011 157.45.102.235 Acme Inc India IN Random-value22 Random-value22
26 25 Adrienne Bellard abellardo@cnn.com f10f9b8d-38ac-4e17-8a7d-d2e6a055f944 +15417543011 170.73.198.47 Acme Inc United States US Random-value23 Random-value23
+44
View File
@@ -188,6 +188,50 @@ RSpec.describe DataImportJob do
end
end
context 'when the data contains country and country_code columns' do
let(:data_with_country) do
[
%w[id name email phone_number country country_code],
['1', 'John Doe', 'john-country@example.com', '+918080808081', 'United States', 'US'],
['2', 'Jane Smith', 'jane-country@example.com', '+918080808082', 'India', 'IN'],
['3', 'Bob Wilson', 'bob-country@example.com', '+918080808083', '', '']
]
end
let(:country_data_import) { create(:data_import, import_file: generate_csv_file(data_with_country)) }
it 'maps the country and country_code columns into additional_attributes' do
described_class.perform_now(country_data_import)
john = Contact.from_email('john-country@example.com')
expect(john.additional_attributes['country']).to eq('United States')
expect(john.additional_attributes['country_code']).to eq('US')
jane = Contact.from_email('jane-country@example.com')
expect(jane.additional_attributes['country']).to eq('India')
expect(jane.additional_attributes['country_code']).to eq('IN')
bob = Contact.from_email('bob-country@example.com')
expect(bob.additional_attributes['country']).to be_nil
expect(bob.additional_attributes['country_code']).to be_nil
end
it 'populates the country_code column for new contacts via the bulk import path' do
described_class.perform_now(country_data_import)
expect(Contact.from_email('john-country@example.com').country_code).to eq('United States')
expect(Contact.from_email('jane-country@example.com').country_code).to eq('India')
expect(Contact.from_email('bob-country@example.com').country_code).to eq('')
end
it 'populates the country_code column when updating an existing contact' do
existing = create(:contact, account: country_data_import.account, email: 'john-country@example.com')
described_class.perform_now(country_data_import)
expect(existing.reload.country_code).to eq('United States')
end
end
context 'when the data contains labels column' do
let(:data_with_labels) do
[