diff --git a/app/models/prefill_description.rb b/app/models/prefill_description.rb index 2148b3dce..090e02fb8 100644 --- a/app/models/prefill_description.rb +++ b/app/models/prefill_description.rb @@ -45,15 +45,33 @@ class PrefillDescription < SimpleDelegator private - def prefilled_champs_for_link - prefilled_champs.map { |type_de_champ| ["champ_#{type_de_champ.to_typed_id}", type_de_champ.example_value] }.to_h - end - - def prefilled_champs_for_query - prefilled_champs.map { |type_de_champ| "\"champ_#{type_de_champ.to_typed_id}\": \"#{type_de_champ.example_value}\"" } .join(', ') - end - def active_fillable_public_types_de_champ active_revision.types_de_champ_public.fillable end + + def prefilled_champs_for_link + prefilled_champs_as_params.map(&:to_a).to_h + end + + def prefilled_champs_for_query + prefilled_champs_as_params.map(&:to_s).join(', ') + end + + def prefilled_champs_as_params + prefilled_champs.map { |type_de_champ| Param.new(type_de_champ.to_typed_id, type_de_champ.example_value) } + end + + Param = Struct.new(:key, :value) do + def to_a + ["champ_#{key}", value] + end + + def to_s + if value.is_a?(Array) + "\"champ_#{key}\": #{value}" + else + "\"champ_#{key}\": \"#{value}\"" + end + end + end end diff --git a/spec/models/prefill_description_spec.rb b/spec/models/prefill_description_spec.rb index 65c8cd1a5..5c5846965 100644 --- a/spec/models/prefill_description_spec.rb +++ b/spec/models/prefill_description_spec.rb @@ -101,6 +101,19 @@ RSpec.describe PrefillDescription, type: :model do ) ) end + + context 'when the type de champ can have multiple values' do + let(:type_de_champ) { create(:type_de_champ_multiple_drop_down_list, procedure: procedure) } + + it 'builds the URL with array parameter' do + expect(prefill_description.prefill_link).to eq( + commencer_url( + path: procedure.path, + "champ_#{type_de_champ.to_typed_id}": TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp.new(type_de_champ).example_value + ) + ) + end + end end describe '#prefill_query' do @@ -120,5 +133,20 @@ RSpec.describe PrefillDescription, type: :model do it "builds the query to create a new prefilled dossier" do expect(prefill_description.prefill_query).to eq(expected_query) end + + context 'when the type de champ can have multiple values' do + let(:type_de_champ) { create(:type_de_champ_multiple_drop_down_list, procedure: procedure) } + let(:expected_query) do + <<~TEXT + curl --request POST '#{api_public_v1_dossiers_url(procedure)}' \\ + --header 'Content-Type: application/json' \\ + --data '{"champ_#{type_de_champ.to_typed_id}": #{TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp.new(type_de_champ).example_value}}' + TEXT + end + + it 'builds the query with array parameter' do + expect(prefill_description.prefill_query).to eq(expected_query) + end + end end end