From a091c62264ba77085cfe0872b586c5e1621b072c Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Fri, 8 Apr 2016 14:58:56 +0300 Subject: [PATCH] Story#117124725 - generate emails of apply_pending_update! only when everything is ok --- app/mailers/domain_mailer.rb | 30 +++++++++++++-- app/models/domain_mail_model.rb | 21 ---------- app/models/epp/domain.rb | 10 +++-- ...d_notification_for_new_registrant.html.erb | 36 +++++++++--------- ...d_notification_for_new_registrant.text.erb | 38 ++++++++++--------- ...d_notification_for_old_registrant.html.erb | 36 +++++++++--------- ...d_notification_for_old_registrant.text.erb | 36 +++++++++--------- spec/mailers/domain_mailer_spec.rb | 4 +- 8 files changed, 108 insertions(+), 103 deletions(-) diff --git a/app/mailers/domain_mailer.rb b/app/mailers/domain_mailer.rb index 28e232a8c..fb2c0e851 100644 --- a/app/mailers/domain_mailer.rb +++ b/app/mailers/domain_mailer.rb @@ -9,12 +9,34 @@ class DomainMailer < ApplicationMailer compose_from(params) end - def registrant_updated_notification_for_new_registrant(params) - compose_from(params) + + def registrant_updated_notification_for_new_registrant(domain_id, old_registrant_id, new_registrant_id, should_deliver) + @domain = Domain.find_by(id: domain_id) + return unless @domain + return if delivery_off?(@domain, should_deliver) + + @old_registrant = Registrant.find(old_registrant_id) + @new_registrant = Registrant.find(new_registrant_id) + + return if whitelist_blocked?(@new_registrant.email) + mail(to: format(@new_registrant.email), + subject: "#{I18n.t(:registrant_updated_notification_for_new_registrant_subject, + name: @domain.name)} [#{@domain.name}]") end - def registrant_updated_notification_for_old_registrant(params) - compose_from(params) + + def registrant_updated_notification_for_old_registrant(domain_id, old_registrant_id, new_registrant_id, should_deliver) + domain = Domain.find_by(id: domain_id) + return unless domain + return if delivery_off?(@domain, should_deliver) + + @old_registrant = Registrant.find(old_registrant_id) + @new_registrant = Registrant.find(new_registrant_id) + + return if whitelist_blocked?(@old_registrant.email) + mail(to: format(@old_registrant.email), + subject: "#{I18n.t(:registrant_updated_notification_for_old_registrant_subject, + name: @domain.name)} [#{@domain.name}]") end def pending_update_rejected_notification_for_new_registrant(params) diff --git a/app/models/domain_mail_model.rb b/app/models/domain_mail_model.rb index 46309ad52..d443d8783 100644 --- a/app/models/domain_mail_model.rb +++ b/app/models/domain_mail_model.rb @@ -20,28 +20,7 @@ class DomainMailModel 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_pending - registrant_old - subject(:registrant_updated_notification_for_old_registrant_subject) - new_registrant = Registrant.find @domain.pending_json['new_registrant_id'] - @params[:registrant_name] = new_registrant.name - @params[:registrant_ident] = new_registrant.ident - @params[:registrant_priv] = new_registrant.priv? - @params[:registrant_email] = new_registrant.email - @params[:registrant_street] = new_registrant.street - @params[:registrant_city] = new_registrant.city - @params[:registrant_country] = new_registrant.country.name - compose - end def pending_update_rejected_notification_for_new_registrant registrant_pending diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 1f2dd2d54..2905bd968 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -525,19 +525,21 @@ class Epp::Domain < Domain preclean_pendings user = ApiUser.find(pending_json['current_user_id']) frame = Nokogiri::XML(pending_json['frame']) + old_registrant_id = registrant_id self.deliver_emails = true # turn on email delivery self.statuses.delete(DomainStatus::PENDING_UPDATE) ::PaperTrail.whodunnit = user.id_role_username # updator str should be the request originator not the approval user - send_mail :registrant_updated_notification_for_old_registrant return unless update(frame, user, false) clean_pendings! - send_mail :registrant_updated_notification_for_new_registrant - WhoisRecord.find_by(domain_id: id).save # need to reload model - save! # for notification if everything fails + + WhoisRecord.find_by(domain_id: id).save # need to reload model + DomainMailer.registrant_updated_notification_for_old_registrant(id, old_registrant_id, registrant_id, should_deliver) + DomainMailer.registrant_updated_notification_for_new_registrant(id, old_registrant_id, registrant_id, should_deliver) + true end diff --git a/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.html.erb b/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.html.erb index c7d464f43..a654b2f80 100644 --- a/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.html.erb +++ b/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.html.erb @@ -1,18 +1,18 @@ Tere,

