From c899cc825d25a7d39c5a957a7e4deabc8f7d23f3 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 18 Jan 2024 13:05:58 +0530 Subject: [PATCH] fix: Handle Contact import `MalformedCSVError` (#8706) --- app/jobs/data_import_job.rb | 17 +++++++++++++++-- .../channel_notifications_mailer.rb | 9 +++++++++ .../contact_import_failed.liquid | 3 +++ spec/jobs/data_import_job_spec.rb | 15 +++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 app/views/mailers/administrator_notifications/channel_notifications_mailer/contact_import_failed.liquid diff --git a/app/jobs/data_import_job.rb b/app/jobs/data_import_job.rb index 0a973c8bd..9703d2e50 100644 --- a/app/jobs/data_import_job.rb +++ b/app/jobs/data_import_job.rb @@ -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 diff --git a/app/mailers/administrator_notifications/channel_notifications_mailer.rb b/app/mailers/administrator_notifications/channel_notifications_mailer.rb index b25a6d66b..8c52a9bd3 100644 --- a/app/mailers/administrator_notifications/channel_notifications_mailer.rb +++ b/app/mailers/administrator_notifications/channel_notifications_mailer.rb @@ -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? diff --git a/app/views/mailers/administrator_notifications/channel_notifications_mailer/contact_import_failed.liquid b/app/views/mailers/administrator_notifications/channel_notifications_mailer/contact_import_failed.liquid new file mode 100644 index 000000000..835cff258 --- /dev/null +++ b/app/views/mailers/administrator_notifications/channel_notifications_mailer/contact_import_failed.liquid @@ -0,0 +1,3 @@ +

Hello,

+ +

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.

diff --git a/spec/jobs/data_import_job_spec.rb b/spec/jobs/data_import_job_spec.rb index ca20ce43b..5ece07e53 100644 --- a/spec/jobs/data_import_job_spec.rb +++ b/spec/jobs/data_import_job_spec.rb @@ -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