From 08383b9cac174bb82407434443a8c8e806051894 Mon Sep 17 00:00:00 2001 From: taitus Date: Mon, 11 Feb 2019 14:04:22 +0100 Subject: [PATCH] Send new notification email after publish Proposal After publish a proposal send email to proposal's author with information text and new actions available. --- app/mailers/dashboard/mailer.rb | 8 ++- app/models/proposal.rb | 9 +++ ...actions_notification_on_published.html.erb | 64 +++++++++++++++++++ config/locales/en/mailers.yml | 16 +++++ config/locales/es/mailers.yml | 16 +++++ spec/mailers/dashboard/mailer_spec.rb | 36 +++++++++++ .../previews/dashboard_mailer_preview.rb | 7 ++ spec/models/proposal_spec.rb | 28 ++++++++ 8 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 app/views/dashboard/mailer/new_actions_notification_on_published.html.erb diff --git a/app/mailers/dashboard/mailer.rb b/app/mailers/dashboard/mailer.rb index e7cbc9a36..f60119a50 100644 --- a/app/mailers/dashboard/mailer.rb +++ b/app/mailers/dashboard/mailer.rb @@ -23,8 +23,14 @@ class Dashboard::Mailer < ApplicationMailer mail to: proposal.author.email, subject: I18n.t("mailers.new_actions_notification_on_create.subject") end + def new_actions_notification_on_published(proposal, new_actions_ids) + @proposal = proposal + @new_actions = get_new_actions(new_actions_ids) + mail to: proposal.author.email, subject: I18n.t("mailers.new_actions_notification_on_published.subject") + end + private def get_new_actions(new_actions_ids) Dashboard::Action.where(id: new_actions_ids) - end + end end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 00cf8bec9..07723ea97 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -86,6 +86,7 @@ class Proposal < ActiveRecord::Base def publish update(published_at: Time.now) + send_new_actions_notification_on_published end def published? @@ -246,6 +247,14 @@ class Proposal < ActiveRecord::Base end end + def send_new_actions_notification_on_published + new_actions_ids = Dashboard::Action.detect_new_actions(self) + + if new_actions_ids.present? + Dashboard::Mailer.new_actions_notification_on_published(self, new_actions_ids).deliver_later + end + end + protected def set_responsible_name diff --git a/app/views/dashboard/mailer/new_actions_notification_on_published.html.erb b/app/views/dashboard/mailer/new_actions_notification_on_published.html.erb new file mode 100644 index 000000000..bae02cd64 --- /dev/null +++ b/app/views/dashboard/mailer/new_actions_notification_on_published.html.erb @@ -0,0 +1,64 @@ + + +

+ <%= t("mailers.new_actions_notification_on_published.hi", + name: @proposal.author.name, + title: @proposal.title) %> +

+

+ <%= t("mailers.new_actions_notification_on_published.text_1") %> +
+ <%= t("mailers.new_actions_notification_on_published.text_2") %> +

+

<%= t("mailers.new_actions_notification_on_published.text_3") %>

+

<%= t("mailers.new_actions_notification_on_published.text_4") %>

+

<%= t("mailers.new_actions_notification_on_published.text_5") %>

+

<%= t("mailers.new_actions_notification_on_published.text_6") %>

+ +
+ +

<%= t("mailers.new_actions_notification_on_published.text_7") %>

+ <% first_resource = @new_actions.resources.first %> + <% if first_resource.present? %> +

<%= t("mailers.new_actions_notification_on_published.text_8") %>

+ + <% end %> + + <% first_proposed_action = @new_actions.proposed_actions.first %> + <% if first_proposed_action.present? %> +

<%= t("mailers.new_actions_notification_on_published.text_9") %>

+ + <% end %> +
+ +

<%= t("mailers.new_actions_notification_on_published.text_10") %>

