Create new email template for new evaluation comment created and add it to system_emails view

This commit is contained in:
lalo
2019-03-27 12:13:43 +01:00
parent a6f8969728
commit 28815cb290
14 changed files with 248 additions and 8 deletions

View File

@@ -14,7 +14,8 @@ class Admin::SystemEmailsController < Admin::BaseController
direct_message_for_receiver: %w[view edit_info], direct_message_for_receiver: %w[view edit_info],
direct_message_for_sender: %w[view edit_info], direct_message_for_sender: %w[view edit_info],
email_verification: %w[view edit_info], email_verification: %w[view edit_info],
user_invite: %w[view edit_info] user_invite: %w[view edit_info],
evaluation_comment: %w[view edit_info]
} }
end end
@@ -34,6 +35,8 @@ class Admin::SystemEmailsController < Admin::BaseController
load_sample_user load_sample_user
when "user_invite" when "user_invite"
@subject = t("mailers.user_invite.subject", org_name: Setting["org_name"]) @subject = t("mailers.user_invite.subject", org_name: Setting["org_name"])
when "evaluation_comment"
load_sample_valuation_comment
end end
end end
@@ -97,6 +100,17 @@ class Admin::SystemEmailsController < Admin::BaseController
end end
end end
def load_sample_valuation_comment
comment = Comment.where(commentable_type: "Budget::Investment").last
if comment
@email = EvaluationCommentEmail.new(comment)
@email_to = @email.to.first
else
redirect_to admin_system_emails_path,
alert: t("admin.system_emails.alert.no_evaluation_comments")
end
end
def load_sample_user def load_sample_user
@user = User.last @user = User.last
@token = @user.email_verification_token || SecureRandom.hex @token = @user.email_verification_token || SecureRandom.hex

View File

@@ -8,4 +8,16 @@ module MailerHelper
return budget_investment_url(commentable.budget_id, commentable) if commentable.is_a?(Budget::Investment) return budget_investment_url(commentable.budget_id, commentable) if commentable.is_a?(Budget::Investment)
end end
def valuation_comments_url(commentable)
admin_budget_budget_investment_url( commentable.budget, commentable, anchor: "comments")
end
def valuation_comments_link(commentable)
link_to(
commentable.title,
valuation_comments_url(@email.commentable),
target: :blank,
style: "color: #2895F1; text-decoration:none;"
)
end
end end

View File

@@ -120,6 +120,13 @@ class Mailer < ApplicationMailer
mail(to: @email_to, from: @newsletter.from, subject: @newsletter.subject) mail(to: @email_to, from: @newsletter.from, subject: @newsletter.subject)
end end
def evaluation_comment(comment, to)
@email = EvaluationCommentEmail.new(comment)
@email_to = to
mail(to: @email_to.email, subject: @email.subject) if @email.can_be_sent?
end
private private
def with_user(user, &block) def with_user(user, &block)

View File

@@ -377,6 +377,12 @@ class Budget
milestones.published.with_status.order_by_publication_date.last&.status_id milestones.published.with_status.order_by_publication_date.last&.status_id
end end
def admin_and_valuator_users_associated
valuator_users = (valuator_groups.map(&:valuators) + valuators).flatten
all_users = valuator_users << administrator
all_users.compact.uniq
end
private private
def set_denormalized_ids def set_denormalized_ids

View File

@@ -0,0 +1,19 @@
<td style="padding-bottom: 20px; padding-left: 10px;">
<h1 style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;">
<%= t("mailers.evaluation_comment.title", investment: @email.commentable.title) %>
</h1>
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t("mailers.evaluation_comment.hi") %> <strong><%= @email_to.name %></strong>,
</p>
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t("mailers.evaluation_comment.new_comment_by_html", commenter: @email.comment.author.name, investment: valuation_comments_link(@email.commentable)) %>
</p>
<%= t("mailers.evaluation_comment.commenter_info", commenter: @email.comment.author.name, time: l(@email.comment.created_at)) %>
<div style="border-left: 2px solid #DEE0E3;font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-style: italic;font-weight: normal;line-height: 24px;margin-left: 20px;padding: 10px;">
<%= simple_format text_with_links(@email.comment.body), {}, sanitize: false %>
</div>
</td>

