fix: Handle Contact import MalformedCSVError (#8706)
This commit is contained in:
@@ -8,8 +8,12 @@ class DataImportJob < ApplicationJob
|
||||
def perform(data_import)
|
||||
@data_import = data_import
|
||||
@contact_manager = DataImport::ContactManager.new(@data_import.account)
|
||||
process_import_file
|
||||
send_import_notification_to_admin
|
||||
begin
|
||||
process_import_file
|
||||
send_import_notification_to_admin
|
||||
rescue CSV::MalformedCSVError => e
|
||||
handle_csv_error(e)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
@@ -83,7 +87,16 @@ class DataImportJob < ApplicationJob
|
||||
end
|
||||
end
|
||||
|
||||
def handle_csv_error(error) # rubocop:disable Lint/UnusedMethodArgument
|
||||
@data_import.update!(status: :failed)
|
||||
send_import_failed_notification_to_admin
|
||||
end
|
||||
|
||||
def send_import_notification_to_admin
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: @data_import.account).contact_import_complete(@data_import).deliver_later
|
||||
end
|
||||
|
||||
def send_import_failed_notification_to_admin
|
||||
AdministratorNotifications::ChannelNotificationsMailer.with(account: @data_import.account).contact_import_failed.deliver_later
|
||||
end
|
||||
end
|
||||
|
||||
@@ -51,6 +51,15 @@ class AdministratorNotifications::ChannelNotificationsMailer < ApplicationMailer
|
||||
send_mail_with_liquid(to: admin_emails, subject: subject) and return
|
||||
end
|
||||
|
||||
def contact_import_failed
|
||||
return unless smtp_config_set_or_development?
|
||||
|
||||
subject = 'Contact Import Failed'
|
||||
|
||||
@meta = {}
|
||||
send_mail_with_liquid(to: admin_emails, subject: subject) and return
|
||||
end
|
||||
|
||||
def contact_export_complete(file_url)
|
||||
return unless smtp_config_set_or_development?
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<p>Hello,</p>
|
||||
|
||||
<p>Your contact import has failed. It appears that the CSV file you uploaded may not be valid. We kindly request that you review the file and ensure it complies with the required format.<p/>
|
||||
@@ -151,5 +151,20 @@ RSpec.describe DataImportJob do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the CSV file is invalid' do
|
||||
let(:invalid_csv_content) do
|
||||
"id,name,email,phone_number,company\n1,\"Clarice Uzzell,\"missing_quote,918080808080,Acmecorp\n2,Marieann Creegan,,+918080808081,Acmecorp"
|
||||
end
|
||||
|
||||
before do
|
||||
allow(data_import.import_file).to receive(:download).and_return(invalid_csv_content)
|
||||
end
|
||||
|
||||
it 'does not import any data and handles the MalformedCSVError' do
|
||||
expect { described_class.perform_now(data_import) }
|
||||
.to change { data_import.reload.status }.from('pending').to('failed')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user