amelioration(bulk_messages): ETQ instructeur, je peux envoyer un message aux usagers ayant un dossier en brouillon

This commit is contained in:
Martin 2023-07-13 17:58:11 +02:00 committed by mfo
parent 764f19d11a
commit 425d8867be
12 changed files with 145 additions and 111 deletions

View file

@ -224,19 +224,18 @@ module Instructeurs
def email_usagers
@procedure = procedure
@commentaire = Commentaire.new
@email_usagers_dossiers = email_usagers_dossiers
@dossiers_count = @email_usagers_dossiers.count
@groupe_instructeurs = email_usagers_groupe_instructeurs_label
@bulk_messages = BulkMessage.includes(:groupe_instructeurs).where(groupe_instructeurs: { id: current_instructeur.groupe_instructeur_ids, procedure: procedure })
@bulk_messages = BulkMessage.includes(:groupe_instructeurs).where(groupe_instructeurs: { procedure: procedure })
@bulk_message = current_instructeur.bulk_messages.build
@dossiers_without_groupe_count = procedure.dossiers.state_brouillon.for_groupe_instructeur(nil).count
end
def create_multiple_commentaire
@procedure = procedure
errors = []
email_usagers_dossiers.each do |dossier|
commentaire = CommentaireService.create(current_instructeur, dossier, commentaire_params)
bulk_message = current_instructeur.bulk_messages.build(bulk_message_params)
dossiers = procedure.dossiers.state_brouillon.for_groupe_instructeur(nil)
dossiers.each do |dossier|
commentaire = CommentaireService.create(current_instructeur, dossier, bulk_message_params.except(:targets))
if commentaire.errors.empty?
commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
else
@ -244,8 +243,15 @@ module Instructeurs
end
end
valid_dossiers_count = email_usagers_dossiers.count - errors.count
create_bulk_message_mail(valid_dossiers_count, Dossier.states.fetch(:brouillon))
valid_dossiers_count = dossiers.count - errors.count
bulk_message.assign_attributes(
dossier_count: valid_dossiers_count,
dossier_state: Dossier.states.fetch(:brouillon),
sent_at: Time.zone.now,
instructeur_id: current_instructeur.id,
groupe_instructeurs: GroupeInstructeur.for_dossiers(dossiers)
)
bulk_message.save!
if errors.empty?
flash[:notice] = "Tous les messages ont été envoyés avec succès"
@ -262,18 +268,6 @@ module Instructeurs
private
def create_bulk_message_mail(dossier_count, dossier_state)
BulkMessage.create(
dossier_count: dossier_count,
dossier_state: dossier_state,
body: commentaire_params[:body],
sent_at: Time.zone.now,
instructeur_id: current_instructeur.id,
piece_jointe: commentaire_params[:piece_jointe],
groupe_instructeurs: email_usagers_groupe_instructeurs
)
end
def assign_to_params
params.require(:assign_to)
.permit(:instant_expert_avis_email_notifications_enabled, :instant_email_dossier_notifications_enabled, :instant_email_message_notifications_enabled, :daily_email_notifications_enabled, :weekly_email_notifications_enabled)
@ -355,20 +349,8 @@ module Instructeurs
@current_filters ||= procedure_presentation.filters.fetch(statut, [])
end
def email_usagers_dossiers
procedure.dossiers.state_brouillon.where(groupe_instructeur: current_instructeur.groupe_instructeur_ids).includes(:groupe_instructeur)
end
def email_usagers_groupe_instructeurs_label
email_usagers_dossiers.map(&:groupe_instructeur).uniq.map(&:label)
end
def email_usagers_groupe_instructeurs
email_usagers_dossiers.map(&:groupe_instructeur).uniq
end
def commentaire_params
params.require(:commentaire).permit(:body, :piece_jointe)
def bulk_message_params
params.require(:bulk_message).permit(:body)
end
end
end

View file

@ -541,6 +541,7 @@ module Users
def update_dossier_and_compute_errors
errors = []
@dossier.assign_attributes(champs_public_params)
if @dossier.champs_public_all.any?(&:changed_for_autosave?)
@dossier.last_champ_updated_at = Time.zone.now

View file

@ -1,5 +1,4 @@
class BulkMessage < ApplicationRecord
belongs_to :instructeur
has_and_belongs_to_many :groupe_instructeurs, -> { order(:label) }
has_one_attached :piece_jointe
end

