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
|
2022-12-05 09:49:03 +01:00
|
|
|
value_by_stable_id = @params
|
2022-12-05 10:09:44 +01:00
|
|
|
.map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
|
2022-12-05 09:49:03 +01:00
|
|
|
.filter { |stable_id, value| stable_id.present? && value.present? }
|
|
|
|
.to_h
|
2022-12-01 11:17:48 +01:00
|
|
|
|
|
|
|
@dossier
|
2022-12-05 09:49:03 +01:00
|
|
|
.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:) }
|
2022-12-01 11:17:48 +01:00
|
|
|
end
|
|
|
|
|
2022-12-05 10:09:44 +01:00
|
|
|
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
|
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),
|
2022-12-05 10:59:43 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:iban)
|
2022-11-28 15:42:58 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
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?
|
2022-12-05 09:52:38 +01:00
|
|
|
authorized? && valid?
|
2022-11-28 15:42:58 +01:00
|
|
|
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
|