2022-11-28 15:42:58 +01:00
|
|
|
class PrefillParams
|
|
|
|
def initialize(dossier, params)
|
|
|
|
@dossier = dossier
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
2022-12-01 15:05:23 +01:00
|
|
|
build_prefill_values.filter(&:prefillable?).map(&:to_h)
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-12-01 15:05:23 +01:00
|
|
|
def build_prefill_values
|
|
|
|
# Builds: [{ stable_id: 1, value: "a value" }, { stable_id: 2, value: "another value"}]
|
|
|
|
stable_ids_and_values =
|
|
|
|
@params
|
|
|
|
.to_unsafe_hash
|
|
|
|
.map { |typed_id, value| { stable_id: stable_id_from_typed_id(typed_id), value: value } }
|
|
|
|
.filter { _1[:stable_id].present? && _1[:value].present? }
|
2022-12-01 11:17:48 +01:00
|
|
|
|
|
|
|
@dossier
|
2022-12-01 15:05:23 +01:00
|
|
|
.find_champs_by_stable_ids(stable_ids_and_values.map { _1[:stable_id] })
|
|
|
|
.map do |champ|
|
|
|
|
value = stable_ids_and_values.find { _1[:stable_id] == champ.stable_id }[:value]
|
2022-12-01 11:17:48 +01:00
|
|
|
|
2022-12-01 15:05:23 +01:00
|
|
|
PrefillValue.new(champ: champ, value: value)
|
|
|
|
end
|
2022-12-01 11:17:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def stable_id_from_typed_id(typed_id)
|
2022-12-01 15:05:23 +01:00
|
|
|
Champ.id_from_typed_id(typed_id).to_i
|
2022-12-01 11:17:48 +01:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-11-28 15:42:58 +01:00
|
|
|
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(:address),
|
|
|
|
TypeDeChamp.type_champs.fetch(:pays),
|
|
|
|
TypeDeChamp.type_champs.fetch(:regions),
|
|
|
|
TypeDeChamp.type_champs.fetch(:departements),
|
|
|
|
TypeDeChamp.type_champs.fetch(:siret),
|
|
|
|
TypeDeChamp.type_champs.fetch(:rna),
|
|
|
|
TypeDeChamp.type_champs.fetch(:iban),
|
|
|
|
TypeDeChamp.type_champs.fetch(:annuaire_education)
|
|
|
|
]
|
|
|
|
|
|
|
|
NEED_VALIDATION_TYPES_DE_CHAMPS = [
|
|
|
|
TypeDeChamp.type_champs.fetch(:decimal_number),
|
|
|
|
TypeDeChamp.type_champs.fetch(:integer_number)
|
|
|
|
]
|
|
|
|
|
|
|
|
attr_reader :champ, :value
|
|
|
|
|
2022-12-01 11:17:48 +01:00
|
|
|
def initialize(champ:, value:)
|
|
|
|
@champ = champ
|
2022-11-28 15:42:58 +01:00
|
|
|
@value = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def prefillable?
|
|
|
|
exists? && authorized? && valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
{
|
|
|
|
id: champ.id,
|
|
|
|
value: value
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
champ.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|