b8ff0ba4a2
* store prefill params in session Instead of using query params on /dossier/new, we assume the user comes from /commencer/:path, which is the new prefill link. There, we store the prefill params in session, and use them to prefill the dossier when creating it, in /dossiers/new. * spec: cover the case * review: serialize with json instead of yaml * review: rename method * review: store only query params * review: comment why we dont override already stored params
80 lines
1.9 KiB
Ruby
80 lines
1.9 KiB
Ruby
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
|
|
.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
|