Skip to content

Commit

Permalink
ajout du mail de réponse no_reply
Browse files Browse the repository at this point in the history
  • Loading branch information
adipasquale committed Dec 16, 2024
1 parent 3f9a4e5 commit e5a7b9f
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 8 deletions.
8 changes: 7 additions & 1 deletion app/jobs/handle_inbound_email_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def self.uuid_from_email_address(email_address)
def perform(sendinblue_hash)
@sendinblue_hash = sendinblue_hash.with_indifferent_access

if rdv&.agents&.pluck(:email)&.compact&.any?
if source_mail.to.first.match(/ne-pas-repondre@reply\.[a-z\-\.]+$/)
reply_no_reply
elsif rdv&.agents&.pluck(:email)&.compact&.any?
notify_agents
else
forward_to_default_mailbox
Expand All @@ -30,6 +32,10 @@ def perform(sendinblue_hash)

private

def reply_no_reply
Users::NoReplyMailer.with(source_mail:).no_reply.deliver_now
end

def notify_agents
Agents::ReplyTransferMailer.notify_agent_of_user_reply(
rdv: rdv,
Expand Down
7 changes: 7 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ def rfc5322_name_and_email(name, email)
def default_from
domain.support_email
end

def no_reply_from
rfc5322_name_and_email(
"Ne pas répondre - #{domain.name}",
"ne-pas-repondre@#{domain.reply_host_name}"
)
end
end
7 changes: 1 addition & 6 deletions app/mailers/custom_devise_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ def invitation_instructions(record, token, opts = {})
private

def set_no_reply_for_users
if @resource.is_a?(User)
mail.from = rfc5322_name_and_email(
"Ne pas répondre - #{domain.name}",
"ne-pas-repondre@#{domain.reply_host_name}"
)
end
mail.from = no_reply_from if @resource.is_a?(User)
end

def domain
Expand Down
30 changes: 30 additions & 0 deletions app/mailers/users/no_reply_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Users::NoReplyMailer < ApplicationMailer
class UnrecognizableEmailAddressError < StandardError; end

# @param [Mail::Message] source_mail : le mail initial envoyé par l’usager et transféré par Brevo à notre serveur
def no_reply
mail(
from: no_reply_from,
to: params[:source_mail].from.first,
subject: "Mail non reçu"
)
end

private

def domain
@domain ||= begin
reply_email_domain = params[:source_mail].to.first.split("@").last
case reply_email_domain
when /rdv-solidarites/
Domain::RDV_SOLIDARITES
when /rdv-service-public/ || /rdv\.anct\.gouv/ || /rdv-mairie/
Domain::RDV_MAIRIE
when /rdv-aide-numerique/
Domain::RDV_AIDE_NUMERIQUE
else
raise UnrecognizableEmailAddressError, params[:source_mail].to.to_s
end
end
end
end
10 changes: 10 additions & 0 deletions app/views/mailers/users/no_reply_mailer/no_reply.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
div
p
' Votre réponse email n’a pas pu être délivrée.
| Cette adresse n’est pas destinée à recevoir des réponses.
p
' Si vous souhaitez annuler ou décaler un RDV, vous pouvez cliquer sur les liens reçus dans les précédents emails ou bien
= link_to "retrouver tous vos RDV en vous connectant ici", new_user_session_url(host: domain.host_name)
'.
.btn-wrapper
= link_to("J’ai besoin d’aide", contact_url(host: domain.host_name), class: "btn btn-primary")
34 changes: 33 additions & 1 deletion spec/jobs/handle_inbound_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end
end

describe "#perform" do
describe "#perform - pour des réponses à des mails transactionnels de RDV" do
subject(:perform_job) { described_class.perform_now(sendinblue_payload) }

before do
Expand Down Expand Up @@ -158,4 +158,36 @@
end
end
end

describe "#perform - pour des réponses au emails ne-pas-repondre" do
subject(:perform_job) { described_class.perform_now(sendinblue_payload) }

let(:sendinblue_payload) do
{
Cc: [],
ReplyTo: nil,
Subject: "",
Attachments: [],
Headers: {
"Message-ID": "<[email protected]>",
Subject: "",
From: "Bénédicte Ficiaire <[email protected]>",
To: "[email protected]",
Date: "Thu, 12 May 2022 12:22:15 +0200",
},
ExtractedMarkdownMessage: "Je souhaite annuler mon RDV",
ExtractedMarkdownSignature: nil,
RawHtmlBody: %(<html dir="ltr"><head></head><body style="text-align:left; direction:ltr;"><div>Je souhaite annuler mon RDV</div>\n</body></html>\n),
RawTextBody: "Je souhaite annuler mon RDV\n",
}
end

it "répond automatiquement à l’usager" do
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1)
transferred_email = ActionMailer::Base.deliveries.last
expect(transferred_email.to).to eq(["[email protected]"])
expect(transferred_email.from).to eq(["[email protected]"])
expect(transferred_email.html_part.body.to_s).to include(%(Votre réponse email n’a pas pu être délivrée))
end
end
end
10 changes: 10 additions & 0 deletions spec/mailers/previews/users/no_reply_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Users::NoReplyMailerPreview < ActionMailer::Preview
def no_reply
source_mail = Mail.new do
from "[email protected]"
to "[email protected]"
end

Users::NoReplyMailer.with(source_mail:).no_reply
end
end
46 changes: 46 additions & 0 deletions spec/mailers/users/no_reply_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
RSpec.describe Users::NoReplyMailer, type: :mailer do
describe "#no_reply" do
context "l’usager a répondu à l’adresse no-reply du même environnement" do
let(:source_mail) do
Mail.new do
from "[email protected]"
to "[email protected]"
end
end

it "répond depuis la même adresse no-reply" do
mail = described_class.with(source_mail:).no_reply
expect(mail[:from].to_s).to eq(%("Ne pas répondre - RDV Solidarités" <[email protected]>))
expect(mail.to).to eq(["[email protected]"])
end
end

context "l’usager a répondu à une adresse no-reply d’un autre environnement" do
let(:source_mail) do
Mail.new do
from "[email protected]"
to "[email protected]"
end
end

it "répond depuis l’adresse no-reply de l’environnement courant" do
mail = described_class.with(source_mail:).no_reply
expect(mail[:from].to_s).to eq(%("Ne pas répondre - RDV Solidarités" <[email protected]>))
expect(mail.to).to eq(["[email protected]"])
end
end

context "l’adresse n’est pas reconnaissable" do
let(:source_mail) do
Mail.new do
from "[email protected]"
to "[email protected]"
end
end

it "répond depuis l’adresse no-reply de l’environnement courant" do
expect { described_class.with(source_mail:).no_reply.deliver_now }.to raise_exception Users::NoReplyMailer::UnrecognizableEmailAddressError
end
end
end
end

0 comments on commit e5a7b9f

Please sign in to comment.