fix(dossier): avoir final state if etablissement is still in degraded mode

Maintenant qu'on autorise un dossier pour entreprise a être créé en mode "dégradé",
(avec établissement incomplet suite à API Entreprise/INSEE down…),
on empêche de l'accepter/refuser/classer sans suite tant qu'on a pas
vérifié son SIRET.

Fix https://sentry.io/organizations/demarches-simplifiees/issues/2839832517/?project=1429550&query=is%3Aunresolved
This commit is contained in:
Colin Darie 2022-09-21 15:05:11 +02:00
parent 864b1f0c3c
commit e7de19b24d
3 changed files with 58 additions and 7 deletions

View file

@ -170,19 +170,19 @@ class Dossier < ApplicationRecord
end
event :accepter, after: :after_accepter do
transitions from: :en_instruction, to: :accepte
transitions from: :en_instruction, to: :accepte, guard: :can_terminer?
end
event :accepter_automatiquement, after: :after_accepter_automatiquement do
transitions from: :en_construction, to: :accepte
transitions from: :en_construction, to: :accepte, guard: :can_terminer?
end
event :refuser, after: :after_refuser do
transitions from: :en_instruction, to: :refuse
transitions from: :en_instruction, to: :refuse, guard: :can_terminer?
end
event :classer_sans_suite, after: :after_classer_sans_suite do
transitions from: :en_instruction, to: :sans_suite
transitions from: :en_instruction, to: :sans_suite, guard: :can_terminer?
end
event :repasser_en_instruction, after: :after_repasser_en_instruction do
@ -515,6 +515,12 @@ class Dossier < ApplicationRecord
brouillon? && procedure.dossier_can_transition_to_en_construction? && !for_procedure_preview?
end
def can_terminer?
return false if etablissement&.as_degraded_mode?
true
end
def can_repasser_en_instruction?
termine? && !user_deleted?
end

View file

@ -18,12 +18,22 @@ FactoryBot.define do
end
trait :with_entreprise do
after(:build) do |dossier, _evaluator|
transient do
as_degraded_mode { false }
end
after(:build) do |dossier, evaluator|
if dossier.procedure.for_individual?
raise 'Inconsistent factory: attempting to create a dossier :with_entreprise on a procedure that is `for_individual?`'
end
etablissement = create(:etablissement, :with_exercices, :with_effectif_mensuel)
dossier.etablissement = etablissement
etablissement = if evaluator.as_degraded_mode
Etablissement.new(siret: build(:etablissement).siret)
else
create(:etablissement, :with_exercices, :with_effectif_mensuel)
end
dossier.update(etablissement:)
end
end

View file

@ -1138,6 +1138,41 @@ describe Dossier do
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
end
describe "can't transition to terminer when etablissement is in degraded mode" do
let(:instructeur) { create(:instructeur) }
let(:motivation) { 'motivation' }
context "when dossier is en_instruction" do
let(:dossier_incomplete) { create(:dossier, :en_instruction, :with_entreprise, as_degraded_mode: true) }
let(:dossier_ok) { create(:dossier, :en_instruction, :with_entreprise, as_degraded_mode: false) }
it "can't accepter" do
expect(dossier_incomplete.may_accepter?(instructeur:, motivation:)).to be_falsey
expect(dossier_ok.accepter(instructeur:, motivation:)).to be_truthy
end
it "can't refuser" do
expect(dossier_incomplete.may_refuser?(instructeur:, motivation:)).to be_falsey
expect(dossier_ok.may_refuser?(instructeur:, motivation:)).to be_truthy
end
it "can't classer_sans_suite" do
expect(dossier_incomplete.may_classer_sans_suite?(instructeur:, motivation:)).to be_falsey
expect(dossier_ok.may_classer_sans_suite?(instructeur:, motivation:)).to be_truthy
end
end
context "when dossier is en_construction" do
let(:dossier_incomplete) { create(:dossier, :en_construction, :with_entreprise, as_degraded_mode: true) }
let(:dossier_ok) { create(:dossier, :en_construction, :with_entreprise, as_degraded_mode: false) }
it "can't accepter_automatiquement" do
expect(dossier_incomplete.may_accepter_automatiquement?(instructeur:, motivation:)).to be_falsey
expect(dossier_ok.accepter_automatiquement(instructeur:, motivation:)).to be_truthy
end
end
end
describe "#check_mandatory_champs" do
include Logic