Story #109367018 - add new model to handle domain values are changing, and need to be captured now, not later

New model will provide data to DomainMailer customized as needed for the invocation reason as passed via symbol to
send_mail(). The mail templetes then required modification to use the new data model. This should handle all the data
errors in the update process, including pendingUpdate, confirmed, rejected or expired.
This commit is contained in:
Matt Farnsworth 2015-12-16 16:14:05 +02:00
parent 2c916001ad
commit 51a5798914
17 changed files with 366 additions and 254 deletions

View file

@ -239,7 +239,7 @@ class Domain < ActiveRecord::Base
end
count += 1
if domain.pending_update?
DomainMailer.pending_update_expired_notification_for_new_registrant(domain.id).deliver
send_mail :pending_update_expired_notification_for_new_registrant
end
if domain.pending_delete? || domain.pending_delete_confirmation?
DomainMailer.pending_delete_expired_notification(domain.id, true).deliver
@ -451,8 +451,8 @@ class Domain < ActiveRecord::Base
new_registrant_email = registrant.email
new_registrant_name = registrant.name
DomainMailer.pending_update_request_for_old_registrant(id, registrant_id_was, deliver_emails).deliver
DomainMailer.pending_update_notification_for_new_registrant(id, registrant_id_was, deliver_emails).deliver
send_mail :pending_update_request_for_old_registrant
send_mail :pending_update_notification_for_new_registrant
reload # revert back to original
@ -818,5 +818,10 @@ class Domain < ActiveRecord::Base
status_notes[status] = notes[i]
end
end
def send_mail(action)
DomainMailer.send(action, DomainMailModel.new(self).send(action)).deliver
end
end
# rubocop: enable Metrics/ClassLength

View file

@ -0,0 +1,176 @@
class DomainMailModel
# Capture current values used in app/views/mailers/domain_mailer/* and app/mailers/domain_mailer will send later
def initialize(domain)
@domain = domain
@params = {errors: [], deliver_emails: domain.deliver_emails, id: domain.id}
end
def pending_update_request_for_old_registrant
registrant_old
subject(:pending_update_request_for_old_registrant_subject)
confirm_update
domain_info
compose
end
def pending_update_notification_for_new_registrant
# NB! new registrant at this point
registrant
subject(:pending_update_notification_for_new_registrant_subject)
domain_info
compose
end
def registrant_updated_notification_for_new_registrant
registrant
subject(:registrant_updated_notification_for_new_registrant_subject)
domain_info
compose
end
def registrant_updated_notification_for_old_registrant
registrant_old
subject(:registrant_updated_notification_for_old_registrant_subject)
registrant_name
compose
end
def pending_update_rejected_notification_for_new_registrant
registrant_pending
subject(:pending_update_rejected_notification_for_new_registrant_subject)
@params[:deliver_emails] = true # trigger from que
@params[:registrar_name] = @domain.registrar.name
compose
end
def pending_update_expired_notification_for_new_registrant
registrant_pending
subject(:pending_update_expired_notification_for_new_registrant_subject)
compose
end
def pending_deleted
registrant
subject(:domain_pending_deleted_subject)
confirm_delete
compose
end
def pending_delete_rejected_notification
registrant
subject(:pending_delete_rejected_notification_subject)
compose
end
def pending_delete_expired_notification
registrant
subject(:pending_delete_expired_notification_subject)
compose
end
def delete_confirmation
registrant
subject(:delete_confirmation_subject)
compose
end
def force_delete
admins
subject(:force_delete_subject)
compose
end
private
def registrant_old
@params[:recipient] = format Registrant.find(@domain.registrant_id_was).email
end
def registrant
@params[:recipient] = format @domain.registrant.email
end
def registrant_name
@params[:registrant_name] = @domain.registrant.name
end
def registrant_pending
@params[:new_registrant_emai] = @domain.pending_json['new_registrant_email']
@params[:new_registrant_name] = @domain.pending_json['new_registrant_name']
end
# registrant and domain admin contacts
def admins
emails = ([@domain.registrant.email] + @domain.admin_contacts.map { |x| format(x.email) })
@params[:recipient] = emails.uniq.map { |x| format(x) }
end
# puny internet domain name, TODO: username<email>
def format(email)
user, host = email.split('@')
host = SimpleIDN.to_ascii(host)
"#{user}@#{host}"
end
def subject(subject)
@params[:name] = @domain.name
@params[:subject] = I18n.t(subject, name: @domain.name)
end
def confirm_update
verification_url('domain_update_confirms')
end
def confirm_delete
verification_url('domain_delete_confirms')
end
def compose
@params
end
#private
def verification_url(path)
token = verification_token or return
@params[:verification_url] = "#{ENV['registrant_url']}/registrant/#{path}/#{@domain.id}?token=#{token}"
end
def verification_token
return warn_missing(:registrant_verification_token) if @domain.registrant_verification_token.blank?
return warn_missing(:registrant_verification_asked_at) if @domain.registrant_verification_asked_at.blank?
@domain.registrant_verification_token
end
def domain_info
[:name, :registrar_name,
:registrant_name, :registrant_ident, :registrant_email,
:registrant_street,:registrant_city, :registrant_country
].each do |attr|
@params.store attr, @domain.send(attr)
end
@params.store :registrant_priv, @domain.registrant.priv?
@params.store old_registrant_name, Registrant.find(@domain.registrant_id_was).name
@params
end
def warn_no_email(item)
#reason = "#{item.to_s} for #{@domain.name}"
warn_not_delivered item #reason
nil
end
def warn_missing(item)
warn_not_delivered "#{item.to_s} is missing for #{@domain.name}"
end
def warn_not_delivered(reason)
message = "EMAIL NOT DELIVERED: #{reason}"
@params[:errors] << message
# Rails.logger.warn message
nil
end
end

View file

@ -500,7 +500,6 @@ class Epp::Domain < Domain
# rubocop: enable Metrics/CyclomaticComplexity
def apply_pending_update!
old_registrant_email = DomainMailer.registrant_updated_notification_for_old_registrant(id, deliver_emails)
preclean_pendings
user = ApiUser.find(pending_json['current_user_id'])
frame = Nokogiri::XML(pending_json['frame'])
@ -512,8 +511,8 @@ class Epp::Domain < Domain
return unless update(frame, user, false)
clean_pendings!
self.deliver_emails = true # turn on email delivery
DomainMailer.registrant_updated_notification_for_new_registrant(id, deliver_emails).deliver
old_registrant_email.deliver
send_mail :registrant_updated_notification_for_old_registrant
send_mail :registrant_updated_notification_for_new_registrant
true
end