View file

@ -1,8 +1,6 @@
class Commentaire < ApplicationRecord
include Discard::Model
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
belongs_to :instructeur, inverse_of: :commentaires, optional: true
belongs_to :expert, inverse_of: :commentaires, optional: true
has_one :dossier_correction, inverse_of: :commentaire, dependent: :nullify

View file

@ -215,7 +215,7 @@ class Dossier < ApplicationRecord
}
scope :for_procedure_preview, -> { where(for_procedure_preview: true) }
scope :for_editing_fork, -> { where.not(editing_fork_origin_id: nil) }
scope :for_groupe_instructeur, -> (groupe_instructeurs) { where(groupe_instructeur: groupe_instructeurs) }
scope :order_by_updated_at, -> (order = :desc) { order(updated_at: order) }
scope :order_by_created_at, -> (order = :asc) { order(depose_at: order, created_at: order, id: order) }
scope :updated_since, -> (since) { where('dossiers.updated_at >= ?', since) }

View file

@ -30,7 +30,7 @@ class GroupeInstructeur < ApplicationRecord
scope :for_api_v2, -> { includes(procedure: [:administrateurs]) }
scope :active, -> { where(closed: false) }
scope :closed, -> { where(closed: true) }
scope :for_dossiers, -> (dossiers) { joins(:dossiers).where(dossiers: dossiers).distinct(:id) }
def add(instructeur)
return if instructeur.nil?
return if in?(instructeur.groupe_instructeurs)

View file

@ -3,30 +3,35 @@
= render partial: 'administrateurs/breadcrumbs',
locals: { steps: [[@procedure.libelle.truncate_words(10), instructeur_procedure_path(@procedure)],
[t('.contact_users')]] }
.messagerie.container
- if @email_usagers_dossiers.present?
%p.notice.mb-2.mt-4
= t('.notice', dossiers_count: pluralize(@dossiers_count, 'personne'), groupe_instructeurs: @groupe_instructeurs.join(', '))
.messagerie.fr-container
%h1 Contacter les usagers
%p.fr-highlight
= t('.hint', count: @dossiers_without_groupe_count).html_safe
= render partial: 'shared/dossiers/messages/form', locals: { commentaire: @commentaire, form_url: create_multiple_commentaire_instructeur_procedure_path(@procedure), disable_piece_jointe: true }
- if @dossiers_without_groupe_count.positive?
= form_for(@bulk_message, url: create_multiple_commentaire_instructeur_procedure_path, html: { data: { controller: 'persisted-form', persisted_form_key_value: dom_id(@procedure, :bulk_message) } }) do |f|
- if @bulk_messages.present?
%section.list-avis.mt-8
%h1.tab-title
Messages envoyés précédemment
%span.fr-badge= @bulk_messages.count
%p.mandatory-explanation= t('asterisk_html', scope: [:utils])
%ul
- @bulk_messages.each do |message|
%li.one-avis.flex.align-start
.width-100
%h2.claimant
%span.email= message.instructeur.email
%span.date message envoyé à #{@dossiers_count} usagers le #{message.sent_at.strftime('%d/%m/%y à %H:%M')}
%p= message.body
.answer.flex.align-start
- if message.piece_jointe.present?
= render Attachment::ShowComponent.new(attachment: message.piece_jointe.attachment)
= render Dsfr::InputComponent.new(form: f, attribute: :body, input_type: :text_area, opts: { rows: 5, placeholder: t('views.shared.dossiers.messages.form.write_message_placeholder'), title: t('views.shared.dossiers.messages.form.write_message_placeholder'), class: 'fr-input message-textarea'})
.fr-mt-3w
= f.submit t('views.shared.dossiers.messages.form.send_message'), class: 'fr-btn', data: { disable: true }
- else
.page-title.center
%h2 Il ny a aucun dossier en brouillon dans vos groupes dinstructeurs
%h2 Il ny a aucun dossier en brouillon
- if @bulk_messages.present?
%section.list-avis.mt-8
%h1.tab-title
Messages envoyés précédemment
%span.fr-badge= @bulk_messages.count
%ul
- @bulk_messages.each do |message|
%li.one-avis.flex.align-start
.width-100
%h2.claimant
%span.email= message.instructeur.email
%span.date message envoyé à #{@dossiers_count} usagers le #{message.sent_at.strftime('%d/%m/%y à %H:%M')}
%p= message.body