Merge pull request #2516 from betagouv/messagerie

Détails d'un dossier : ajout de la messagerie
This commit is contained in:
Pierre de La Morinerie 2018-09-05 14:09:24 +02:00 committed by GitHub
commit 1836d16fb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 113 additions and 21 deletions

View file

@ -4,8 +4,8 @@ module NewUser
helper_method :new_demarche_url
before_action :ensure_ownership!, except: [:index, :show, :demande, :modifier, :update, :recherche]
before_action :ensure_ownership_or_invitation!, only: [:show, :demande, :modifier, :update]
before_action :ensure_ownership!, except: [:index, :show, :demande, :messagerie, :modifier, :update, :recherche]
before_action :ensure_ownership_or_invitation!, only: [:show, :demande, :messagerie, :modifier, :update, :create_commentaire]
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update]
before_action :forbid_invite_submission!, only: [:update]
@ -38,6 +38,11 @@ module NewUser
@dossier = dossier
end
def messagerie
@dossier = dossier
@commentaire = Commentaire.new
end
def attestation
send_data(dossier.attestation.pdf.read, filename: 'attestation.pdf', type: 'application/pdf')
end
@ -122,6 +127,18 @@ module NewUser
@dossier = current_user.dossiers.includes(:procedure).find(params[:id])
end
def create_commentaire
@commentaire = CommentaireService.create(current_user, dossier, commentaire_params)
if @commentaire.save
flash.notice = "Message envoyé"
redirect_to messagerie_dossier_path(dossier)
else
flash.now.alert = @commentaire.errors.full_messages
render :messagerie
end
end
def ask_deletion
dossier = current_user.dossiers.includes(:user, procedure: :administrateur).find(params[:id])
@ -185,7 +202,7 @@ module NewUser
end
def dossier
Dossier.find(params[:id] || params[:dossier_id])
@dossier ||= Dossier.find(params[:id] || params[:dossier_id])
end
def dossier_with_champs
@ -223,6 +240,10 @@ module NewUser
params.require(:dossier).permit(:autorisation_donnees)
end
def commentaire_params
params.require(:commentaire).permit(:body, :file)
end
def passage_en_construction?
dossier.brouillon? && !draft?
end

View file

@ -2,4 +2,4 @@
= render partial: 'header', locals: { avis: @avis, dossier: @dossier }
= render partial: "new_gestionnaire/shared/messagerie", locals: { dossier: @dossier, messagerie_seen_at: nil, new_commentaire: @commentaire, form_url: commentaire_gestionnaire_avis_path(@avis) }
= render partial: "shared/dossiers/messagerie", locals: { dossier: @dossier, user_email: current_gestionnaire.email, messagerie_seen_at: nil, new_commentaire: @commentaire, form_url: commentaire_gestionnaire_avis_path(@avis) }

View file

@ -2,4 +2,4 @@
= render partial: "header", locals: { dossier: @dossier }
= render partial: "new_gestionnaire/shared/messagerie", locals: { dossier: @dossier, messagerie_seen_at: @messagerie_seen_at , new_commentaire: @commentaire, form_url: commentaire_gestionnaire_dossier_path(@dossier.procedure, @dossier) }
= render partial: "shared/dossiers/messagerie", locals: { dossier: @dossier, user_email: current_gestionnaire.email, messagerie_seen_at: @messagerie_seen_at , new_commentaire: @commentaire, form_url: commentaire_gestionnaire_dossier_path(@dossier.procedure, @dossier) }

View file

@ -84,7 +84,7 @@
%ul.messages-list
- @dossier.commentaires.each do |commentaire|
%li
= render partial: "new_gestionnaire/shared/messages/message", locals: { commentaire: commentaire, messagerie_seen_at: nil }
= render partial: "new_gestionnaire/shared/messages/message", locals: { commentaire: commentaire, user_email: current_gestionnaire.email, messagerie_seen_at: nil }
%script{ type: "text/javascript" }
window.print();

View file

@ -1,7 +0,0 @@
.messagerie.container
%ul.messages-list
- dossier.commentaires.each do |commentaire|
%li{ class: commentaire_is_from_me_class(commentaire, current_gestionnaire.email) }
= render partial: "new_gestionnaire/shared/messages/message", locals: { commentaire: commentaire, messagerie_seen_at: messagerie_seen_at }
= render partial: "new_gestionnaire/shared/messages/form", locals: { commentaire: new_commentaire, form_url: form_url }

View file

@ -0,0 +1,6 @@
- content_for(:title, "Messagerie · Dossier nº #{@dossier.id} (#{@dossier.procedure.libelle})")
#dossier-show
= render partial: 'new_user/dossiers/show/header', locals: { dossier: @dossier }
= render partial: "shared/dossiers/messagerie", locals: { dossier: @dossier, user_email: current_user.email, messagerie_seen_at: nil, new_commentaire: @commentaire, form_url: commentaire_dossier_path(@dossier) }

View file

@ -10,3 +10,4 @@
%ul.tabs
= active_tab_item('Résumé', dossier_path(dossier))
= active_tab_item('Demande', demande_dossier_path(dossier))
= active_tab_item('Messagerie', messagerie_dossier_path(dossier))