View File

@@ -875,6 +875,9 @@ en:
user_invite: user_invite:
title: "User Invitation" title: "User Invitation"
description: "Sent to the person that has been invited to register an account." description: "Sent to the person that has been invited to register an account."
evaluation_comment:
title: "New evaluation comment"
description: "Sent to administrators and evaluators related to commented investment"
edit_info: "You can edit this email in" edit_info: "You can edit this email in"
message_title: "Message's Title" message_title: "Message's Title"
message_body: "This is a sample of message's content." message_body: "This is a sample of message's content."
@@ -882,6 +885,7 @@ en:
no_investments: "There aren't any budget investment created. Some example data is needed in order to preview the email." no_investments: "There aren't any budget investment created. Some example data is needed in order to preview the email."
no_comments: "There aren't any comments created. Some example data is needed in order to preview the email." no_comments: "There aren't any comments created. Some example data is needed in order to preview the email."
no_replies: "There aren't any replies created. Some example data is needed in order to preview the email." no_replies: "There aren't any replies created. Some example data is needed in order to preview the email."
no_evaluation_comments: "There aren't any evaluation comments created. Some example data is needed in order to preview the email."
emails_download: emails_download:
index: index:
title: Emails download title: Emails download

View File

@@ -70,6 +70,12 @@ en:
hi: "Dear user," hi: "Dear user,"
thanks: "Thank you again for participating." thanks: "Thank you again for participating."
sincerely: "Sincererly" sincerely: "Sincererly"
evaluation_comment:
subject: "New evaluation comment"
title: New evaluation comment for %{investment}
hi: Hi
new_comment_by_html: There is a new evaluation comment from <b>%{commenter}</b> to the budget investment %{investment}
commenter_info: "%{commenter}, %{time}:"
new_actions_notification_rake_created: new_actions_notification_rake_created:
subject: "More news about your citizen proposal" subject: "More news about your citizen proposal"
hi: "Hello %{name}," hi: "Hello %{name},"

View File

@@ -874,6 +874,9 @@ es:
user_invite: user_invite:
title: "Invitación de usuarios" title: "Invitación de usuarios"
description: "Enviado a la persona que ha sido invitada a registrar una cuenta." description: "Enviado a la persona que ha sido invitada a registrar una cuenta."
evaluation_comment:
title: "Nuevo comentario de evaluación"
description: "Enviado a administradores y evaluadores del presupuesto."
edit_info: "Puedes editar este email en" edit_info: "Puedes editar este email en"
message_title: "Título del mensaje" message_title: "Título del mensaje"
message_body: "Este es un ejemplo de contenido de un mensaje." message_body: "Este es un ejemplo de contenido de un mensaje."
@@ -881,6 +884,7 @@ es:
no_investments: "No se ha creado ningún proyecto de gasto. Se necesita algún ejemplo para poder previsualizar el email." no_investments: "No se ha creado ningún proyecto de gasto. Se necesita algún ejemplo para poder previsualizar el email."
no_comments: "No se ha creado ningún comentario. Se necesita algún ejemplo para poder previsualizar el email." no_comments: "No se ha creado ningún comentario. Se necesita algún ejemplo para poder previsualizar el email."
no_replies: "No se ha creado ninguna respuesta. Se necesita algún ejemplo para poder previsualizar el email." no_replies: "No se ha creado ninguna respuesta. Se necesita algún ejemplo para poder previsualizar el email."
no_evaluation_comments: "No se ha creado ningún comentario de evaluación. Se necesita algún ejemplo para poder previsualizar el email."
emails_download: emails_download:
index: index:
title: Descarga de emails title: Descarga de emails

View File

