2023-09-07 10:03:17 +02:00
|
|
|
class PrefillChamps
|
2023-02-21 16:00:58 +01:00
|
|
|
attr_reader :dossier, :params
|
|
|
|
|
2022-11-28 15:42:58 +01:00
|
|
|
def initialize(dossier, params)
|
|
|
|
@dossier = dossier
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
2023-02-12 11:55:28 +01:00
|
|
|
build_prefill_values.filter(&:prefillable?).map(&:champ_attributes).flatten
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
|
2023-06-12 12:05:48 +02:00
|
|
|
def self.digest(params)
|
|
|
|
Digest::SHA256.hexdigest(params.reject { |(key, _)| key.split('_').first != "champ" }.to_json)
|
|
|
|
end
|
|
|
|
|
2022-11-28 15:42:58 +01:00
|
|
|
private
|
|
|
|
|
2022-12-01 15:05:23 +01:00
|
|
|
def build_prefill_values
|
2023-02-21 16:00:58 +01:00
|
|
|
value_by_stable_id = params
|
2023-02-23 11:00:09 +01:00
|
|
|
.map { |prefixed_typed_id, value| [Champ.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
|
|
|
|
2023-02-21 16:00:58 +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]] }
|
2023-02-21 16:00:58 +01:00
|
|
|
.map { |champ, value| PrefillValue.new(champ:, value:, dossier:) }
|
2022-12-01 11:17:48 +01:00
|
|
|
end
|
|
|
|
|
2022-11-28 15:42:58 +01:00
|
|
|
class PrefillValue
|
|
|
|
NEED_VALIDATION_TYPES_DE_CHAMPS = [
|
|
|
|
TypeDeChamp.type_champs.fetch(:decimal_number),
|
2022-12-19 22:00:22 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:integer_number),
|
2023-01-12 17:42:02 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:date),
|
|
|
|
TypeDeChamp.type_champs.fetch(:datetime),
|
2023-01-06 14:01:59 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:civilite),
|
|
|
|
TypeDeChamp.type_champs.fetch(:yes_no),
|
2023-01-18 12:52:38 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:checkbox),
|
2023-01-24 12:25:17 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:pays),
|
2023-01-20 11:23:52 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:regions),
|
2023-02-15 17:39:19 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:departements),
|
2023-01-24 15:10:25 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:multiple_drop_down_list),
|
2023-02-22 15:56:31 +01:00
|
|
|
TypeDeChamp.type_champs.fetch(:epci),
|
|
|
|
TypeDeChamp.type_champs.fetch(:dossier_link)
|
2022-11-28 15:42:58 +01:00
|
|
|
]
|
|
|
|
|
2023-02-21 16:00:58 +01:00
|
|
|
attr_reader :champ, :value, :dossier
|
2022-11-28 15:42:58 +01:00
|
|
|
|
2023-02-21 16:00:58 +01:00
|
|
|
def initialize(champ:, value:, dossier:)
|
2022-12-01 11:17:48 +01:00
|
|
|
@champ = champ
|
2022-11-28 15:42:58 +01:00
|
|
|
@value = value
|
2023-02-21 16:00:58 +01:00
|
|
|
@dossier = dossier
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def prefillable?
|
2023-02-22 20:05:36 +01:00
|
|
|
champ.prefillable? && valid? && champ_attributes.present?
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
|
2023-02-15 18:13:16 +01:00
|
|
|
def champ_attributes
|
2023-02-22 20:05:36 +01:00
|
|
|
@champ_attributes ||= TypesDeChamp::PrefillTypeDeChamp
|
2023-02-21 16:00:58 +01:00
|
|
|
.build(champ.type_de_champ, dossier.revision)
|
2023-02-15 18:13:16 +01:00
|
|
|
.to_assignable_attributes(champ, value)
|
|
|
|
end
|
|
|
|
|
2022-11-28 15:42:58 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
return true unless NEED_VALIDATION_TYPES_DE_CHAMPS.include?(champ.type_champ)
|
|
|
|
|
2023-02-12 11:55:28 +01:00
|
|
|
champ.assign_attributes(champ_attributes)
|
2023-01-12 17:42:02 +01:00
|
|
|
champ.valid?(:prefill)
|
2022-11-28 15:42:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|