View file

@ -0,0 +1,7 @@
.messagerie.container
%ul.messages-list
- dossier.commentaires.each do |commentaire|
%li{ class: commentaire_is_from_me_class(commentaire, user_email) }
= render partial: "shared/dossiers/messages/message", locals: { commentaire: commentaire, user_email: user_email, messagerie_seen_at: messagerie_seen_at }
= render partial: "shared/dossiers/messages/form", locals: { commentaire: new_commentaire, form_url: form_url }

View file

@ -1,10 +1,10 @@
= render partial: 'new_gestionnaire/shared/messages/message_icon', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire }
= render partial: 'shared/dossiers/messages/message_icon', locals: { commentaire: commentaire, user_email: user_email }
.width-100
%h2
%span.mail
= render partial: 'new_gestionnaire/shared/messages/message_issuer', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire }
- if ![current_gestionnaire.email, commentaire.dossier.user.email, OLD_CONTACT_EMAIL, CONTACT_EMAIL].include?(commentaire.email)
= render partial: 'shared/dossiers/messages/message_issuer', locals: { commentaire: commentaire, user_email: user_email }
- if ![user_email, commentaire.dossier.user.email, OLD_CONTACT_EMAIL, CONTACT_EMAIL].include?(commentaire.email)
%span.guest Invité
%span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) }
= I18n.l(commentaire.created_at.localtime, format: '%d/%m/%Y à %H:%M ')

View file

@ -1,5 +1,5 @@
- case commentaire.email
- when current_gestionnaire.email
- when user_email
= image_tag('icons/account-circle.svg', class: 'person-icon')
- when OLD_CONTACT_EMAIL
- when CONTACT_EMAIL

View file

@ -1,5 +1,5 @@
- case commentaire.email
- when current_gestionnaire.email
- when user_email
Vous
- when OLD_CONTACT_EMAIL
- when CONTACT_EMAIL

View file

@ -279,6 +279,8 @@ Rails.application.routes.draw do
patch 'modifier', to: 'dossiers#update'
get 'merci'
get 'demande'
get 'messagerie'
post 'commentaire' => 'dossiers#create_commentaire'
post 'ask_deletion'
get 'attestation'
end

View file

@ -523,6 +523,48 @@ describe NewUser::DossiersController, type: :controller do
it { is_expected.to render_template(:demande) }
end
describe "#create_commentaire" do
let(:dossier) { create(:dossier, :en_construction, user: user) }
let(:saved_commentaire) { dossier.commentaires.first }
let(:body) { "avant\napres" }
let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') }
let(:scan_result) { true }
subject {
post :create_commentaire, params: {
id: dossier.id,
commentaire: {
body: body,
file: file
}
}
}
before do
sign_in(user)
allow(ClamavService).to receive(:safe_file?).and_return(scan_result)
end
it "creates a commentaire" do
expect { subject }.to change(Commentaire, :count).by(1)
expect(response).to redirect_to(messagerie_dossier_path(dossier))
expect(flash.notice).to be_present
end
context "when the commentaire creation fails" do
let(:scan_result) { false }
it "renders the messagerie page with the invalid commentaire" do
expect { subject }.not_to change(Commentaire, :count)
expect(response).to render_template :messagerie
expect(flash.alert).to be_present
expect(assigns(:commentaire).body).to eq("<p>avant\n<br />apres</p>")
end
end
end
describe '#ask_deletion' do
before { sign_in(user) }

View file

@ -33,6 +33,27 @@ describe 'Dossier details:' do
expect(page).to have_content('Nouveau texte')
end
context 'with messages' do
let!(:commentaire) { create(:commentaire, dossier: dossier, email: 'instructeur@exemple.fr', body: 'Message envoyé à lusager') }
let(:message_body) { 'Message envoyé à linstructeur' }
scenario 'the user can send a message' do
visit_dossier dossier
click_on 'Messagerie'
expect(page).to have_current_path(messagerie_dossier_path(dossier))
expect(page).to have_content(commentaire.body)
fill_in 'commentaire_body', with: message_body
click_on 'Envoyer'
expect(page).to have_current_path(messagerie_dossier_path(dossier))
expect(page).to have_content('Message envoyé')
expect(page).to have_content(commentaire.body)
expect(page).to have_content(message_body)
end
end
private
def visit_dossier(dossier)

View file

@ -1,11 +1,10 @@
describe 'new_gestionnaire/shared/messages/message.html.haml', type: :view do
describe 'shared/dossiers/messages/message.html.haml', type: :view do
before { view.extend DossierHelper }
subject { render 'new_gestionnaire/shared/messages/message.html.haml', commentaire: commentaire, messagerie_seen_at: seen_at, current_gestionnaire: current_gestionnaire }
subject { render 'shared/dossiers/messages/message.html.haml', commentaire: commentaire, messagerie_seen_at: seen_at, user_email: dossier.user.email }
let(:dossier) { create(:dossier) }
let(:commentaire) { create(:commentaire, dossier: dossier) }
let(:current_gestionnaire) { create(:gestionnaire) }
let(:seen_at) { commentaire.created_at + 1.hour }
context "with a seen_at after commentaire created_at" do