-Domeeni <%= @params[:name] %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud. +Domeeni <%= @domain.name %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud.

Uue registreerija andmed:
-Nimi: <%= @params[:registrant_name] %>
-<% if @params[:registrant_priv] %> -Isikukood: <%= @params[:registrant_ident] %>
+Nimi: <%= @new_registrant.name %>
+<% if @new_registrant.priv? %> + Isikukood: <%= @new_registrant.ident %>
<% else %> -Äriregistrikood: <%= @params[:registrant_ident] %>
+ Äriregistrikood: <%= @new_registrant.ident %>
<% end %> -Epost: <%= @params[:registrant_email] %>
-Tänav: <%= @params[:registrant_street] %>
-Linn: <%= @params[:registrant_city] %>
-Riik: <%= @params[:registrant_country] %> +Epost: <%= @new_registrant.email %>
+Tänav: <%= @new_registrant.street %>
+Linn: <%= @new_registrant.city %>
+Riik: <%= @new_registrant.country.name %>

Lugupidamisega
Eesti Interneti SA @@ -21,19 +21,19 @@ Eesti Interneti SA

Hi,

-Process for changing registrant of the domain <%= @params[:name] %> has been approved and the data in the registry is updated. +Process for changing registrant of the domain <%= @domain.name %> has been approved and the data in the registry is updated.

New registrant:
-Name: <%= @params[:registrant_name] %>
-<% if @params[:registrant_priv] %> -Personal code: <%= @params[:registrant_ident] %>
+Name: <%= @new_registrant.name %>
+<% if @new_registrant.priv? %> + Personal code: <%= @new_registrant.ident %>
<% else %> -Business Registry code: <%= @params[:registrant_ident] %>
+ Business Registry code: <%= @new_registrant.ident %>
<% end %> -E-mail: <%= @params[:registrant_email] %>
-Street: <%= @params[:registrant_street] %>
-City: <%= @params[:registrant_city] %>
-Country: <%= @params[:registrant_country] %> +E-mail: <%= @new_registrant.email %>
+Street: <%= @new_registrant.street %>
+City: <%= @new_registrant.city %>
+Country: <%= @new_registrant.country.name %>

