diff --git a/app/mailers/dashboard/mailer.rb b/app/mailers/dashboard/mailer.rb index e246fb914..91b0dbd2d 100644 --- a/app/mailers/dashboard/mailer.rb +++ b/app/mailers/dashboard/mailer.rb @@ -5,4 +5,9 @@ class Dashboard::Mailer < ApplicationMailer @proposal = proposal mail to: proposal.author.email, subject: proposal.title end + + def new_actions_notification_on_create(proposal) + @proposal = proposal + mail to: proposal.author.email, subject: I18n.t("mailers.new_actions_notification_on_create.subject") + end end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index e96e49592..00cf8bec9 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -55,6 +55,8 @@ class Proposal < ActiveRecord::Base before_save :calculate_hot_score, :calculate_confidence_score + after_create :send_new_actions_notification_on_create + scope :for_render, -> { includes(:tags) } scope :sort_by_hot_score, -> { reorder(hot_score: :desc) } scope :sort_by_confidence_score, -> { reorder(confidence_score: :desc) } @@ -236,6 +238,14 @@ class Proposal < ActiveRecord::Base Setting["feature.user.skip_verification"].present? end + def send_new_actions_notification_on_create + new_actions = Dashboard::Action.detect_new_actions(self) + + if new_actions.present? + Dashboard::Mailer.new_actions_notification_on_create(self).deliver_later + end + end + protected def set_responsible_name diff --git a/app/views/dashboard/mailer/new_actions_notification_on_create.html.erb b/app/views/dashboard/mailer/new_actions_notification_on_create.html.erb new file mode 100644 index 000000000..b26d8f7f9 --- /dev/null +++ b/app/views/dashboard/mailer/new_actions_notification_on_create.html.erb @@ -0,0 +1,47 @@ + + +

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

+

<%= t("mailers.new_actions_notification_on_create.introduction", title: @proposal.title) %>

+

+ <%= t("mailers.new_actions_notification_on_create.text_1") %> +
+ <%= t("mailers.new_actions_notification_on_create.text_2", link: proposal_dashboard_url(@proposal)).html_safe %> +

+

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

+

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

+ +
+

+ <%= t("mailers.new_actions_notification_on_create.text_5") %> +
+ <%= t("mailers.new_actions_notification_on_create.text_6") %> +

