Add DossiersController#terminer

This commit is contained in:
gregoirenovel 2017-12-01 12:57:01 +01:00
parent a5b4b20a7d
commit a04af24cbf
4 changed files with 170 additions and 1 deletions

View file

@ -1,5 +1,6 @@
module NewGestionnaire module NewGestionnaire
class DossiersController < ProceduresController class DossiersController < ProceduresController
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::TextHelper include ActionView::Helpers::TextHelper
def attestation def attestation
@ -67,6 +68,44 @@ module NewGestionnaire
redirect_to dossier_path(procedure, dossier) redirect_to dossier_path(procedure, dossier)
end 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 def create_commentaire
commentaire_hash = commentaire_params.merge(email: current_gestionnaire.email, dossier: dossier) commentaire_hash = commentaire_params.merge(email: current_gestionnaire.email, dossier: dossier)
@ -137,5 +176,15 @@ module NewGestionnaire
def champs_private_params def champs_private_params
params.require(:dossier).permit(champs_private_attributes: [:id, :value, value: []]) params.require(:dossier).permit(champs_private_attributes: [:id, :value, value: []])
end 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
end end

View file

@ -3,7 +3,7 @@
.icon{ class: popup_class } .icon{ class: popup_class }
#{popup_title} nº #{dossier.id} #{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)' = text_area :dossier, :motivation, class: 'motivation-text-area', placeholder: 'Rédigez votre motivation ici (facultative)'
- if title == 'Accepter' - if title == 'Accepter'
%p.help %p.help

View file

@ -256,6 +256,7 @@ Rails.application.routes.draw do
post 'commentaire' => 'dossiers#create_commentaire' post 'commentaire' => 'dossiers#create_commentaire'
post 'passer-en-instruction' => 'dossiers#passer_en_instruction' post 'passer-en-instruction' => 'dossiers#passer_en_instruction'
post 'repasser-en-construction' => 'dossiers#repasser_en_construction' post 'repasser-en-construction' => 'dossiers#repasser_en_construction'
post 'terminer'
scope :carte do scope :carte do
get 'position' get 'position'
end end

View file

@ -108,6 +108,125 @@ describe NewGestionnaire::DossiersController, type: :controller do
it { is_expected.to redirect_to dossier_path(procedure, dossier) } it { is_expected.to redirect_to dossier_path(procedure, dossier) }
end 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 describe '#show #messagerie #annotations_privees #avis' do
before do before do
dossier.notifications = %w(champs annotations_privees avis commentaire).map{ |type| Notification.create!(type_notif: type) } dossier.notifications = %w(champs annotations_privees avis commentaire).map{ |type| Notification.create!(type_notif: type) }