diff --git a/app/jobs/bounced_emails_cleaner_job.rb b/app/jobs/bounced_emails_cleaner_job.rb new file mode 100644 index 000000000..aa8ccc01e --- /dev/null +++ b/app/jobs/bounced_emails_cleaner_job.rb @@ -0,0 +1,10 @@ +class BouncedEmailsCleanerJob < ApplicationJob + queue_as :default + + def perform + BouncedMailAddress.find_each do |bounce| + count = Contact.where(email: bounce.email).count + bounce.destroy if count == 0 + end + end +end \ No newline at end of file diff --git a/test/models/bounced_mail_address_test.rb b/test/models/bounced_mail_address_test.rb index 2a24c00b0..89ee1dda2 100644 --- a/test/models/bounced_mail_address_test.rb +++ b/test/models/bounced_mail_address_test.rb @@ -16,6 +16,21 @@ class BouncedMailAddressTest < ActiveSupport::TestCase @contact_email = "john@inbox.test" end + def test_remove_bounced_email_after_changed_related_email + @bounced_mail.update(email: @contact_email) + @bounced_mail.save + + contacts_with_bounced_mails = Contact.where(email: @contact_email) + contacts_with_bounced_mails.each do |contact| + contact.email = "sara@inbox.com" + contact.save + end + + BouncedEmailsCleanerJob.perform_now + + assert_nil BouncedMailAddress.find_by(email: @contact_email) + end + def test_soft_force_delete_related_domains domain_contacts = Contact.where(email: @contact_email).map(&:domain_contacts).flatten