Skip to content

Commit

Permalink
Add mail notification on soft force delete
Browse files Browse the repository at this point in the history
  • Loading branch information
yulgolem committed Jan 15, 2020
1 parent 9916ca5 commit c252d80
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 34 deletions.
23 changes: 17 additions & 6 deletions app/controllers/admin/domains/force_delete_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/registrant/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
29 changes: 26 additions & 3 deletions app/models/concerns/domain/force_delete.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions app/models/concerns/job/force_delete_logging.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/models/domain_cron.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20200115102202_add_force_delete_data_to_domains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddForceDeleteDataToDomains < ActiveRecord::Migration[5.0]
def change
add_column :domains, :force_delete_data, :hstore
end
end
7 changes: 5 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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

);


Expand Down Expand Up @@ -4336,6 +4338,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191206183853'),
('20191212133136'),
('20191227110904'),
('20200113091254');
('20200113091254'),
('20200115102202');


1 change: 1 addition & 0 deletions test/models/domain/force_delete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
3 changes: 2 additions & 1 deletion test/system/admin_area/domains/force_delete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
end

0 comments on commit c252d80

Please sign in to comment.