Skip to content

Commit

Permalink
Merge pull request #463 from IgorMonardez/issue_405
Browse files Browse the repository at this point in the history
Issue 405
  • Loading branch information
JoaoFelipe authored May 27, 2024
2 parents 3098699 + 8ca6b7e commit 9cf2ae2
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/mailers/devise_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def headers_for(action, opts)
headers[:body] = render_to_string(
inline: @template.body
) unless @template.body.nil?
headers[:reply_to] = render_to_string(
inline: CustomVariable.reply_to
)
@template.update_mailer_headers(headers)
headers
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/custom_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
class CustomVariable < ApplicationRecord
has_paper_trail

SAPOS_MAIL = "[email protected]"

VARIABLES = {
"single_advisor_points" => :text,
"multiple_advisor_points" => :text,
"program_level" => :text,
"identity_issuing_country" => :text,
"class_schedule_text" => :text,
"redirect_email" => :text,
"reply_to" => :text,
"notification_footer" => :text,
"minimum_grade_for_approval" => :text,
"grade_of_disapproval_for_absence" => :text,
Expand Down Expand Up @@ -59,6 +62,12 @@ def self.redirect_email
config.blank? ? nil : (config.value || "")
end

def self.reply_to
default = ActionMailer::Base.default[:from]
config = CustomVariable.find_by_variable(:reply_to)
config.blank? ? default : (config.value || default)
end

def self.notification_footer
config = CustomVariable.find_by_variable(:notification_footer)
config.blank? ? "" : config.value
Expand Down
1 change: 1 addition & 0 deletions app/models/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def update_mailer_headers(headers)
headers[:to] = CustomVariable.redirect_email
headers[:skip_redirect] = true
end
headers[:reply_to] = CustomVariable.reply_to
headers[:skip_message] = ! self.enabled
headers[:skip_footer] = true
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddReplyToToNotificationLogs < ActiveRecord::Migration[7.0]
def up
add_column :notification_logs, :reply_to, :string, limit: 255
end
def down
remove_column :notification_logs, :reply_to
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{ description: "País padrão de emissão da identidade", variable: "identity_issuing_country", value: "Brasil" },
{ description: "Texto no final do quadro de horários", variable: "class_schedule_text", value: "Alunos interessados em cursar disciplinas de Tópicos Avançados devem consultar os respectivos professores antes da matrícula." },
{ description: "E-mail de redirecionamento para as notificações", variable: "redirect_email", value: "" },
{ description: "E-mail das resposta de emails automáticos", variable: "reply_to", value: "[email protected]"},
{ description: "Texto de rodapé da notificação",
variable: "notification_footer",
value: <<~TEXT
Expand Down
12 changes: 9 additions & 3 deletions lib/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ def self.send_emails(notifications)
m = message.merge(options)
next if m[:skip_message]
m_attachments = messages_attachments[message]
if ! CustomVariable.notification_footer.empty? && ! m[:skip_footer]
if !CustomVariable.notification_footer.empty? && ! m[:skip_footer]
m[:body] += "\n\n\n" + CustomVariable.notification_footer
end
if ! CustomVariable.redirect_email.nil? && ! m[:skip_redirect]
if !CustomVariable.redirect_email.nil? && ! m[:skip_redirect]
Notifier.logger.info "Custom Variable 'redirect_email' is set. Redirecting the emails"
m[:body] = "Originalmente para #{m[:to]}\n\n" + m[:body]
m[:to] = CustomVariable.redirect_email
end
unless CustomVariable.reply_to.nil?
Notifier.logger.info "Custom Variable 'reply_to' is set. Forwarding email"
m[:reply_to] = CustomVariable.reply_to
end
unless m[:to].blank?
actionmailer_base = ActionMailer::Base.new

Expand All @@ -65,6 +69,7 @@ def self.send_emails(notifications)
to: m[:to],
subject: m[:subject],
body: m[:body],
reply_to: m[:reply_to],
attachments_file_names: attachments_file_name_list
).save
Notifier.display_notification_info(m, attachments_file_name_list)
Expand All @@ -75,7 +80,8 @@ def self.send_emails(notifications)
def self.display_notification_info(notification, attachments_file_name_list)
Notifier.logger.info "\n#{Time.now.strftime("%Y/%m/%d %H:%M:%S")}"
Notifier.logger.info "########## Notification ##########"
Notifier.logger.info "Notifying #{notification[:to]}"
Notifier.logger.info "Notifying: #{notification[:to]}"
Notifier.logger.info "Replying: #{notification[:reply_to]}"
Notifier.logger.info "Subject: #{notification[:subject]}"
Notifier.logger.info "body: #{notification[:body]}"
if attachments_file_name_list
Expand Down
26 changes: 26 additions & 0 deletions spec/models/custom_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
variable: "single_advisor_points"
)
end
let(:default_from) { ActionMailer::Base.default[:from]}
subject { custom_variable }
describe "Validations" do
it { should be_valid }
Expand Down Expand Up @@ -136,6 +137,31 @@
end
end

