Skip to content

Commit dd8b3d2

Browse files
committed
fix(siret_type_de_champ): mandatory check no longer requires a resolved etablissement
champ_blank_or_invalid? checked Siret.new(siret: champ.value), but value is only populated by a successful fetch (update_external_data!). For a mandatory SIRET champ, this meant the dossier's mandatory-field check (Dossier#check_mandatory_and_visible_champs_for) always blocked submission while pending or in a non-blocking technical_error, regardless of ExternalDataChampValidator's kind-aware logic. Check external_id's format instead: it's set as soon as the user types a SIRET and survives resets, so a mandatory field is satisfied by a syntactically valid input. Whether that SIRET actually exists is ExternalDataChampValidator's separate, kind-aware concern.
1 parent 38ef044 commit dd8b3d2

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

app/models/types_de_champ/siret_type_de_champ.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ def estimated_fill_duration(revision)
77
FILL_DURATION_MEDIUM
88
end
99

10-
def champ_blank_or_invalid?(champ) = Siret.new(siret: champ.value).invalid?
10+
# A syntactically valid external_id is enough to satisfy the mandatory
11+
# check while the async fetch is pending or failed non-blockingly:
12+
# the actual blocking decision belongs to ExternalDataChampValidator.
13+
def champ_blank_or_invalid?(champ) = Siret.new(siret: champ.external_id).invalid?
1114

1215
def columns(procedure_id:, displayable: true, prefix: nil)
1316
super

spec/models/champs/siret_champ_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,42 @@
8585
end
8686
end
8787

88+
describe '#mandatory_blank?' do
89+
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :siret, mandatory: true }]) }
90+
let(:external_id) { "12345678901245" }
91+
92+
context 'when the fetch is pending' do
93+
before { champ.update_columns(external_state: 'waiting_for_job') }
94+
95+
it 'is not mandatory_blank (a syntactically valid SIRET is enough while pending)' do
96+
expect(champ.mandatory_blank?).to be false
97+
end
98+
end
99+
100+
context 'when the fetch failed with a technical error' do
101+
let(:exception) { ExternalDataException.new(error: 'API down', code: 503, kind: :technical_error) }
102+
103+
before do
104+
champ.update_columns(
105+
external_state: 'external_error',
106+
fetch_external_data_exceptions: [exception]
107+
)
108+
end
109+
110+
it 'is not mandatory_blank (a syntactically valid SIRET is enough despite the technical error)' do
111+
expect(champ.mandatory_blank?).to be false
112+
end
113+
end
114+
115+
context 'when the format is invalid' do
116+
let(:external_id) { "12345" }
117+
118+
it 'is mandatory_blank' do
119+
expect(champ.mandatory_blank?).to be true
120+
end
121+
end
122+
end
123+
88124
describe '.fetch_external_data' do
89125
let(:api_etablissement_status) { 200 }
90126
let(:api_etablissement_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }

spec/models/dossier_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@
20622062
let(:champ_siret) { dossier.champs.first }
20632063

20642064
before do
2065-
champ_siret.update(value: '44011762001530')
2065+
champ_siret.update(external_id: '44011762001530', value: '44011762001530', etablissement: build(:etablissement, siret: '44011762001530'))
20662066
end
20672067

20682068
it 'should not have errors' do
@@ -2071,7 +2071,7 @@
20712071

20722072
context "and invalid SIRET" do
20732073
before do
2074-
champ_siret.update(value: "1234")
2074+
champ_siret.update(external_id: "1234", value: "1234")
20752075
dossier.reload
20762076
end
20772077

0 commit comments

Comments
 (0)