diff --git a/app/mailers/devise_mailer.rb b/app/mailers/devise_mailer.rb index a8d697a8..8376fabe 100644 --- a/app/mailers/devise_mailer.rb +++ b/app/mailers/devise_mailer.rb @@ -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 diff --git a/app/models/custom_variable.rb b/app/models/custom_variable.rb index ce95da97..4ed0fee1 100644 --- a/app/models/custom_variable.rb +++ b/app/models/custom_variable.rb @@ -7,6 +7,8 @@ class CustomVariable < ApplicationRecord has_paper_trail + SAPOS_MAIL = "sapos@sapos.ic.uff.br" + VARIABLES = { "single_advisor_points" => :text, "multiple_advisor_points" => :text, @@ -14,6 +16,7 @@ class CustomVariable < ApplicationRecord "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, @@ -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 diff --git a/app/models/email_template.rb b/app/models/email_template.rb index dd9244f2..56921ea4 100644 --- a/app/models/email_template.rb +++ b/app/models/email_template.rb @@ -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 diff --git a/db/migrate/20240517150433_add_reply_to_to_notification_logs.rb b/db/migrate/20240517150433_add_reply_to_to_notification_logs.rb new file mode 100644 index 00000000..69d41599 --- /dev/null +++ b/db/migrate/20240517150433_add_reply_to_to_notification_logs.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 5fc78926..8e251eb9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_01_18_203814) do +ActiveRecord::Schema[7.0].define(version: 2024_05_22_181457) do create_table "accomplishments", force: :cascade do |t| t.integer "enrollment_id" t.integer "phase_id" @@ -619,6 +619,7 @@ t.string "subject", limit: 255 t.text "body" t.string "attachments_file_names" + t.string "reply_to", limit: 255 t.index ["notification_id"], name: "index_notification_logs_on_notification_id" end diff --git a/db/seeds.rb b/db/seeds.rb index 8d31fed0..a96c2d47 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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: "sapos@sapos.ic.uff.br"}, { description: "Texto de rodapé da notificação", variable: "notification_footer", value: <<~TEXT diff --git a/lib/notifier.rb b/lib/notifier.rb index a38bf072..1e33560c 100644 --- a/lib/notifier.rb +++ b/lib/notifier.rb @@ -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 @@ -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) @@ -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 diff --git a/spec/models/custom_variable_spec.rb b/spec/models/custom_variable_spec.rb index bbfb18c5..a954cb5a 100644 --- a/spec/models/custom_variable_spec.rb +++ b/spec/models/custom_variable_spec.rb @@ -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 } @@ -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@email.com.br") + + expect(CustomVariable.reply_to).to eq("email@email.com.br") + end + end + context "notification_footer" do it "should return '' when there is no variable defined" do config = CustomVariable.find_by_variable(:notification_footer) diff --git a/spec/models/email_template_spec.rb b/spec/models/email_template_spec.rb index 807d16be..e955f942 100644 --- a/spec/models/email_template_spec.rb +++ b/spec/models/email_template_spec.rb @@ -16,6 +16,7 @@ subject: "Title", ) end + let(:default_from) { ActionMailer::Base.default[:from] } subject { email_template } describe "Validations" do it { should be_valid } @@ -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 @@ -106,11 +107,38 @@ } template.update_mailer_headers(headers) expect(headers).to eq({ - to: "email@email.com", subject: "b (Originalmente para a)", body: "c", + to: "email@email.com", 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@email.com") + 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@email.com", 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 @@ -122,6 +150,7 @@ to: "t1", subject: "s1", body: "b1", + reply_to: default_from, skip_footer: true, skip_message: true, })