+ + + + + + + +
+ <%= link_to progress_proposal_dashboard_url(@proposal), + style: "font-family: 'Open Sans',arial,sans-serif; background: #3700fd; + border-radius: 6px; color: #fff !important; font-weight: bold; + padding: 17px 20px; text-align: center; text-decoration: none; + font-size: 20px; min-width: 200px; display: inline-block;", + target: "_blank" do %> + <%= t("mailers.new_actions_notification_on_published.dashboard_button") %> + <% end %> +
+ + diff --git a/config/locales/en/mailers.yml b/config/locales/en/mailers.yml index d2ac7287b..e2239c651 100644 --- a/config/locales/en/mailers.yml +++ b/config/locales/en/mailers.yml @@ -116,3 +116,19 @@ en: text_5: "And for you to start with all the motivation," text_6: "here you have several resources and a whole list of tips that will come to you every day to prepare the broadcast!" dashboard_button: "Go ahead, discover them!" + new_actions_notification_on_published: + subject: "Your citizen proposal is already published Dont stop spreading!" + hi: "Congratulations %{name}! Your proposal %{title} has been created successfully." + text_1: "And now, go for your first 100 supports!" + text_2: "Why 100?" + text_3: "Our experience tells us that the first day is fundamental. Because in addition to having the energy to launch something new, being a newly published proposal, you will have the important visibility of being among the new proposals highlighted in Decide Madrid." + text_4: "Get 100 supports on the first day, and you will have a first community to back you up." + text_5: "That is why we challenge you to get it, but not without a lot of help!" + text_6: "Remember that in your Proposal Panel you have new resources available and recommendations for dissemination actions." + list_1: "Come in every day to see your progress and use the tips and resources we will share with you. They are ideas and also practical solutions to get the support you need." + list_2: "As you get more support, you will unlock new and better resources. Do not stop adding support and we will not stop rewarding and helping you!" + text_7: "And for you to start at full speed..." + text_8: "Here is a great resource at your disposal!" + text_9: "You will also find this new recommended dissemination action..." + text_10: "You sure have more resources to use!" + dashboard_button: "Go ahead, discover them!" diff --git a/config/locales/es/mailers.yml b/config/locales/es/mailers.yml index 5d26c0ac7..c5de488ab 100644 --- a/config/locales/es/mailers.yml +++ b/config/locales/es/mailers.yml @@ -116,3 +116,19 @@ es: text_5: "Y para que empieces con toda la motivación," text_6: "¡aquí tienes varios recursos y toda una lista de consejos que te irán llegando cada día para preparar la difusión!" dashboard_button: "Adelante, ¡descúbrelos!" + new_actions_notification_on_published: + subject: "Tu propuesta ciudadana ya está publicada ¡No pares de difundir!" + hi: "¡Enhorabuena %{name}! Tu propuesta %{title} ha sido creado con éxito." + text_1: "Y ahora, ¡a por tus primeros 100 apoyos! " + text_2: "¿Por qué 100?" + text_3: "Nuestra experiencia nos dice que el primer día es fundamental. Porque además de tener la energía de lanzar algo nuevo, al ser una propuesta recién publicada, contarás con la importante visibilidad de estar entre las nuevas propuestas destacadas en Decide Madrid." + text_4: "Logra 100 apoyos en el primer día, y tendrás una primera comunidad en la que respaldarte." + text_5: "Por eso, te retamos a que lo consigas, ¡pero no sin una gran ayuda!" + text_6: "Recuerda que en tu Panel de propuestas tienes nuevos recursos disponibles y recomendaciones de acciones de difusión." + list_1: "Entra cada día para ver tu progreso y utilizar los consejos y recursos que te iremos compartiendo. Son ideas y también soluciones prácticas para lograr los apoyos que necesitas." + list_2: "A medida que logres más apoyos, desbloquearás nuevos y mejores recursos. ¡No dejes de sumar apoyos y nosotros no dejaremos de premiarte y ayudarte!" + text_7: "Y para que empieces a toda máquina..." + text_8: "¡aquí tienes un gran recurso a tu total disposición!" + text_9: "También encontrarás esta nueva acción de difusión recomendada…" + text_10: "¡Seguro que tienes más recursos pendientes de usar!" + dashboard_button: "Adelante, ¡descúbrelos!" diff --git a/spec/mailers/dashboard/mailer_spec.rb b/spec/mailers/dashboard/mailer_spec.rb index d407138a0..3f43be4b2 100644 --- a/spec/mailers/dashboard/mailer_spec.rb +++ b/spec/mailers/dashboard/mailer_spec.rb @@ -112,4 +112,40 @@ describe Dashboard::Mailer do expect(email).to have_body_text("Go ahead, discover them!") end end + + describe "#new_actions_notification_on_published" do + + before do + ActionMailer::Base.deliveries.clear + end + + let!(:proposal) { build(:proposal, :draft) } + let!(:resource) { create(:dashboard_action, :resource, :active, day_offset: 0, published_proposal: true) } + let!(:proposed_action) { create(:dashboard_action, :proposed_action, :active, day_offset: 0, published_proposal: true) } + + it "sends emails when detect new actions when publish a proposal" do + proposal.save + proposal.publish + + email = open_last_email + + expect(email).to deliver_from("CONSUL ") + expect(email).to deliver_to(proposal.author) + expect(email).to have_subject("Your citizen proposal is already published Dont stop spreading!") + expect(email).to have_body_text("Congratulations #{proposal.author.name}! Your proposal #{proposal.title} has been created successfully.") + expect(email).to have_body_text("And now, go for your first 100 supports!") + expect(email).to have_body_text("Why 100?") + expect(email).to have_body_text("Our experience tells us that the first day is fundamental. Because in addition to having the energy to launch something new, being a newly published proposal, you will have the important visibility of being among the new proposals highlighted in Decide Madrid.") + expect(email).to have_body_text("Get 100 supports on the first day, and you will have a first community to back you up.") + expect(email).to have_body_text("That is why we challenge you to get it, but not without a lot of help!") + expect(email).to have_body_text("Remember that in your Proposal Panel you have new resources available and recommendations for dissemination actions.") + expect(email).to have_body_text("Come in every day to see your progress and use the tips and resources we will share with you. They are ideas and also practical solutions to get the support you need.") + expect(email).to have_body_text("As you get more support, you will unlock new and better resources. Do not stop adding support and we will not stop rewarding and helping you!") + expect(email).to have_body_text("And for you to start at full speed...") + expect(email).to have_body_text("Here is a great resource at your disposal!") + expect(email).to have_body_text("You will also find this new recommended dissemination action...") + expect(email).to have_body_text("You sure have more resources to use!") + expect(email).to have_body_text("Go ahead, discover them!") + end + end end diff --git a/spec/mailers/previews/dashboard_mailer_preview.rb b/spec/mailers/previews/dashboard_mailer_preview.rb index 8cf2f99c3..38d5f4942 100644 --- a/spec/mailers/previews/dashboard_mailer_preview.rb +++ b/spec/mailers/previews/dashboard_mailer_preview.rb @@ -23,4 +23,11 @@ class DashboardMailerPreview < ActionMailer::Preview proposal = Proposal.first Dashboard::Mailer.new_actions_notification_on_create(proposal) end + + # http://localhost:3000/rails/mailers/dashboard_mailer/new_actions_notification_on_published + def new_actions_notification_on_published + proposal = Proposal.first + new_actions = Dashboard::Action.all + Dashboard::Mailer.new_actions_notification_on_published(proposal, new_actions) + end end diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index ca78a9d7f..4cac95081 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -1018,4 +1018,32 @@ describe Proposal do end + describe "#send_new_actions_notification_on_published" do + + before do + ActionMailer::Base.deliveries.clear + end + + it "send notification after published when there are new actived actions" do + create(:dashboard_action, :proposed_action, :active, day_offset: 0, published_proposal: true) + create(:dashboard_action, :resource, :active, day_offset: 0, published_proposal: true) + + proposal = create(:proposal, :draft) + proposal.publish + + expect(ActionMailer::Base.deliveries.count).to eq(1) + end + + it "Not send notification after published when there are not new actived actions" do + create(:dashboard_action, :proposed_action, :active, day_offset: 1, published_proposal: true) + create(:dashboard_action, :resource, :active, day_offset: 1, published_proposal: true) + + proposal = create(:proposal, :draft) + proposal.publish + + expect(ActionMailer::Base.deliveries.count).to eq(0) + end + + end + end