diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index 66c54612f..a185ee643 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -1,5 +1,6 @@ module NewGestionnaire class DossiersController < ProceduresController + include ActionView::Helpers::NumberHelper include ActionView::Helpers::TextHelper def attestation @@ -67,6 +68,44 @@ module NewGestionnaire redirect_to dossier_path(procedure, dossier) end + def terminer + if params[:dossier] && params[:dossier][:motivation].present? + motivation = params[:dossier][:motivation] + end + + case params[:process_action] + when "refuse" + next_step = "refuse" + notice = "Dossier considéré comme refusé." + template = procedure.refused_mail_template + when "without_continuation" + next_step = "without_continuation" + notice = "Dossier considéré comme sans suite." + template = procedure.without_continuation_mail_template + when "close" + next_step = "close" + notice = "Dossier traité avec succès." + template = procedure.closed_mail_template + end + + dossier.next_step!('gestionnaire', next_step, motivation) + + # needed to force Carrierwave to provide dossier.attestation.pdf.read + # when the Feature.remote_storage is true, otherwise pdf.read is a closed stream. + dossier.reload + + attestation_pdf = nil + if check_attestation_emailable + attestation_pdf = dossier.attestation.pdf.read + end + + flash.notice = notice + + NotificationMailer.send_notification(dossier, template, attestation_pdf).deliver_now! + + redirect_to dossier_path(procedure, dossier) + end + def create_commentaire commentaire_hash = commentaire_params.merge(email: current_gestionnaire.email, dossier: dossier) @@ -137,5 +176,15 @@ module NewGestionnaire def champs_private_params params.require(:dossier).permit(champs_private_attributes: [:id, :value, value: []]) end + + def check_attestation_emailable + if dossier&.attestation&.emailable? == false + human_size = number_to_human_size(dossier.attestation.pdf.size) + msg = "the attestation of the dossier #{dossier.id} cannot be mailed because it is too heavy: #{human_size}" + capture_message(msg, level: 'error') + end + + dossier&.attestation&.emailable? + end end end diff --git a/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml b/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml index 2dfadaaf0..8dc3e716b 100644 --- a/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml +++ b/app/views/new_gestionnaire/dossiers/_state_button_motivation.html.haml @@ -3,7 +3,7 @@ .icon{ class: popup_class } #{popup_title} nº #{dossier.id} - = form_tag(backoffice_dossier_process_dossier_url(dossier.id, new_ui: true), method: :post, class: 'form') do + = form_tag(terminer_dossier_path(dossier.procedure, dossier), method: :post, class: 'form') do = text_area :dossier, :motivation, class: 'motivation-text-area', placeholder: 'Rédigez votre motivation ici (facultative)' - if title == 'Accepter' %p.help diff --git a/config/routes.rb b/config/routes.rb index 15b847679..3f83c1b52 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -256,6 +256,7 @@ Rails.application.routes.draw do post 'commentaire' => 'dossiers#create_commentaire' post 'passer-en-instruction' => 'dossiers#passer_en_instruction' post 'repasser-en-construction' => 'dossiers#repasser_en_construction' + post 'terminer' scope :carte do get 'position' end diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 2265e3724..68805ae28 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -108,6 +108,125 @@ describe NewGestionnaire::DossiersController, type: :controller do it { is_expected.to redirect_to dossier_path(procedure, dossier) } end + describe '#terminer' do + context "with refuse" do + before do + dossier.received! + sign_in gestionnaire + end + + subject { post :terminer, params: { process_action: "refuse", procedure_id: procedure.id, dossier_id: dossier.id} } + + it 'change state to refused' do + subject + + dossier.reload + expect(dossier.state).to eq('refused') + end + + it 'Notification email is sent' do + expect(NotificationMailer).to receive(:send_notification) + .with(dossier, kind_of(Mails::RefusedMail), nil).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_now!) + + subject + end + + it { is_expected.to redirect_to redirect_to dossier_path(procedure, dossier) } + end + + context "with without_continuation" do + before do + dossier.received! + sign_in gestionnaire + end + + subject { post :terminer, params: { process_action: "without_continuation", procedure_id: procedure.id, dossier_id: dossier.id} } + + it 'change state to without_continuation' do + subject + + dossier.reload + expect(dossier.state).to eq('without_continuation') + end + + it 'Notification email is sent' do + expect(NotificationMailer).to receive(:send_notification) + .with(dossier, kind_of(Mails::WithoutContinuationMail), nil).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_now!) + + subject + end + + it { is_expected.to redirect_to redirect_to dossier_path(procedure, dossier) } + end + + context "with close" do + let(:expected_attestation) { nil } + + before do + dossier.received! + sign_in gestionnaire + + expect(NotificationMailer).to receive(:send_notification) + .with(dossier, kind_of(Mails::ClosedMail), expected_attestation) + .and_return(NotificationMailer) + + expect(NotificationMailer).to receive(:deliver_now!) + end + + subject { post :terminer, params: { process_action: "close", procedure_id: procedure.id, dossier_id: dossier.id} } + + it 'change state to closed' do + subject + + dossier.reload + expect(dossier.state).to eq('closed') + end + + context 'when the dossier does not have any attestation' do + it 'Notification email is sent' do + subject + end + end + + context 'when the dossier has an attestation' do + before do + attestation = Attestation.new + allow(attestation).to receive(:pdf).and_return(double(read: 'pdf', size: 2.megabytes)) + allow(attestation).to receive(:emailable?).and_return(emailable) + + expect_any_instance_of(Dossier).to receive(:reload) + allow_any_instance_of(Dossier).to receive(:build_attestation).and_return(attestation) + end + + context 'emailable' do + let(:emailable) { true } + let(:expected_attestation) { 'pdf' } + + it 'Notification email is sent with the attestation' do + subject + + is_expected.to redirect_to redirect_to dossier_path(procedure, dossier) + end + end + + context 'when the dossier has an attestation not emailable' do + let(:emailable) { false } + let(:expected_attestation) { nil } + + it 'Notification email is sent without the attestation' do + expect(controller).to receive(:capture_message) + + subject + + is_expected.to redirect_to redirect_to dossier_path(procedure, dossier) + end + end + end + end + end + describe '#show #messagerie #annotations_privees #avis' do before do dossier.notifications = %w(champs annotations_privees avis commentaire).map{ |type| Notification.create!(type_notif: type) }