feat(dossier): notify user by email about pending corrections

This commit is contained in:
Colin Darie 2023-03-14 18:48:19 +01:00
parent ca3b127942
commit 9565267170
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
9 changed files with 79 additions and 6 deletions

View file

@ -231,7 +231,7 @@ module Instructeurs
elsif !dossier.may_flag_as_pending_correction?
flash.alert = dossier.termine? ? "Impossible de demander de corriger un dossier terminé." : "Le dossier est déjà en attente de correction."
else
commentaire = CommentaireService.create(current_instructeur, dossier, { body: message, piece_jointe: })
commentaire = CommentaireService.build(current_instructeur, dossier, { body: message, piece_jointe: })
dossier.flag_as_pending_correction!(commentaire)
dossier.update!(last_commentaire_updated_at: Time.zone.now)
current_instructeur.follow(dossier)

View file

@ -46,6 +46,19 @@ class DossierMailer < ApplicationMailer
end
end
def notify_pending_correction(dossier)
I18n.with_locale(dossier.user_locale) do
@dossier = dossier
@service = dossier.procedure.service
@logo_url = attach_logo(dossier.procedure)
@subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle)
mail(to: dossier.user_email_for(:notification), subject: @subject) do |format|
format.html { render layout: 'mailers/notifications_layout' }
end
end
end
def notify_new_avis_to_instructeur(avis, instructeur_email)
I18n.with_locale(avis.dossier.user_locale) do
@avis = avis

View file

@ -50,6 +50,10 @@ class NotificationMailer < ApplicationMailer
with(dossier: dossier, state: Dossier.states.fetch(:sans_suite)).send_notification
end
def self.send_pending_correction(dossier)
with(dossier: dossier).send_notification
end
private
def set_services_publics_plus

View file

@ -19,7 +19,7 @@ class Commentaire < ApplicationRecord
belongs_to :instructeur, inverse_of: :commentaires, optional: true
belongs_to :expert, inverse_of: :commentaires, optional: true
has_one :dossier_resolution, inverse_of: :commentaire, dependent: :nullify
has_one :dossier_correction, inverse_of: :commentaire, dependent: :nullify
validate :messagerie_available?, on: :create, unless: -> { dossier.brouillon? }
@ -95,8 +95,8 @@ class Commentaire < ApplicationRecord
update! body: ''
end
def flagged_pending_corrections?
DossierResolution.exists?(commentaire: self)
def flagged_pending_correction?
DossierCorrection.exists?(commentaire: self)
end
private
@ -113,7 +113,11 @@ class Commentaire < ApplicationRecord
end
def notify_user(job_options = {})
DossierMailer.with(commentaire: self).notify_new_answer.deliver_later(job_options)
if flagged_pending_correction?
DossierMailer.notify_pending_correction(dossier).deliver_later(job_options)
else
DossierMailer.with(commentaire: self).notify_new_answer.deliver_later(job_options)
end
end
def messagerie_available?

View file

@ -0,0 +1,13 @@
- content_for :procedure_logo do
= render 'layouts/mailers/logo', url: @logo_url
%p= t(:hello, scope: [:views, :shared, :greetings])
%p= t('.explanation_html', dossier_id: @dossier.id, libelle_demarche: @dossier.procedure.libelle)
%p= t('.link')
= round_button(t('.access_message'), messagerie_dossier_url(@dossier), :primary)
= render 'layouts/mailers/signature', service: @service
- content_for :footer do
= render 'layouts/mailers/service_footer', service: @service, dossier: @dossier

View file

@ -0,0 +1,12 @@
---
en:
dossier_mailer:
notify_pending_correction:
subject: You need to modify your file no. %{dossier_id} « %{libelle_demarche} »
explanation_html:
In order to continue its instruction, <strong>an instructor asks you to edit information</strong> to your file no. %{dossier_id} of the « %{libelle_demarche} » procedure.
link:
Check your file's mailbox to see what changes need to be made, then edit the file directly on the website.
access_message: Open the mailbox

View file

@ -0,0 +1,13 @@
---
fr:
dossier_mailer:
notify_pending_correction:
subject: Vous devez corriger votre dossier nº %{dossier_id} « %{libelle_demarche} »
explanation_html:
Afin de poursuivre son instruction, <strong>un instructeur vous demande dapporter des corrections</strong> à votre dossier nº %{dossier_id} de la démarche « %{libelle_demarche} ».
link:
Consultez la messagerie de votre dossier pour prendre connaissance des modifications à effectuer,
puis modifiez le dossier directement sur le site.
access_message: Ouvrir la messagerie

View file

@ -507,13 +507,23 @@ describe Instructeurs::DossiersController, type: :controller do
}, format: :turbo_stream
end
before { sign_in(instructeur.user) }
before do
sign_in(instructeur.user)
allow(DossierMailer).to receive(:notify_pending_correction)
.and_return(double(deliver_later: nil))
end
context "dossier en instruction" do
let(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure: procedure) }
before { subject }
it 'sends an email to user' do
expect(DossierMailer).to have_received(:notify_pending_correction).once
expect(DossierMailer).to have_received(:notify_pending_correction).with(dossier)
end
it 'pass en_construction and create a pending resolution' do
expect(response).to have_http_status(:ok)
expect(response.body).to include('en attente de modifications')

View file

@ -8,6 +8,10 @@ class DossierMailerPreview < ActionMailer::Preview
DossierMailer.with(commentaire: commentaire(on: draft)).notify_new_answer
end
def notify_pending_correction
DossierMailer.with(dossier: dossier_en_construction).notify_pending_correction
end
def notify_revert_to_instruction
DossierMailer.notify_revert_to_instruction(dossier)
end