@@ -70,6 +70,12 @@ es:
hi: "Estimado/a usuario/a" hi: "Estimado/a usuario/a"
thanks: "Gracias de nuevo por tu participación." thanks: "Gracias de nuevo por tu participación."
sincerely: "Atentamente" sincerely: "Atentamente"
evaluation_comment:
subject: "Nuevo comentario de evaluación"
title: Nuevo comentario de evaluación para %{investment}
hi: Hola
new_comment_by_html: Hay un nuevo comentario de evaluación de <b>%{commenter}</b> en el presupuesto participativo %{investment}
commenter_info: "%{commenter}, %{time}"
new_actions_notification_rake_created: new_actions_notification_rake_created:
subject: "Más novedades de tu propuesta ciudadana" subject: "Más novedades de tu propuesta ciudadana"
hi: "Hola %{name}," hi: "Hola %{name},"

View File

@@ -0,0 +1,31 @@
class EvaluationCommentEmail
attr_reader :comment
def initialize(comment)
@comment = comment
end
def commentable
comment.commentable
end
def to
@to ||= related_users
end
def subject
I18n.t("mailers.evaluation_comment.subject")
end
def can_be_sent?
commentable.present? && to.any?
end
private
def related_users
return [] if comment.commentable.nil?
comment.commentable
.admin_and_valuator_users_associated
.reject { |associated_user| associated_user.user == comment.author }
end
end

View File

@@ -14,7 +14,8 @@ describe "System Emails" do
let(:system_emails) do let(:system_emails) do
%w[proposal_notification_digest budget_investment_created budget_investment_selected %w[proposal_notification_digest budget_investment_created budget_investment_selected
budget_investment_unfeasible budget_investment_unselected comment reply budget_investment_unfeasible budget_investment_unselected comment reply
direct_message_for_receiver direct_message_for_sender email_verification user_invite] direct_message_for_receiver direct_message_for_sender email_verification user_invite
evaluation_comment]
end end
context "System emails" do context "System emails" do
@@ -243,6 +244,31 @@ describe "System Emails" do
visit admin_system_email_view_path("reply") visit admin_system_email_view_path("reply")
expect(page).to have_content "There aren't any replies created." expect(page).to have_content "There aren't any replies created."
expect(page).to have_content "Some example data is needed in order to preview the email." expect(page).to have_content "Some example data is needed in order to preview the email."
visit admin_system_email_view_path("evaluation_comment")
expect(page).to have_content "There aren't any evaluation comments created."
expect(page).to have_content "Some example data is needed in order to preview the email."
end
scenario "#evaluation_comment" do
admin = create(:administrator, user: create(:user, username: "Baby Doe"))
investment = create(:budget_investment,
title: "Cleaner city",
heading: heading,
author: user,
administrator: admin)
comment = create(:comment, :valuation, commentable: investment)
visit admin_system_email_view_path("evaluation_comment")
expect(page).to have_content "New evaluation comment for Cleaner city"
expect(page).to have_content "Hi #{admin.name}"
expect(page).to have_content "There is a new evaluation comment from #{comment.user.name} "\
"to the budget investment Cleaner city"
expect(page).to have_content comment.body
expect(page).to have_link "Cleaner city",
href: admin_budget_budget_investment_url( investment.budget, investment, anchor: "comments")
end end
end end

View File

