diff --git a/app/controllers/admin/domains/force_delete_controller.rb b/app/controllers/admin/domains/force_delete_controller.rb index 36835bfe50..28a058b4d0 100644 --- a/app/controllers/admin/domains/force_delete_controller.rb +++ b/app/controllers/admin/domains/force_delete_controller.rb @@ -9,17 +9,21 @@ def create domain.registrar.notifications.create!(text: t('force_delete_set_on_domain', domain_name: domain.name)) - if notify_by_email? - DomainDeleteMailer.forced(domain: domain, - registrar: domain.registrar, - registrant: domain.registrant, - template_name: params[:template_name]).deliver_now - end + notify_by_email if notify_by_email? end redirect_to edit_admin_domain_url(domain), notice: t('.scheduled') end + def notify_by_email + if force_delete_type == :fast_track + send_email + domain.update(contact_notification_sent_date: Time.zone.today) + else + domain.update(template_name: params[:template_name]) + end + end + def destroy authorize! :manage, domain domain.cancel_force_delete @@ -36,6 +40,13 @@ def notify_by_email? ActiveRecord::Type::Boolean.new.cast(params[:notify_by_email]) end + def send_email + DomainDeleteMailer.forced(domain: domain, + registrar: domain.registrar, + registrant: domain.registrant, + template_name: params[:template_name]).deliver_now + end + def force_delete_type soft_delete? ? :soft : :fast_track end diff --git a/app/controllers/registrant/domains_controller.rb b/app/controllers/registrant/domains_controller.rb index fc66806e25..216f87e54f 100644 --- a/app/controllers/registrant/domains_controller.rb +++ b/app/controllers/registrant/domains_controller.rb @@ -76,4 +76,4 @@ def search_params params.require(:q).permit(:name_matches, :registrant_ident_eq, :valid_to_gteq, :valid_to_lteq, :results_per_page) end -end \ No newline at end of file +end diff --git a/app/models/concerns/domain/force_delete.rb b/app/models/concerns/domain/force_delete.rb index ffe9973226..0120347c8e 100644 --- a/app/models/concerns/domain/force_delete.rb +++ b/app/models/concerns/domain/force_delete.rb @@ -1,6 +1,18 @@ -module Concerns::Domain::ForceDelete +module Concerns::Domain::ForceDelete # rubocop:disable Metrics/ModuleLength extend ActiveSupport::Concern + included do + store_accessor :force_delete_data, + :force_delete_type, + :contact_notification_sent_date, + :template_name + + scope :notification_not_sent, + lambda { + where("(force_delete_data->>'contact_notification_sent_date') is null") + } + end + class_methods do def force_delete_scheduled where('force_delete_start <= ?', Time.zone.now) @@ -12,8 +24,8 @@ def force_delete_scheduled? end def should_notify_on_soft_force_delete? - force_delete_scheduled? && !statuses.include?(DomainStatus::CLIENT_HOLD) && - force_delete_start.present? && force_delete_start.to_date == Time.zone.now.to_date + force_delete_scheduled? && contact_notification_sent_date.blank? && + force_delete_start.to_date == Time.zone.now.to_date && force_delete_type.to_sym == :soft end def client_holdable? @@ -37,9 +49,14 @@ def schedule_force_delete(type: :fast_track) type == :fast_track ? force_delete_fast_track : force_delete_soft end + def add_force_delete_type(force_delete_type) + self.force_delete_type = force_delete_type + end + def force_delete_fast_track preserve_current_statuses_for_force_delete add_force_delete_statuses + add_force_delete_type(:fast) self.force_delete_date = force_delete_fast_track_start_date + 1.day self.force_delete_start = Time.zone.today + 1.day stop_all_pending_actions @@ -50,15 +67,21 @@ def force_delete_fast_track def force_delete_soft preserve_current_statuses_for_force_delete add_force_delete_statuses + add_force_delete_type(:soft) calculate_soft_delete_date stop_all_pending_actions allow_deletion save(validate: false) end + def clear_force_delete_data + self.force_delete_data = nil + end + def cancel_force_delete restore_statuses_before_force_delete remove_force_delete_statuses + clear_force_delete_data self.force_delete_date = nil self.force_delete_start = nil save(validate: false) diff --git a/app/models/concerns/job/force_delete_logging.rb b/app/models/concerns/job/force_delete_logging.rb new file mode 100644 index 0000000000..072c5b98f7 --- /dev/null +++ b/app/models/concerns/job/force_delete_logging.rb @@ -0,0 +1,28 @@ +module Concerns + module Job + module ForceDeleteLogging + extend ActiveSupport::Concern + + class_methods do + def log_prepare_client_hold + return if Rails.env.test? + + STDOUT << "#{Time.zone.now.utc} - Setting client_hold to domains\n" + end + + def log_start_client_hold(domain) + return if Rails.env.test? + + STDOUT << "#{Time.zone.now.utc} DomainCron.start_client_hold: ##{domain.id} "\ + "(#{domain.name}) #{domain.changes}\n" + end + + def log_end_end_client_hold(domain) + return if Rails.env.test? + + STDOUT << "#{Time.zone.now.utc} - Successfully set client_hold on (#{domain.name})" + end + end + end + end +end diff --git a/app/models/concerns/job/force_delete_messages.rb b/app/models/concerns/job/force_delete_notify.rb similarity index 56% rename from app/models/concerns/job/force_delete_messages.rb rename to app/models/concerns/job/force_delete_notify.rb index cb4f298a86..bc73226455 100644 --- a/app/models/concerns/job/force_delete_messages.rb +++ b/app/models/concerns/job/force_delete_notify.rb @@ -1,28 +1,9 @@ module Concerns module Job - module ForceDeleteMessages + module ForceDeleteNotify extend ActiveSupport::Concern class_methods do - def log_prepare_client_hold - return if Rails.env.test? - - STDOUT << "#{Time.zone.now.utc} - Setting client_hold to domains\n" - end - - def log_start_client_hold(domain) - return if Rails.env.test? - - STDOUT << "#{Time.zone.now.utc} DomainCron.start_client_hold: ##{domain.id} "\ - "(#{domain.name}) #{domain.changes}\n" - end - - def log_end_end_client_hold(domain) - return if Rails.env.test? - - STDOUT << "#{Time.zone.now.utc} - Successfully set client_hold on (#{domain.name})" - end - def notify_client_hold(domain) domain.registrar.notifications.create!(text: I18n.t('client_hold_set_on_domain', domain_name: domain.name, @@ -33,6 +14,15 @@ def notify_on_grace_period(domain) domain.registrar.notifications.create!(text: I18n.t('grace_period_started_domain', domain_name: domain.name, date: domain.force_delete_start)) + send_mail(domain) + domain.update(contact_notification_sent_date: Time.zone.today) + end + + def send_mail(domain) + DomainDeleteMailer.forced(domain: domain, + registrar: domain.registrar, + registrant: domain.registrant, + template_name: domain.template_name).deliver_now end end end diff --git a/app/models/domain_cron.rb b/app/models/domain_cron.rb index 47e5a99b55..bc075c0677 100644 --- a/app/models/domain_cron.rb +++ b/app/models/domain_cron.rb @@ -1,6 +1,7 @@ class DomainCron include Concerns::Job::ForceDelete - include Concerns::Job::ForceDeleteMessages + include Concerns::Job::ForceDeleteLogging + include Concerns::Job::ForceDeleteNotify def self.clean_expired_pendings STDOUT << "#{Time.zone.now.utc} - Clean expired domain pendings\n" unless Rails.env.test? diff --git a/db/migrate/20200115102202_add_force_delete_data_to_domains.rb b/db/migrate/20200115102202_add_force_delete_data_to_domains.rb new file mode 100644 index 0000000000..2ecc7ceaf3 --- /dev/null +++ b/db/migrate/20200115102202_add_force_delete_data_to_domains.rb @@ -0,0 +1,5 @@ +class AddForceDeleteDataToDomains < ActiveRecord::Migration[5.0] + def change + add_column :domains, :force_delete_data, :hstore + end +end diff --git a/db/structure.sql b/db/structure.sql index 7d15804a41..a23623bae4 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -744,7 +744,9 @@ CREATE TABLE public.domains ( up_date timestamp without time zone, uuid uuid DEFAULT public.gen_random_uuid() NOT NULL, locked_by_registrant_at timestamp without time zone, - force_delete_start timestamp without time zone + force_delete_start timestamp without time zone, + force_delete_data public.hstore + ); @@ -4336,6 +4338,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20191206183853'), ('20191212133136'), ('20191227110904'), -('20200113091254'); +('20200113091254'), +('20200115102202'); diff --git a/test/models/domain/force_delete_test.rb b/test/models/domain/force_delete_test.rb index f59cf58bd3..5945ac807d 100644 --- a/test/models/domain/force_delete_test.rb +++ b/test/models/domain/force_delete_test.rb @@ -201,6 +201,7 @@ def test_not_sets_hold_before_treshold asserted_status = DomainStatus::CLIENT_HOLD @domain.update_columns(valid_to: Time.zone.parse('2010-10-05'), force_delete_date: nil) + @domain.update(template_name: 'legal_person') travel_to Time.zone.parse('2010-07-05') diff --git a/test/system/admin_area/domains/force_delete_test.rb b/test/system/admin_area/domains/force_delete_test.rb index 5e135bf244..b8af02f946 100644 --- a/test/system/admin_area/domains/force_delete_test.rb +++ b/test/system/admin_area/domains/force_delete_test.rb @@ -31,6 +31,7 @@ def test_notifies_registrar def test_notifies_registrant_and_admin_contacts_by_email_by_default assert_emails 1 do visit edit_admin_domain_url(@domain) + find(:css, '#soft_delete').set(false) click_link_or_button 'Force delete domain' end end @@ -62,4 +63,4 @@ def test_force_delete_procedure_cannot_be_scheduled_on_a_discarded_domain assert_no_button 'Schedule force delete' assert_no_link 'Schedule force delete' end -end \ No newline at end of file +end