+ + + + + + + +
+ <%= 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_create.dashboard_button") %> + <% end %> +
+ + diff --git a/config/locales/en/mailers.yml b/config/locales/en/mailers.yml index daf1ef259..253d25214 100644 --- a/config/locales/en/mailers.yml +++ b/config/locales/en/mailers.yml @@ -77,3 +77,18 @@ en: hi: "Dear user," thanks: "Thank you again for participating." sincerely: "Sincererly" + new_actions_notification_on_create: + subject: "Your draft citizen proposal in Decide Madrid is created" + hi: "Hi %{name}!" + introduction: "Your %{title} proposal has been successfully created." + text_1: "Take advantage that your proposal is not public yet and get ready to contact a lot of people." + text_2: "When you are ready publish your citizen proposal from this link." + text_3: "We know that creating a proposal with a hook and getting the necessary support can seem complicated. But dont worry because we are going to help you!" + text_4: "You have a tool that will be your new best ally: The Citizen Proposals panel." + list_1: "Enter every day in the panel of your proposal to use the tips and resources that we will share with you." + list_2: "These tips, actions and resources will give you ideas and also practical solutions to get more support and a wider community. Dont forget them!" + list_3: "As you gain more support, you will unlock new and better resources. At the moment, you have an e-mail template to send massively to all your contacts, a poster to print, among other features and rewards that you will discover. Dont stop adding support and we will not stop rewarding and helping you!" + list_4: "You have %{max_month} months since you publish the proposal to get %{max_votes} support and your proposal can become a reality. But the first days are the most important. It is a challenge. Get ready!" + 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!" diff --git a/config/locales/es/mailers.yml b/config/locales/es/mailers.yml index 8f1bd9d2f..ef7dfb7b6 100644 --- a/config/locales/es/mailers.yml +++ b/config/locales/es/mailers.yml @@ -77,3 +77,18 @@ es: hi: "Estimado/a usuario/a" thanks: "Gracias de nuevo por tu participación." sincerely: "Atentamente" + new_actions_notification_on_create: + subject: "Tu borrador de propuesta ciudadana en Decide Madrid está creado" + hi: "Hola %{name}!" + introduction: "Tu propuesta %{title} ha sido creada con éxito." + text_1: "Aprovecha que tu propuesta aún no es pública y prepárate para contactar a mucha gente." + text_2: "Cuando estés preparado/a publica tu propuesta ciudadana desde este enlace." + text_3: "Sabemos que crear una propuesta con gancho y conseguir los apoyos necesarios, puede parecerte complicado. Pero no te preocupes porque ¡vamos a ayudarte!" + text_4: "Tienes una herramienta que será tu nuevo mejor aliado: El panel de Propuestas Ciudadanas." + list_1: "Entra cada día en el panel de tu propuesta para utilizar los consejos y recursos que te iremos compartiendo." + list_2: "Estos consejos, acciones y recursos te darán ideas y también soluciones prácticas para lograr más apoyos y contar con una comunidad más amplia. ¡No te olvides de ellos!" + list_3: "A medida que logres más apoyos, desbloquearás nuevos y mejores recursos. De momento, cuentas con una plantilla de e-mail para enviar masivamente a todos tus contactos, un cartel para imprimir, entre otras funcionalidades y recompensas que irás descubriendo ¡No dejes de sumar apoyos y nosotros no dejaremos de premiarte y ayudarte!" + list_4: "Tienes %{max_month} meses desde que publiques la propuesta para conseguir %{max_votes} apoyos y que tu propuesta pueda hacerse realidad. Pero los primeros días son los más importantes. Es un reto ¡prepárate!" + 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!" diff --git a/spec/mailers/dashboard/mailer_spec.rb b/spec/mailers/dashboard/mailer_spec.rb new file mode 100644 index 000000000..d4d3a660e --- /dev/null +++ b/spec/mailers/dashboard/mailer_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +describe Dashboard::Mailer do + + describe "#new_actions_notification_on_create" do + + before do + ActionMailer::Base.deliveries.clear + end + + let!(:proposal) { build(:proposal, :draft) } + let!(:action) { create(:dashboard_action, :proposed_action, :active, + day_offset: 0, + published_proposal: true) } + let!(:resource) { create(:dashboard_action, :resource, :active, + day_offset: 0, + published_proposal: true) } + + it "sends emails when detect new actions when create a proposal" do + action.update(published_proposal: false) + resource.update(published_proposal: false) + proposal.save + + email = open_last_email + + expect(email).to deliver_from("CONSUL ") + expect(email).to deliver_to(proposal.author) + expect(email).to have_subject("Your draft citizen proposal in Decide Madrid is created") + expect(email).to have_body_text("Hi #{proposal.author.name}!") + expect(email).to have_body_text("Your #{proposal.title} proposal has been successfully created.") + expect(email).to have_body_text("Take advantage that your proposal is not public yet and get ready to contact a lot of people.") + expect(email).to have_body_text(I18n.t("mailers.new_actions_notification_on_create.text_2", link: proposal_dashboard_url(proposal)).html_safe) + expect(email).to have_body_text("We know that creating a proposal with a hook and getting the necessary support can seem complicated. But dont worry because we are going to help you!") + expect(email).to have_body_text("You have a tool that will be your new best ally: The Citizen Proposals panel.") + expect(email).to have_body_text("Enter every day in the panel of your proposal to use the tips and resources that we will share with you.") + expect(email).to have_body_text("These tips, actions and resources will give you ideas and also practical solutions to get more support and a wider community. Dont forget them!") + expect(email).to have_body_text("As you gain more support, you will unlock new and better resources. At the moment, you have an e-mail template to send massively to all your contacts, a poster to print, among other features and rewards that you will discover. Dont stop adding support and we will not stop rewarding and helping you!") + expect(email).to have_body_text("You have #{Setting['months_to_archive_proposals']} months since you publish the proposal to get #{Setting['votes_for_proposal_success']} support and your proposal can become a reality. But the first days are the most important. It is a challenge. Get ready!") + expect(email).to have_body_text("And for you to start with all the motivation,") + expect(email).to have_body_text("here you have several resources and a whole list of tips that will come to you every day to prepare the broadcast!") + 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 eded26aef..5e6ce05f4 100644 --- a/spec/mailers/previews/dashboard_mailer_preview.rb +++ b/spec/mailers/previews/dashboard_mailer_preview.rb @@ -3,4 +3,10 @@ class DashboardMailerPreview < ActionMailer::Preview proposal = Proposal.first Dashboard::Mailer.forward(proposal) end + + # http://localhost:3000/rails/mailers/dashboard_mailer/new_actions_notification_on_create + def new_actions_notification_on_create + proposal = Proposal.first + Dashboard::Mailer.new_actions_notification_on_create(proposal) + end end diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index fa4226764..ca78a9d7f 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -992,4 +992,30 @@ describe Proposal do end + describe "#send_new_actions_notification_on_create" do + + before do + ActionMailer::Base.deliveries.clear + end + + it "send notification after create when there are new actived actions" do + create(:dashboard_action, :proposed_action, :active, day_offset: 0, published_proposal: false) + create(:dashboard_action, :resource, :active, day_offset: 0, published_proposal: false) + + create(:proposal, :draft) + + expect(ActionMailer::Base.deliveries.count).to eq(1) + end + + it "Not send notification after create when there are not new actived actions" do + create(:dashboard_action, :proposed_action, :active, day_offset: 1, published_proposal: false) + create(:dashboard_action, :resource, :active, day_offset: 1, published_proposal: false) + + create(:proposal, :draft) + + expect(ActionMailer::Base.deliveries.count).to eq(0) + end + + end + end