context "reply_to" do
it "should return default value when there is no variable defined" do
config = CustomVariable.find_by_variable(:reply_to)
config.delete unless config.nil?

expect(CustomVariable.reply_to).to eq(default_from)
end

it "should return default value when the value is nil" do
config = CustomVariable.find_by_variable(:reply_to)
config.delete unless config.nil?
@destroy_later << CustomVariable.create(variable: :reply_to, value: nil)

expect(CustomVariable.reply_to).to eq(default_from)
end

it "should return value when the values is defined" do
config = CustomVariable.find_by_variable(:reply_to)
config.delete unless config.nil?
@destroy_later << CustomVariable.create(variable: :reply_to, value: "[email protected]")

expect(CustomVariable.reply_to).to eq("[email protected]")
end
end

context "notification_footer" do
it "should return '' when there is no variable defined" do
config = CustomVariable.find_by_variable(:notification_footer)
Expand Down
33 changes: 31 additions & 2 deletions spec/models/email_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
subject: "Title",
)
end
let(:default_from) { ActionMailer::Base.default[:from] }
subject { email_template }
describe "Validations" do
it { should be_valid }
Expand Down Expand Up @@ -93,7 +94,7 @@
}
template.update_mailer_headers(headers)
expect(headers).to eq({
to: "a", subject: "b", body: "c",
to: "a", subject: "b", body: "c", reply_to: default_from,
skip_message: true, skip_footer: true
})
end
Expand All @@ -106,11 +107,38 @@
}
template.update_mailer_headers(headers)
expect(headers).to eq({
to: "[email protected]", subject: "b (Originalmente para a)", body: "c",
to: "[email protected]", subject: "b (Originalmente para a)", body: "c", reply_to: default_from,
skip_message: false, skip_footer: true, skip_redirect: true
})
CustomVariable.delete_all
end
it "should have 'reply_to' equal to variable reply_to attribute when variable is set" do
FactoryBot.create(:custom_variable, variable: "reply_to", value: "[email protected]")
template = EmailTemplate.load_template("accomplishments:email_to_advisor")

headers = {
to: "a", subject: "b", body: "c"
}
template.update_mailer_headers(headers)
expect(headers).to eq({
reply_to: "[email protected]", subject: "b", body: "c", to: "a",
skip_message: false, skip_footer: true
})
CustomVariable.delete_all
end
it "should have 'reply_to' equal to default[:from] when variable isnt set" do
template = EmailTemplate.load_template("accomplishments:email_to_advisor")

headers = {
to: "a", subject: "b", body: "c"
}
template.update_mailer_headers(headers)
expect(headers).to eq({
reply_to: default_from, subject: "b", body: "c", to: "a",
skip_message: false, skip_footer: true
})
CustomVariable.delete_all
end
end
describe "prepare_message" do
it "should use the ERB formating in all fields" do
Expand All @@ -122,6 +150,7 @@
to: "t1",
subject: "s1",
body: "b1",
reply_to: default_from,
skip_footer: true,
skip_message: true,
})
Expand Down

0 comments on commit 9cf2ae2

Please sign in to comment.