Best Regards,
Estonian Internet Foundation diff --git a/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.text.erb b/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.text.erb index 115655897..64db26ec7 100644 --- a/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.text.erb +++ b/app/views/mailers/domain_mailer/registrant_updated_notification_for_new_registrant.text.erb @@ -1,18 +1,19 @@ Tere, -Domeeni <%= @params[:name] %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud. +Domeeni <%= @domain.name %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud. Uue registreerija andmed: -Nimi: <%= @params[:registrant_name] %> -<% if @params[:registrant_priv] %> -Isikukood: <%= @params[:registrant_ident] %> +Nimi: <%= @new_registrant.name %> + +<% if @new_registrant.priv? %> + Isikukood: <%= @new_registrant.ident %> <% else %> -Äriregistrikood: <%= @params[:registrant_ident] %> + Äriregistrikood: <%= @new_registrant.ident %> <% end %> -Epost: <%= @params[:registrant_email] %> -Tänav: <%= @params[:registrant_street] %> -Linn: <%= @params[:registrant_city] %> -Riik: <%= @params[:registrant_country] %> +Epost: <%= @new_registrant.email %> +Tänav: <%= @new_registrant.street %> +Linn: <%= @new_registrant.city %> +Riik: <%= @new_registrant.country.name %> Lugupidamisega Eesti Interneti SA @@ -21,19 +22,20 @@ Eesti Interneti SA Hi, -Process for changing registrant of the domain <%= @params[:name] %> has been approved and the data in the registry is updated. +Process for changing registrant of the domain <%= @domain.name %> has been approved and the data in the registry is updated. New registrant: -Name: <%= @params[:registrant_name] %> -<% if @params[:registrant_priv] %> -Personal code: <%= @params[:registrant_ident] %> +Name: <%= @new_registrant.name %> + +<% if @new_registrant.priv? %> + Personal code: <%= @new_registrant.ident %> <% else %> -Business Registry code: <%= @params[:registrant_ident] %> + Business Registry code: <%= @new_registrant.ident %> <% end %> -E-mail: <%= @params[:registrant_email] %> -Street: <%= @params[:registrant_street] %> -City: <%= @params[:registrant_city] %> -Country: <%= @params[:registrant_country] %> +E-mail: <%= @new_registrant.email %> +Street: <%= @new_registrant.street %> +City: <%= @new_registrant.city %> +Country: <%= @new_registrant.country.name %> Best Regards, Estonian Internet Foundation diff --git a/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.html.erb b/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.html.erb index b41e1f1eb..180988f08 100644 --- a/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.html.erb +++ b/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.html.erb @@ -1,18 +1,18 @@ Tere,

-Domeeni <%= @params[:name] %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud. +Domeeni <%= @domain.name %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud.

Uue registreerija andmed:
-Nimi: <%= @params[:new_registrant_name] %>
-<% if @params[:registrant_priv] %> -Isikukood: <%= @params[:registrant_ident] %>
+Nimi: <%= @new_registrant.name %>
+<% if @new_registrant.priv? %> +Isikukood: <%= @new_registrant.ident %>
<% else %> -Äriregistrikood: <%= @params[:registrant_ident] %>
+Äriregistrikood: <%= @new_registrant.ident %>
<% end %> -Epost: <%= @params[:registrant_email] %>
-Tänav: <%= @params[:registrant_street] %>
-Linn: <%= @params[:registrant_city] %>
-Riik: <%= @params[:registrant_country] %> +Epost: <%= @new_registrant.email %>
+Tänav: <%= @new_registrant.street %>
+Linn: <%= @new_registrant.city %>
+Riik: <%= @new_registrant.country.name %>

Lugupidamisega
Eesti Interneti SA @@ -21,19 +21,19 @@ Eesti Interneti SA

Hi,

-Process for changing registrant of the domain <%= @params[:name] %> has been approved and the data in the registry is updated. +Process for changing registrant of the domain <%= @domain.name %> has been approved and the data in the registry is updated.

New registrant:
-Name: <%= @params[:new_registrant_name] %>
-<% if @params[:registrant_priv] %> -Personal code: <%= @params[:registrant_ident] %>
+Name: <%= @new_registrant.name %>
+<% if @new_registrant.priv? %> +Personal code: <%= @new_registrant.ident %>
<% else %> -Business Registry code: <%= @params[:registrant_ident] %>
+Business Registry code: <%= @new_registrant.ident %>
<% end %> -E-mail: <%= @params[:registrant_email] %>
-Street: <%= @params[:registrant_street] %>
-City: <%= @params[:registrant_city] %>
-Country: <%= @params[:registrant_country] %> +E-mail: <%= @new_registrant.email %>
+Street: <%= @new_registrant.street %>
+City: <%= @new_registrant.city %>
+Country: <%= @new_registrant.country.name %>

