Skip to content

Commit 3140132

Browse files
committed
feat(siret_champ): block submission when the siret champ feeds a condition
A condition can target a column of a SIRET champ (value_json), so it cannot be evaluated until the data is fetched. In that case the historical blocking validation is restored via dependent_conditions?.
1 parent a9a4dde commit 3140132

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

app/models/champs/siret_champ.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def lenient_external_data_validation?
1313
true
1414
end
1515

16+
# A condition based on one of this champ's columns (value_json) cannot be
17+
# evaluated until the data is fetched: restore the blocking validation.
18+
def external_data_required_for_conditions?
19+
dependent_conditions?
20+
end
21+
1622
def external_data_status_message
1723
return :technical_error if etablissement&.as_degraded_mode?
1824

spec/models/champs/siret_champ_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
describe Champs::SiretChamp do
4+
include Logic
5+
46
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :siret }]) }
57
let(:dossier) { create(:dossier, procedure:) }
68
let(:champ) { dossier.champs.first.tap { _1.update(external_id:, etablissement:) } }
@@ -273,6 +275,26 @@
273275
end
274276
end
275277

278+
describe '#external_data_required_for_conditions?' do
279+
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :siret }, { type: :text }]) }
280+
let(:siret_tdc) { procedure.draft_revision.types_de_champ_public.first }
281+
let(:text_tdc) { procedure.draft_revision.types_de_champ_public.second }
282+
let(:champ) { dossier.champs.find(&:siret?) }
283+
284+
context 'when no other champ has a condition based on the siret champ' do
285+
it { expect(champ.external_data_required_for_conditions?).to be false }
286+
end
287+
288+
context 'when another champ has a condition based on a column of the siret champ' do
289+
before do
290+
naf_column = siret_tdc.columns(procedure_id: procedure.id).find { _1.label.match?(/NAF/i) }
291+
text_tdc.update!(condition: ds_eq(champ_column_value(naf_column), constant('4950Z')))
292+
end
293+
294+
it { expect(champ.external_data_required_for_conditions?).to be true }
295+
end
296+
end
297+
276298
describe '#reset_external_data!' do
277299
let(:external_id) { "12345678901245" }
278300
let(:etablissement) { create(:etablissement, siret: external_id) }

spec/models/concerns/champ_external_data_concern_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
let(:champ) { Champs::SiretChamp.new }
100100

101101
it 'returns nil when fetched' do
102-
allow(champ).to receive_messages(pending?: false, external_error?: false)
102+
allow(champ).to receive_messages(pending?: false, external_error?: false, external_data_required_for_conditions?: false)
103103
expect(champ.external_data_status_message).to be_nil
104104
end
105105

spec/validators/external_data_champ_validator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222

2323
context 'fetched' do
24-
before { allow(champ).to receive_messages(pending?: false, external_error?: false) }
24+
before { allow(champ).to receive_messages(pending?: false, external_error?: false, external_data_required_for_conditions?: false) }
2525
include_examples 'no error added'
2626
end
2727

0 commit comments

Comments
 (0)