diff --git a/app/interactions/actions/contact_create.rb b/app/interactions/actions/contact_create.rb index d31af2e38..84ed10caf 100644 --- a/app/interactions/actions/contact_create.rb +++ b/app/interactions/actions/contact_create.rb @@ -84,6 +84,7 @@ module Actions return false if @error contact.generate_code + contact.email_history = contact.email contact.save end end diff --git a/app/interactions/actions/contact_update.rb b/app/interactions/actions/contact_update.rb index 0cf76d116..1a5b0cd16 100644 --- a/app/interactions/actions/contact_update.rb +++ b/app/interactions/actions/contact_update.rb @@ -112,6 +112,7 @@ module Actions email_changed = contact.will_save_change_to_email? old_email = contact.email_was + contact.email_history = old_email updated = contact.save if updated && email_changed && contact.registrant? diff --git a/app/models/depp/contact.rb b/app/models/depp/contact.rb index 7deb36562..32e7f1eab 100644 --- a/app/models/depp/contact.rb +++ b/app/models/depp/contact.rb @@ -5,7 +5,8 @@ module Depp attr_accessor :id, :name, :email, :phone, :org_name, :ident, :ident_type, :ident_country_code, :street, :city, :zip, :state, :country_code, - :password, :legal_document, :statuses, :code + :password, :legal_document, :statuses, :code, + :email_history DISABLED = 'Disabled' DISCLOSURE_TYPES = [DISABLED, '1', '0'] diff --git a/app/models/validation_event.rb b/app/models/validation_event.rb index 621fec030..77facf4db 100644 --- a/app/models/validation_event.rb +++ b/app/models/validation_event.rb @@ -62,6 +62,7 @@ class ValidationEvent < ApplicationRecord if object.need_to_start_force_delete? start_force_delete elsif object.need_to_lift_force_delete? + refresh_status_notes lift_force_delete end end @@ -70,6 +71,23 @@ class ValidationEvent < ApplicationRecord Domains::ForceDeleteEmail::Base.run(email: email) end + def refresh_status_notes + old_email = object.email_history + domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten + registrant_ids = Registrant.where(email: email).pluck(:id) + + domains = domain_contacts.map(&:domain).flatten + + Domain.where(registrant_id: registrant_ids) + + domains.uniq.each do |domain| + next unless domain.status_notes[DomainStatus::FORCE_DELETE] + + domain.status_notes[DomainStatus::FORCE_DELETE].slice!(old_email) + domain.status_notes[DomainStatus::FORCE_DELETE].lstrip! + domain.save(validate: false) + end + end + def lift_force_delete # domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten # registrant_ids = Registrant.where(email: email).pluck(:id) diff --git a/db/migrate/20220113201642_add_email_history_to_contacts.rb b/db/migrate/20220113201642_add_email_history_to_contacts.rb new file mode 100644 index 000000000..2a5f59953 --- /dev/null +++ b/db/migrate/20220113201642_add_email_history_to_contacts.rb @@ -0,0 +1,9 @@ +class AddEmailHistoryToContacts < ActiveRecord::Migration[6.1] + def change + add_column :contacts, :email_history, :string + + reversible do |dir| + dir.up { Contact.update_all('email_history = email') } + end + end +end diff --git a/test/models/domain/force_delete_test.rb b/test/models/domain/force_delete_test.rb index f3294f034..6cae1c3a5 100644 --- a/test/models/domain/force_delete_test.rb +++ b/test/models/domain/force_delete_test.rb @@ -411,6 +411,57 @@ class ForceDeleteTest < ActionMailer::TestCase assert notification.text.include? asserted_text end + def test_force_delete_notes + domain = domains(:airport) + domain.update(valid_to: Time.zone.parse('2012-08-05')) + assert_not domain.force_delete_scheduled? + travel_to Time.zone.parse('2010-07-05') + email_1 = '`@internet.ee' + asserted_text = "Invalid email: #{email_1}" + + Truemail.configure.default_validation_type = :regex + + contact_first = domain.admin_contacts.first + contact_first.update_attribute(:email, email_1) + + ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do + contact_first.verify_email + end + + assert contact_first.email_verification_failed? + + domain.reload + email_2 = '`@internet2.ee' + contact_second = domain.admin_contacts.last + contact_second.update_attribute(:email, email_2) + + ValidationEvent::VALID_EVENTS_COUNT_THRESHOLD.times do + contact_second.verify_email + end + + assert contact_second.email_verification_failed? + + domain.reload + contact_first.update( + email: 'correct_email@internet2.ee', + email_history: email_1 + ) + + + contact_first.verify_email + + + domain.reload + assert domain.force_delete_scheduled? + assert_equal 'invalid_email', domain.template_name + assert_equal Date.parse('2010-09-19'), domain.force_delete_date.to_date + assert_equal Date.parse('2010-08-05'), domain.force_delete_start.to_date + + assert_equal domain.status_notes[DomainStatus::FORCE_DELETE], email_2 + notification = domain.registrar.notifications.last + assert notification.text.include? asserted_text + end + def test_domain_should_have_several_bounced_emails @domain.update(valid_to: Time.zone.parse('2012-08-05')) assert_not @domain.force_delete_scheduled? @@ -458,7 +509,7 @@ class ForceDeleteTest < ActionMailer::TestCase notification = @domain.registrar.notifications.last assert notification.text.include? asserted_text - @domain.registrant.update(email: 'aaa@bbb.com') + @domain.registrant.update(email: 'aaa@bbb.com', email_history: email) @domain.registrant.verify_email assert @domain.registrant.need_to_lift_force_delete? CheckForceDeleteLift.perform_now