Best Regards,
Estonian Internet Foundation diff --git a/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.text.erb b/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.text.erb index c2efa5af9..1d45fb5e0 100644 --- a/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.text.erb +++ b/app/views/mailers/domain_mailer/registrant_updated_notification_for_old_registrant.text.erb @@ -1,19 +1,19 @@ Tere, -Domeeni <%= @params[:name] %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud. +Domeeni <%= @domain.name %> registreerija vahetuse taotlus on kinnitatud ning andmed registris uuendatud. Uue registreerija andmed: -Nimi: <%= @params[:new_registrant_name] %> +Nimi: <%= @new_registrant.name %> -<% if @params[:registrant_priv] %> -Isikukood: <%= @params[:registrant_ident] %> +<% if @new_registrant.priv? %> +Isikukood: <%= @new_registrant.ident %> <% else %> -Äriregistrikood: <%= @params[:registrant_ident] %> +Äriregistrikood: <%= @new_registrant.ident %> <% end %> -Epost: <%= @params[:registrant_email] %> -Tänav: <%= @params[:registrant_street] %> -Linn: <%= @params[:registrant_city] %> -Riik: <%= @params[:registrant_country] %> +Epost: <%= @new_registrant.email %> +Tänav: <%= @new_registrant.street %> +Linn: <%= @new_registrant.city %> +Riik: <%= @new_registrant.country.name %> Lugupidamisega Eesti Interneti SA @@ -22,20 +22,20 @@ Eesti Interneti SA Hi, -Process for changing registrant of the domain <%= @params[:name] %> has been approved and the data in the registry is updated. +Process for changing registrant of the domain <%= @domain.name %> has been approved and the data in the registry is updated. New registrant: -Name: <%= @params[:new_registrant_name] %> +Name: <%= @new_registrant.name %> -<% if @params[:registrant_priv] %> -Personal code: <%= @params[:registrant_ident] %> +<% if @new_registrant.priv? %> +Personal code: <%= @new_registrant.ident %> <% else %> -Business Registry code: <%= @params[:registrant_ident] %> +Business Registry code: <%= @new_registrant.ident %> <% end %> -E-mail: <%= @params[:registrant_email] %> -Street: <%= @params[:registrant_street] %> -City: <%= @params[:registrant_city] %> -Country: <%= @params[:registrant_country] %> +E-mail: <%= @new_registrant.email %> +Street: <%= @new_registrant.street %> +City: <%= @new_registrant.city %> +Country: <%= @new_registrant.country.name %> Best Regards, Estonian Internet Foundation diff --git a/spec/mailers/domain_mailer_spec.rb b/spec/mailers/domain_mailer_spec.rb index 3f645f7e0..9963fdd38 100644 --- a/spec/mailers/domain_mailer_spec.rb +++ b/spec/mailers/domain_mailer_spec.rb @@ -153,7 +153,7 @@ describe DomainMailer do @registrant = Fabricate(:registrant, email: 'test@example.com') @domain = Fabricate(:domain, registrant: @registrant) @domain.deliver_emails = true - @mail = DomainMailer.registrant_updated_notification_for_new_registrant(@domain.id, deliver_emails) + @mail = DomainMailer.registrant_updated_notification_for_new_registrant(@domain.id, @registrant.id, @registrant.id, deliver_emails) end it 'should render email subject' do @@ -178,7 +178,7 @@ describe DomainMailer do @registrant = Fabricate(:registrant, email: 'test@example.com') @domain = Fabricate(:domain, registrant: @registrant) @domain.deliver_emails = true - @mail = DomainMailer.registrant_updated_notification_for_old_registrant(@domain.id, deliver_emails) + @mail = DomainMailer.registrant_updated_notification_for_old_registrant(@domain.id, @registrant.id, @registrant.id, deliver_emails) end it 'should render email subject' do