Merge pull request #4085 from betagouv/avoid_wrong_aasm_transitions
Avoid wrong aasm transitions
This commit is contained in:
commit
29adf51a0e
2 changed files with 79 additions and 21 deletions
|
@ -3,6 +3,7 @@ module Gestionnaires
|
||||||
include ActionView::Helpers::NumberHelper
|
include ActionView::Helpers::NumberHelper
|
||||||
include ActionView::Helpers::TextHelper
|
include ActionView::Helpers::TextHelper
|
||||||
include CreateAvisConcern
|
include CreateAvisConcern
|
||||||
|
include DossierHelper
|
||||||
|
|
||||||
after_action :mark_demande_as_read, only: :show
|
after_action :mark_demande_as_read, only: :show
|
||||||
after_action :mark_messagerie_as_read, only: [:messagerie, :create_commentaire]
|
after_action :mark_messagerie_as_read, only: [:messagerie, :create_commentaire]
|
||||||
|
@ -91,15 +92,23 @@ module Gestionnaires
|
||||||
end
|
end
|
||||||
|
|
||||||
def repasser_en_construction
|
def repasser_en_construction
|
||||||
dossier.repasser_en_construction!(current_gestionnaire)
|
if dossier.en_construction?
|
||||||
flash.notice = 'Dossier repassé en construction.'
|
flash.notice = 'Le dossier est déjà en construction.'
|
||||||
|
else
|
||||||
|
dossier.repasser_en_construction!(current_gestionnaire)
|
||||||
|
flash.notice = 'Dossier repassé en construction.'
|
||||||
|
end
|
||||||
|
|
||||||
render partial: 'state_button_refresh', locals: { dossier: dossier }
|
render partial: 'state_button_refresh', locals: { dossier: dossier }
|
||||||
end
|
end
|
||||||
|
|
||||||
def repasser_en_instruction
|
def repasser_en_instruction
|
||||||
flash.notice = "Le dossier #{dossier.id} a été repassé en instruction."
|
if dossier.en_instruction?
|
||||||
dossier.repasser_en_instruction!(current_gestionnaire)
|
flash.notice = 'Le dossier est déjà en instruction.'
|
||||||
|
else
|
||||||
|
flash.notice = "Le dossier #{dossier.id} a été repassé en instruction."
|
||||||
|
dossier.repasser_en_instruction!(current_gestionnaire)
|
||||||
|
end
|
||||||
|
|
||||||
render partial: 'state_button_refresh', locals: { dossier: dossier }
|
render partial: 'state_button_refresh', locals: { dossier: dossier }
|
||||||
end
|
end
|
||||||
|
@ -108,16 +117,20 @@ module Gestionnaires
|
||||||
motivation = params[:dossier] && params[:dossier][:motivation]
|
motivation = params[:dossier] && params[:dossier][:motivation]
|
||||||
justificatif = params[:dossier] && params[:dossier][:justificatif_motivation]
|
justificatif = params[:dossier] && params[:dossier][:justificatif_motivation]
|
||||||
|
|
||||||
case params[:process_action]
|
if dossier.termine?
|
||||||
when "refuser"
|
flash.notice = "Le dossier est déjà #{dossier_display_state(dossier, lower: true)}"
|
||||||
dossier.refuser!(current_gestionnaire, motivation, justificatif)
|
else
|
||||||
flash.notice = "Dossier considéré comme refusé."
|
case params[:process_action]
|
||||||
when "classer_sans_suite"
|
when "refuser"
|
||||||
dossier.classer_sans_suite!(current_gestionnaire, motivation, justificatif)
|
dossier.refuser!(current_gestionnaire, motivation, justificatif)
|
||||||
flash.notice = "Dossier considéré comme sans suite."
|
flash.notice = "Dossier considéré comme refusé."
|
||||||
when "accepter"
|
when "classer_sans_suite"
|
||||||
dossier.accepter!(current_gestionnaire, motivation, justificatif)
|
dossier.classer_sans_suite!(current_gestionnaire, motivation, justificatif)
|
||||||
flash.notice = "Dossier traité avec succès."
|
flash.notice = "Dossier considéré comme sans suite."
|
||||||
|
when "accepter"
|
||||||
|
dossier.accepter!(current_gestionnaire, motivation, justificatif)
|
||||||
|
flash.notice = "Dossier traité avec succès."
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
render partial: 'state_button_refresh', locals: { dossier: dossier }
|
render partial: 'state_button_refresh', locals: { dossier: dossier }
|
||||||
|
|
|
@ -128,21 +128,51 @@ describe Gestionnaires::DossiersController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#repasser_en_construction' do
|
describe '#repasser_en_construction' do
|
||||||
|
let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
dossier.en_instruction!
|
|
||||||
sign_in gestionnaire
|
sign_in gestionnaire
|
||||||
|
post :repasser_en_construction,
|
||||||
|
params: { procedure_id: procedure.id, dossier_id: dossier.id },
|
||||||
|
format: 'js'
|
||||||
end
|
end
|
||||||
|
|
||||||
subject { post :repasser_en_construction, params: { procedure_id: procedure.id, dossier_id: dossier.id }, format: 'js' }
|
it { expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) }
|
||||||
|
it { expect(response).to have_http_status(:ok) }
|
||||||
|
it { expect(response.body).to include('.state-button') }
|
||||||
|
|
||||||
it 'change state to en_construction' do
|
context 'when the dossier has already been put en_construction' do
|
||||||
subject
|
let(:dossier) { create(:dossier, :en_construction, procedure: procedure) }
|
||||||
|
|
||||||
dossier.reload
|
it 'warns about the error, but doesn’t raise' do
|
||||||
expect(dossier.state).to eq(Dossier.states.fetch(:en_construction))
|
expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction))
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#repasser_en_instruction' do
|
||||||
|
let(:dossier) { create(:dossier, :refuse, procedure: procedure) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
sign_in gestionnaire
|
||||||
|
post :repasser_en_instruction,
|
||||||
|
params: { procedure_id: procedure.id, dossier_id: dossier.id },
|
||||||
|
format: 'js'
|
||||||
end
|
end
|
||||||
|
|
||||||
it { expect(subject.body).to include('.state-button') }
|
it { expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_instruction)) }
|
||||||
|
it { expect(response).to have_http_status(:ok) }
|
||||||
|
it { expect(response.body).to include('.state-button') }
|
||||||
|
|
||||||
|
context 'when the dossier has already been put en_instruction' do
|
||||||
|
let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) }
|
||||||
|
|
||||||
|
it 'warns about the error, but doesn’t raise' do
|
||||||
|
expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_instruction))
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#terminer' do
|
describe '#terminer' do
|
||||||
|
@ -319,6 +349,21 @@ describe Gestionnaires::DossiersController, type: :controller do
|
||||||
it { expect(subject.body).to include('.state-button') }
|
it { expect(subject.body).to include('.state-button') }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when a dossier is already closed' do
|
||||||
|
let(:dossier) { create(:dossier, :accepte, procedure: procedure) }
|
||||||
|
|
||||||
|
before { allow(dossier).to receive(:accepter!) }
|
||||||
|
|
||||||
|
subject { post :terminer, params: { process_action: "accepter", procedure_id: procedure.id, dossier_id: dossier.id, dossier: { justificatif_motivation: fake_justificatif } }, format: 'js' }
|
||||||
|
|
||||||
|
it 'does not close it again' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(dossier).not_to have_received(:accepter!)
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#create_commentaire" do
|
describe "#create_commentaire" do
|
||||||
|
|
Loading…
Reference in a new issue