diff --git a/app/controllers/instructeurs/avis_controller.rb b/app/controllers/instructeurs/avis_controller.rb index 0b280cd8e..8b2c0487c 100644 --- a/app/controllers/instructeurs/avis_controller.rb +++ b/app/controllers/instructeurs/avis_controller.rb @@ -6,7 +6,7 @@ module Instructeurs before_action :check_if_avis_revoked, only: [:show] before_action :redirect_if_no_sign_up_needed, only: [:sign_up] before_action :check_avis_exists_and_email_belongs_to_avis, only: [:sign_up, :create_instructeur] - before_action :set_avis_and_dossier, only: [:show, :instruction, :update] + before_action :set_avis_and_dossier, only: [:show, :instruction, :messagerie, :create_commentaire, :update] A_DONNER_STATUS = 'a-donner' DONNES_STATUS = 'donnes' @@ -53,6 +53,23 @@ module Instructeurs end end + def messagerie + @commentaire = Commentaire.new + end + + def create_commentaire + @commentaire = CommentaireService.build(current_instructeur, avis.dossier, commentaire_params) + + if @commentaire.save + @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now) + flash.notice = "Message envoyé" + redirect_to messagerie_instructeur_avis_path(avis.procedure, avis) + else + flash.alert = @commentaire.errors.full_messages + render :messagerie + end + end + def create_avis @procedure = Procedure.find(params[:procedure_id]) if !feature_enabled_for?(:expert_not_allowed_to_invite, @procedure) diff --git a/app/views/instructeurs/avis/_header.html.haml b/app/views/instructeurs/avis/_header.html.haml index 1c01bf985..0d2ab24e6 100644 --- a/app/views/instructeurs/avis/_header.html.haml +++ b/app/views/instructeurs/avis/_header.html.haml @@ -8,3 +8,4 @@ %ul.tabs = dynamic_tab_item('Demande', instructeur_avis_path(avis.procedure, avis)) = dynamic_tab_item('Avis', instruction_instructeur_avis_path(avis.procedure, avis), notification: avis.answer.blank?) + = dynamic_tab_item('Messagerie', messagerie_instructeur_avis_path(avis.procedure, avis)) diff --git a/app/views/instructeurs/avis/messagerie.html.haml b/app/views/instructeurs/avis/messagerie.html.haml new file mode 100644 index 000000000..46e64e69a --- /dev/null +++ b/app/views/instructeurs/avis/messagerie.html.haml @@ -0,0 +1,5 @@ +- content_for(:title, "Messagerie · Dossier nº #{@dossier.id} (#{@dossier.owner_name})") + += render partial: 'header', locals: { avis: @avis, dossier: @dossier } + += render partial: "shared/dossiers/messagerie", locals: { dossier: @dossier, connected_user: current_instructeur, messagerie_seen_at: nil, new_commentaire: @commentaire, form_url: commentaire_instructeur_avis_path(@avis) } diff --git a/config/routes.rb b/config/routes.rb index 405a06824..b207161a6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -300,6 +300,7 @@ Rails.application.routes.draw do get '', action: 'procedure', on: :collection, as: :procedure member do get 'instruction' + get 'messagerie' post 'commentaire' => 'avis#create_commentaire' post 'avis' => 'avis#create_avis' patch 'revoquer' diff --git a/spec/controllers/instructeurs/avis_controller_spec.rb b/spec/controllers/instructeurs/avis_controller_spec.rb index 6b6ffe4cc..720578b19 100644 --- a/spec/controllers/instructeurs/avis_controller_spec.rb +++ b/spec/controllers/instructeurs/avis_controller_spec.rb @@ -65,6 +65,14 @@ describe Instructeurs::AvisController, type: :controller do it { expect(assigns(:dossier)).to eq(dossier) } end + describe '#messagerie' do + before { get :messagerie, params: { id: avis_without_answer.id, procedure_id: procedure.id } } + + it { expect(response).to have_http_status(:success) } + it { expect(assigns(:avis)).to eq(avis_without_answer) } + it { expect(assigns(:dossier)).to eq(dossier) } + end + describe '#bilans_bdf' do before { get :bilans_bdf, params: { id: avis_without_answer.id, procedure_id: procedure.id } } @@ -110,6 +118,40 @@ describe Instructeurs::AvisController, type: :controller do end end + describe '#create_commentaire' do + let(:file) { nil } + let(:scan_result) { true } + let(:now) { Time.zone.parse("14/07/1789") } + + subject { post :create_commentaire, params: { id: avis_without_answer.id, procedure_id: procedure.id, commentaire: { body: 'commentaire body', piece_jointe: file } } } + + before do + allow(ClamavService).to receive(:safe_file?).and_return(scan_result) + Timecop.freeze(now) + end + + after { Timecop.return } + + it do + subject + + expect(response).to redirect_to(messagerie_instructeur_avis_path(avis_without_answer.procedure, avis_without_answer)) + expect(dossier.commentaires.map(&:body)).to match(['commentaire body']) + expect(dossier.reload.last_commentaire_updated_at).to eq(now) + end + + context "with a file" do + let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') } + + it do + subject + expect(Commentaire.last.piece_jointe.filename).to eq("piece_justificative_0.pdf") + end + + it { expect { subject }.to change(Commentaire, :count).by(1) } + end + end + describe '#expert_cannot_invite_another_expert' do let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, instructeur: instructeur, confidentiel: previous_avis_confidentiel) } let(:previous_avis_confidentiel) { false }