@@ -171,7 +171,7 @@ describe "Internal valuation comments on Budget::Investments" do
scenario "Create comment", :js do scenario "Create comment", :js do
visit valuation_budget_budget_investment_path(budget, investment) visit valuation_budget_budget_investment_path(budget, investment)
fill_in "comment-body-budget_investment_#{investment.id}", with: "Have you thought about...?" fill_in "Leave your comment", with: "Have you thought about...?"
click_button "Publish comment" click_button "Publish comment"
within "#comments" do within "#comments" do
@@ -199,7 +199,7 @@ describe "Internal valuation comments on Budget::Investments" do
click_link "Reply" click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment-body-comment_#{comment.id}", with: "It will be done next week." fill_in "Leave your comment", with: "It will be done next week."
click_button "Publish reply" click_button "Publish reply"
end end
@@ -261,8 +261,8 @@ describe "Internal valuation comments on Budget::Investments" do
login_as(admin_user) login_as(admin_user)
visit valuation_budget_budget_investment_path(budget, investment) visit valuation_budget_budget_investment_path(budget, investment)
fill_in "comment-body-budget_investment_#{investment.id}", with: "I am your Admin!" fill_in "Leave your comment", with: "I am your Admin!"
check "comment-as-administrator-budget_investment_#{investment.id}" check "Comment as admin"
click_button "Publish comment" click_button "Publish comment"
within "#comments" do within "#comments" do
@@ -282,8 +282,8 @@ describe "Internal valuation comments on Budget::Investments" do
click_link "Reply" click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment-body-comment_#{comment.id}", with: "Top of the world!" fill_in "Leave your comment", with: "Top of the world!"
check "comment-as-administrator-comment_#{comment.id}" check "Comment as admin"
click_button "Publish reply" click_button "Publish reply"
end end
@@ -298,4 +298,25 @@ describe "Internal valuation comments on Budget::Investments" do
end end
end end
scenario "Send email notification", :js do
ActionMailer::Base.deliveries = []
login_as(admin_user)
expect(ActionMailer::Base.deliveries).to eq([])
visit valuation_budget_budget_investment_path(budget, investment)
fill_in "Leave your comment", with: "I am your Admin!"
check "Comment as admin"
click_button "Publish comment"
within "#comments" do
expect(page).to have_content("I am your Admin!")
end
expect(ActionMailer::Base.deliveries.count).to eq(1)
expect(ActionMailer::Base.deliveries.first.to).to eq([valuator_user.email])
expect(ActionMailer::Base.deliveries.first.subject).to eq("New evaluation comment")
end
end end

View File

@@ -0,0 +1,57 @@
require "rails_helper"
describe EvaluationCommentEmail do
let(:author) { create(:user) }
let(:administrator) { create(:administrator)}
let(:investment) { create(:budget_investment, author: author, administrator: administrator) }
let(:commenter) { create(:user, email: "email@commenter.org") }
let(:comment) { create(:comment, commentable: investment, user: commenter) }
let(:comment_email) { EvaluationCommentEmail.new(comment) }
describe "#commentable" do
it "returns the commentable object that contains the replied comment" do
expect(comment_email.commentable).to eq investment
end
end
describe "#to" do
it "returns an array of users related to investment" do
expect(comment_email.to).to eq [administrator]
end
it "returns empty array if commentable not exists" do
allow(comment).to receive(:commentable).and_return(nil)
expect(comment_email.to).to eq []
end
it "returns empty array if not associated users" do
allow(investment).to receive(:admin_and_valuator_users_associated).and_return([])
expect(comment_email.to).to eq []
end
end
describe "#subject" do
it "returns the translation for a evaluation comment email subject" do
expect(comment_email.subject).to eq "New evaluation comment"
end
end
describe "#can_be_sent?" do
it "returns true if investment has any associated users" do
expect(comment_email.can_be_sent?).to be true
end
it "returns false if the comment doesn't exist" do
comment.update(commentable: nil)
expect(comment_email.can_be_sent?).to be false
end
it "returns false if recipients are empty" do
investment.administrator = nil
expect(comment_email.can_be_sent?).to be false
end
end
end

View File

@@ -1218,4 +1218,31 @@ describe Budget::Investment do
end end
end end
end end
describe "admin_and_valuator_users_associated" do
let(:investment) { create(:budget_investment) }
let(:valuator_group) { create(:valuator_group) }
let(:valuator) { create(:valuator) }
let(:administrator) { create(:administrator) }
it "returns empty array if not valuators or administrator assigned" do
expect(investment.admin_and_valuator_users_associated).to eq([])
end
it "returns all valuator and administrator users" do
valuator_group.valuators << valuator
investment.valuator_groups << valuator_group
expect(investment.admin_and_valuator_users_associated).to eq([valuator])
investment.administrator = administrator
expect(investment.admin_and_valuator_users_associated).to eq([valuator, administrator])
end
it "returns uniq valuators or administrator users" do
valuator_group.valuators << valuator
investment.valuator_groups << valuator_group
investment.valuators << valuator
investment.administrator = administrator
expect(investment.admin_and_valuator_users_associated).to eq([valuator, administrator])
end
end
end end