Skip to content

Commit

Permalink
chore: Persist emojis in Contact Import (#7803)
Browse files Browse the repository at this point in the history
The previous fix would remove emojis in contact data. This change ensures they are persisited

ref: #7787
  • Loading branch information
sojan-official committed Aug 24, 2023
1 parent 3dd3b7b commit 64ae9f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/jobs/data_import_job.rb
Expand Up @@ -27,7 +27,11 @@ def parse_csv_and_build_contacts
rejected_contacts = []
# Ensuring that importing non utf-8 characters will not throw error
data = @data_import.import_file.download
clean_data = data.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
utf8_data = data.force_encoding('UTF-8')

# Ensure that the data is valid UTF-8, preserving valid characters
clean_data = utf8_data.valid_encoding? ? utf8_data : utf8_data.encode('UTF-16le', invalid: :replace, replace: '').encode('UTF-8')

csv = CSV.parse(clean_data, headers: true)

csv.each do |row|
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/data_import/with_emoji.csv
@@ -0,0 +1,2 @@
phone_number,name
+123456,T 馃彔 馃敟 Test
13 changes: 13 additions & 0 deletions spec/jobs/data_import_job_spec.rb
Expand Up @@ -48,6 +48,19 @@
expect(invalid_data_import.reload.processed_records).to eq(csv_length)
end

it 'will preserve emojis' do
data_import = create(:data_import,
import_file: Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/data_import/with_emoji.csv'),
'text/csv'))
csv_data = CSV.parse(data_import.import_file.download, headers: true)
csv_length = csv_data.length

described_class.perform_now(data_import)
expect(data_import.account.contacts.count).to eq(csv_length)

expect(data_import.account.contacts.first.name).to eq('T 馃彔 馃敟 Test')
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'),
Expand Down

0 comments on commit 64ae9f6

Please sign in to comment.