Merge pull request #8145 from demarches-simplifiees/new-prefilled-dossier
feat(dossier): prefill dossier from query params
This commit is contained in:
commit
f7deb5d7f3
48 changed files with 512 additions and 4 deletions
|
@ -228,6 +228,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
div.field_with_errors > input { // scss-lint:disable SelectorFormat
|
||||
border: 1px solid $dark-red;
|
||||
}
|
||||
|
||||
input[type=text],
|
||||
input[type=email],
|
||||
input[type=password],
|
||||
|
|
|
@ -148,6 +148,7 @@ module Users
|
|||
|
||||
def brouillon
|
||||
@dossier = dossier_with_champs
|
||||
@dossier.valid?(context: :prefilling)
|
||||
|
||||
# TODO: remove when the champs are unifed
|
||||
if !@dossier.autorisation_donnees
|
||||
|
@ -303,6 +304,7 @@ module Users
|
|||
)
|
||||
dossier.build_default_individual
|
||||
dossier.save!
|
||||
dossier.prefill!(PrefillParams.new(dossier, params).to_a)
|
||||
|
||||
if dossier.procedure.for_individual
|
||||
redirect_to identite_dossier_path(dossier)
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
@ -79,6 +80,7 @@ class Champ < ApplicationRecord
|
|||
scope :public_ordered, -> { public_only.ordered }
|
||||
scope :private_ordered, -> { private_only.ordered }
|
||||
scope :root, -> { where(parent_id: nil) }
|
||||
scope :prefilled, -> { where(prefilled: true) }
|
||||
|
||||
before_create :set_dossier_id, if: :needs_dossier_id?
|
||||
before_validation :set_dossier_id, if: :needs_dossier_id?
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
# id :integer not null, primary key
|
||||
# data :jsonb
|
||||
# fetch_external_data_exceptions :string is an Array
|
||||
# prefilled :boolean default(FALSE)
|
||||
# private :boolean default(FALSE), not null
|
||||
# rebased_at :datetime
|
||||
# row :integer
|
||||
|
|
12
app/models/concerns/dossier_prefillable_concern.rb
Normal file
12
app/models/concerns/dossier_prefillable_concern.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module DossierPrefillableConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def prefill!(champs_public_attributes)
|
||||
return if champs_public_attributes.empty?
|
||||
|
||||
assign_attributes(champs_public_attributes: champs_public_attributes.map { |h| h.merge(prefilled: true) })
|
||||
save(validate: false)
|
||||
end
|
||||
end
|
|
@ -46,6 +46,7 @@ class Dossier < ApplicationRecord
|
|||
self.ignored_columns = [:en_construction_conservation_extension]
|
||||
include DossierFilteringConcern
|
||||
include DossierRebaseConcern
|
||||
include DossierPrefillableConcern
|
||||
|
||||
enum state: {
|
||||
brouillon: 'brouillon',
|
||||
|
@ -82,6 +83,7 @@ class Dossier < ApplicationRecord
|
|||
has_many :champs_public, -> { root.public_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy
|
||||
has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy
|
||||
has_many :champs_public_all, -> { public_only }, class_name: 'Champ', inverse_of: false
|
||||
has_many :prefilled_champs_public, -> { root.public_only.prefilled }, class_name: 'Champ', inverse_of: false, dependent: :destroy
|
||||
has_many :commentaires, inverse_of: :dossier, dependent: :destroy
|
||||
has_many :invites, dependent: :destroy
|
||||
has_many :follows, -> { active }, inverse_of: :dossier
|
||||
|
@ -433,6 +435,8 @@ class Dossier < ApplicationRecord
|
|||
validates :individual, presence: true, if: -> { revision.procedure.for_individual? }
|
||||
validates :groupe_instructeur, presence: true, if: -> { !brouillon? }
|
||||
|
||||
validates_associated :prefilled_champs_public, on: :prefilling
|
||||
|
||||
def types_de_champ_public
|
||||
types_de_champ
|
||||
end
|
||||
|
@ -1226,6 +1230,12 @@ class Dossier < ApplicationRecord
|
|||
cloned_dossier
|
||||
end
|
||||
|
||||
def find_champs_by_stable_ids(stable_ids)
|
||||
return [] if stable_ids.compact.empty?
|
||||
|
||||
champs_public.joins(:type_de_champ).where(types_de_champ: { stable_id: stable_ids })
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_missing_traitemets
|
||||
|
|
81
app/models/prefill_params.rb
Normal file
81
app/models/prefill_params.rb
Normal file
|
@ -0,0 +1,81 @@
|
|||
class PrefillParams
|
||||
def initialize(dossier, params)
|
||||
@dossier = dossier
|
||||
@params = params
|
||||
end
|
||||
|
||||
def to_a
|
||||
build_prefill_values.filter(&:prefillable?).map(&:to_h)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_prefill_values
|
||||
value_by_stable_id = @params
|
||||
.to_unsafe_hash
|
||||
.map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
|
||||
.filter { |stable_id, value| stable_id.present? && value.present? }
|
||||
.to_h
|
||||
|
||||
@dossier
|
||||
.find_champs_by_stable_ids(value_by_stable_id.keys)
|
||||
.map { |champ| [champ, value_by_stable_id[champ.stable_id]] }
|
||||
.map { |champ, value| PrefillValue.new(champ:, value:) }
|
||||
end
|
||||
|
||||
def stable_id_from_typed_id(prefixed_typed_id)
|
||||
return nil unless prefixed_typed_id.starts_with?("champ_")
|
||||
|
||||
Champ.id_from_typed_id(prefixed_typed_id.gsub("champ_", "")).to_i
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
class PrefillValue
|
||||
AUTHORIZED_TYPES_DE_CHAMPS = [
|
||||
TypeDeChamp.type_champs.fetch(:text),
|
||||
TypeDeChamp.type_champs.fetch(:textarea),
|
||||
TypeDeChamp.type_champs.fetch(:decimal_number),
|
||||
TypeDeChamp.type_champs.fetch(:integer_number),
|
||||
TypeDeChamp.type_champs.fetch(:email),
|
||||
TypeDeChamp.type_champs.fetch(:phone),
|
||||
TypeDeChamp.type_champs.fetch(:iban)
|
||||
]
|
||||
|
||||
NEED_VALIDATION_TYPES_DE_CHAMPS = [
|
||||
TypeDeChamp.type_champs.fetch(:decimal_number),
|
||||
TypeDeChamp.type_champs.fetch(:integer_number)
|
||||
]
|
||||
|
||||
attr_reader :champ, :value
|
||||
|
||||
def initialize(champ:, value:)
|
||||
@champ = champ
|
||||
@value = value
|
||||
end
|
||||
|
||||
def prefillable?
|
||||
authorized? && valid?
|
||||
end
|
||||
|
||||
def to_h
|
||||
{
|
||||
id: champ.id,
|
||||
value: value
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def authorized?
|
||||
AUTHORIZED_TYPES_DE_CHAMPS.include?(champ.type_champ)
|
||||
end
|
||||
|
||||
def valid?
|
||||
return true unless NEED_VALIDATION_TYPES_DE_CHAMPS.include?(champ.type_champ)
|
||||
|
||||
champ.value = value
|
||||
champ.valid?
|
||||
end
|
||||
end
|
||||
end
|
5
db/migrate/20221201103802_add_prefilled_to_champs.rb
Normal file
5
db/migrate/20221201103802_add_prefilled_to_champs.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AddPrefilledToChamps < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :champs, :prefilled, :boolean
|
||||
end
|
||||
end
|
|
@ -220,6 +220,7 @@ ActiveRecord::Schema.define(version: 2022_12_05_144624) do
|
|||
t.string "external_id"
|
||||
t.string "fetch_external_data_exceptions", array: true
|
||||
t.bigint "parent_id"
|
||||
t.boolean "prefilled", default: false
|
||||
t.boolean "private", default: false, null: false
|
||||
t.datetime "rebased_at"
|
||||
t.integer "row"
|
||||
|
|
|
@ -1054,8 +1054,9 @@ describe Users::DossiersController, type: :controller do
|
|||
describe '#new' do
|
||||
let(:procedure) { create(:procedure, :published) }
|
||||
let(:procedure_id) { procedure.id }
|
||||
let(:params) { { procedure_id: procedure_id } }
|
||||
|
||||
subject { get :new, params: { procedure_id: procedure_id } }
|
||||
subject { get :new, params: params }
|
||||
|
||||
it 'clears the stored procedure context' do
|
||||
subject
|
||||
|
@ -1085,6 +1086,56 @@ describe Users::DossiersController, type: :controller do
|
|||
|
||||
it { is_expected.to redirect_to dossiers_path }
|
||||
end
|
||||
|
||||
context 'when prefill values are given' do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:value_1) { "any value" }
|
||||
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) }
|
||||
let(:value_2) { "another value" }
|
||||
|
||||
let(:params) {
|
||||
{
|
||||
procedure_id: procedure_id,
|
||||
"champ_#{type_de_champ_1.to_typed_id}" => value_1,
|
||||
"champ_#{type_de_champ_2.to_typed_id}" => value_2
|
||||
}
|
||||
}
|
||||
|
||||
it { expect { subject }.to change { Dossier.count }.by(1) }
|
||||
|
||||
it "prefills the dossier's champs with the given values" do
|
||||
subject
|
||||
|
||||
dossier = Dossier.last
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).value).to eq(value_1)
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).value).to eq(value_2)
|
||||
end
|
||||
|
||||
it { is_expected.to redirect_to siret_dossier_path(id: Dossier.last) }
|
||||
|
||||
context 'when prefill values contain a hash' do
|
||||
let(:value_2) { { evil: "payload" } }
|
||||
|
||||
it "prefills the dossier's champ with the hash stored as a string" do
|
||||
subject
|
||||
|
||||
dossier = Dossier.last
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).value).to eq("{\"evil\"=>\"payload\"}")
|
||||
end
|
||||
end
|
||||
|
||||
context 'when prefill values contain an array' do
|
||||
let(:value_2) { ["a", "b", "c"] }
|
||||
|
||||
it "prefills the dossier's champ with the array stored as a string" do
|
||||
subject
|
||||
|
||||
dossier = Dossier.last
|
||||
expect(find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).value).to eq("[\"a\", \"b\", \"c\"]")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
context 'when user is not logged' do
|
||||
it { is_expected.to have_http_status(302) }
|
||||
|
@ -1180,9 +1231,9 @@ describe Users::DossiersController, type: :controller do
|
|||
|
||||
context 'when not logged in' do
|
||||
it 'fails' do
|
||||
subject
|
||||
expect { expect(response).to redirect_to(new_user_session_path) }
|
||||
end
|
||||
subject
|
||||
expect { expect(response).to redirect_to(new_user_session_path) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1202,4 +1253,10 @@ describe Users::DossiersController, type: :controller do
|
|||
it { expect { subject }.to change { dossier.user.dossiers.count }.by(1) }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_champ_by_stable_id(dossier, stable_id)
|
||||
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||
end
|
||||
end
|
||||
|
|
64
spec/models/concern/dossier_prefillable_concern_spec.rb
Normal file
64
spec/models/concern/dossier_prefillable_concern_spec.rb
Normal file
|
@ -0,0 +1,64 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe DossierPrefillableConcern do
|
||||
describe '.prefill!' do
|
||||
let(:procedure) { create(:procedure, :published) }
|
||||
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
|
||||
|
||||
subject(:fill) { dossier.prefill!(values) }
|
||||
|
||||
context 'when champs_public_attributes is empty' do
|
||||
let(:values) { [] }
|
||||
|
||||
it "does nothing" do
|
||||
expect(dossier).not_to receive(:save)
|
||||
fill
|
||||
end
|
||||
end
|
||||
|
||||
context 'when champs_public_attributes has values' do
|
||||
context 'when the champs are valid' do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:value_1) { "any value" }
|
||||
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
||||
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_phone, procedure: procedure) }
|
||||
let(:value_2) { "33612345678" }
|
||||
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
||||
|
||||
let(:values) { [{ id: champ_id_1, value: value_1 }, { id: champ_id_2, value: value_2 }] }
|
||||
|
||||
it "updates the champs with the new values and mark them as prefilled" do
|
||||
fill
|
||||
|
||||
expect(dossier.champs_public.first.value).to eq(value_1)
|
||||
expect(dossier.champs_public.first.prefilled).to eq(true)
|
||||
expect(dossier.champs_public.last.value).to eq(value_2)
|
||||
expect(dossier.champs_public.last.prefilled).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a champ is invalid' do
|
||||
let!(:type_de_champ) { create(:type_de_champ_phone, procedure: procedure) }
|
||||
let(:value) { "a non phone value" }
|
||||
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
|
||||
|
||||
let(:values) { [{ id: champ_id, value: value }] }
|
||||
|
||||
it "still updates the champ" do
|
||||
expect { fill }.to change { dossier.champs_public.first.value }.from(nil).to(value)
|
||||
end
|
||||
|
||||
it "still marks it as prefilled" do
|
||||
expect { fill }.to change { dossier.champs_public.first.prefilled }.from(false).to(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_champ_by_stable_id(dossier, stable_id)
|
||||
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||
end
|
||||
end
|
|
@ -1855,6 +1855,41 @@ describe Dossier do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#find_champs_by_stable_ids' do
|
||||
let(:procedure) { create(:procedure, :published) }
|
||||
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
|
||||
|
||||
subject { dossier.find_champs_by_stable_ids(stable_ids) }
|
||||
|
||||
context 'when stable_ids is empty' do
|
||||
let(:stable_ids) { [] }
|
||||
|
||||
it { expect(subject).to match([]) }
|
||||
end
|
||||
|
||||
context 'when stable_ids contains nil or blank values' do
|
||||
let(:stable_ids) { [nil, ""] }
|
||||
|
||||
it { expect(subject).to match([]) }
|
||||
end
|
||||
|
||||
context 'when stable_ids contains present values' do
|
||||
context 'when the dossier has no champ with the given stable ids' do
|
||||
let(:stable_ids) { ['My Neighbor Totoro', 'Miyazaki'] }
|
||||
|
||||
it { expect(subject).to match([]) }
|
||||
end
|
||||
|
||||
context 'when the dossier has champs with the given stable ids' do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) }
|
||||
let(:stable_ids) { [type_de_champ_1.stable_id, type_de_champ_2.stable_id] }
|
||||
|
||||
it { expect(subject).to match(dossier.champs_public.joins(:type_de_champ).where(types_de_champ: { stable_id: stable_ids })) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'BatchOperation' do
|
||||
subject { build(:dossier) }
|
||||
it { is_expected.to belong_to(:batch_operation).optional }
|
||||
|
|
130
spec/models/prefill_params_spec.rb
Normal file
130
spec/models/prefill_params_spec.rb
Normal file
|
@ -0,0 +1,130 @@
|
|||
RSpec.describe PrefillParams do
|
||||
describe "#to_a" do
|
||||
let(:procedure) { create(:procedure, :published) }
|
||||
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
|
||||
|
||||
subject(:prefill_params_array) { described_class.new(dossier, ActionController::Parameters.new(params)).to_a }
|
||||
|
||||
context "when the stable ids match the TypeDeChamp of the corresponding procedure" do
|
||||
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:value_1) { "any value" }
|
||||
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
||||
|
||||
let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) }
|
||||
let(:value_2) { "another value" }
|
||||
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
||||
|
||||
let(:params) {
|
||||
{
|
||||
"champ_#{type_de_champ_1.to_typed_id}" => value_1,
|
||||
"champ_#{type_de_champ_2.to_typed_id}" => value_2
|
||||
}
|
||||
}
|
||||
|
||||
it "builds an array of hash(id, value) matching all the given params" do
|
||||
expect(prefill_params_array).to match([
|
||||
{ id: champ_id_1, value: value_1 },
|
||||
{ id: champ_id_2, value: value_2 }
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
context "when the typed id is not prefixed by 'champ_'" do
|
||||
let!(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
|
||||
|
||||
let(:params) { { type_de_champ.to_typed_id => "value" } }
|
||||
|
||||
it "filters out the champ" do
|
||||
expect(prefill_params_array).to match([])
|
||||
end
|
||||
end
|
||||
|
||||
context "when the typed id is unknown" do
|
||||
let(:params) { { "champ_jane_doe" => "value" } }
|
||||
|
||||
it "filters out the unknown params" do
|
||||
expect(prefill_params_array).to match([])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is no Champ that matches the TypeDeChamp with the given stable id' do
|
||||
let!(:type_de_champ) { create(:type_de_champ_text) } # goes to another procedure
|
||||
|
||||
let(:params) { { "champ_#{type_de_champ.to_typed_id}" => "value" } }
|
||||
|
||||
it "filters out the param" do
|
||||
expect(prefill_params_array).to match([])
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "a champ public value that is authorized" do |type_de_champ_name, value|
|
||||
context "when the type de champ is authorized (#{type_de_champ_name})" do
|
||||
let!(:type_de_champ) { create(type_de_champ_name, procedure: procedure) }
|
||||
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
|
||||
|
||||
let(:params) { { "champ_#{type_de_champ.to_typed_id}" => value } }
|
||||
|
||||
it "builds an array of hash(id, value) matching the given params" do
|
||||
expect(prefill_params_array).to match([{ id: champ_id, value: value }])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "a champ public value that is unauthorized" do |type_de_champ_name, value|
|
||||
let!(:type_de_champ) { create(type_de_champ_name, procedure: procedure) }
|
||||
|
||||
let(:params) { { "champ_#{type_de_champ.to_typed_id}" => value } }
|
||||
|
||||
context 'when the type de champ is unauthorized (type_de_champ_name)' do
|
||||
it "filters out the param" do
|
||||
expect(prefill_params_array).to match([])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it_behaves_like "a champ public value that is authorized", :type_de_champ_text, "value"
|
||||
it_behaves_like "a champ public value that is authorized", :type_de_champ_textarea, "value"
|
||||
it_behaves_like "a champ public value that is authorized", :type_de_champ_decimal_number, "3.14"
|
||||
it_behaves_like "a champ public value that is authorized", :type_de_champ_integer_number, "42"
|
||||
it_behaves_like "a champ public value that is authorized", :type_de_champ_email, "value"
|
||||
it_behaves_like "a champ public value that is authorized", :type_de_champ_phone, "value"
|
||||
it_behaves_like "a champ public value that is authorized", :type_de_champ_iban, "value"
|
||||
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_decimal_number, "non decimal string"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_integer_number, "non integer string"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_number, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_communes, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_dossier_link, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_titre_identite, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_checkbox, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_civilite, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_yes_no, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_date, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_datetime, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_drop_down_list, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_multiple_drop_down_list, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_linked_drop_down_list, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_header_section, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_explication, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_piece_justificative, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_repetition, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_cnaf, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_dgfip, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_pole_emploi, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_mesri, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_carte, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_address, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_pays, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_regions, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_departements, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_siret, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_rna, "value"
|
||||
it_behaves_like "a champ public value that is unauthorized", :type_de_champ_annuaire_education, "value"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_champ_by_stable_id(dossier, stable_id)
|
||||
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||
end
|
||||
end
|
70
spec/system/users/dossier_prefill_spec.rb
Normal file
70
spec/system/users/dossier_prefill_spec.rb
Normal file
|
@ -0,0 +1,70 @@
|
|||
describe 'Prefilling a dossier:' do
|
||||
let(:siret) { '41816609600051' }
|
||||
|
||||
let(:procedure) { create(:procedure, :published, opendata: true) }
|
||||
let(:dossier) { procedure.dossiers.last }
|
||||
|
||||
let(:type_de_champ_text) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:type_de_champ_phone) { create(:type_de_champ_phone, procedure: procedure) }
|
||||
let(:text_value) { "My Neighbor Totoro is the best movie ever" }
|
||||
let(:phone_value) { "invalid phone value" }
|
||||
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/)
|
||||
.to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/etablissements.json'))
|
||||
|
||||
visit new_dossier_path(
|
||||
procedure_id: procedure.id,
|
||||
"champ_#{type_de_champ_text.to_typed_id}" => text_value,
|
||||
"champ_#{type_de_champ_phone.to_typed_id}" => phone_value
|
||||
)
|
||||
end
|
||||
|
||||
context 'when the user already exists' do
|
||||
let(:password) { 'my-s3cure-p4ssword' }
|
||||
let!(:user) { create(:user, password: password) }
|
||||
|
||||
scenario "the user has got a prefilled dossier after signing in" do
|
||||
expect(page).to have_content("Connectez-vous")
|
||||
sign_in_with user.email, password
|
||||
|
||||
expect(page).to have_current_path siret_dossier_path(procedure.dossiers.last)
|
||||
fill_in 'Numéro SIRET', with: siret
|
||||
click_on 'Valider'
|
||||
|
||||
expect(page).to have_current_path(etablissement_dossier_path(dossier))
|
||||
expect(page).to have_content('OCTO TECHNOLOGY')
|
||||
click_on 'Continuer avec ces informations'
|
||||
|
||||
expect(page).to have_current_path(brouillon_dossier_path(dossier))
|
||||
expect(page).to have_field(type_de_champ_text.libelle, with: text_value)
|
||||
expect(page).to have_field(type_de_champ_phone.libelle, with: phone_value)
|
||||
expect(page).to have_css('.field_with_errors', text: type_de_champ_phone.libelle)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when this is a new user' do
|
||||
before do
|
||||
allow_any_instance_of(FranceConnectParticulierClient).to receive(:authorization_uri).and_return(france_connect_particulier_callback_path(code: "c0d3"))
|
||||
allow(FranceConnectService).to receive(:retrieve_user_informations_particulier).and_return(build(:france_connect_information))
|
||||
end
|
||||
|
||||
scenario "the user has got a prefilled dossier after signing up" do
|
||||
expect(page).to have_content("Connectez-vous")
|
||||
page.find('.fr-connect').click
|
||||
|
||||
expect(page).to have_current_path siret_dossier_path(procedure.dossiers.last)
|
||||
fill_in 'Numéro SIRET', with: siret
|
||||
click_on 'Valider'
|
||||
|
||||
expect(page).to have_current_path(etablissement_dossier_path(dossier))
|
||||
expect(page).to have_content('OCTO TECHNOLOGY')
|
||||
click_on 'Continuer avec ces informations'
|
||||
|
||||
expect(page).to have_current_path(brouillon_dossier_path(dossier))
|
||||
expect(page).to have_field(type_de_champ_text.libelle, with: text_value)
|
||||
expect(page).to have_field(type_de_champ_phone.libelle, with: phone_value)
|
||||
expect(page).to have_css('.field_with_errors', text: type_de_champ_phone.libelle)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue