2022-12-19 12:32:09 +01:00
|
|
|
class PrefillDescription < SimpleDelegator
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
|
|
|
MAX_PREFILL_LINK_LENGTH = 2000
|
|
|
|
|
|
|
|
attr_reader :selected_type_de_champ_ids
|
|
|
|
|
|
|
|
def initialize(procedure)
|
|
|
|
super(procedure)
|
|
|
|
@selected_type_de_champ_ids = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(attributes)
|
|
|
|
@selected_type_de_champ_ids = attributes[:selected_type_de_champ_ids].presence || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def types_de_champ
|
2023-01-18 09:47:22 +01:00
|
|
|
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision.types_de_champ_public.fillable.partition(&:prefillable?).flatten)
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def include?(type_de_champ_id)
|
|
|
|
selected_type_de_champ_ids.include?(type_de_champ_id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_too_long?
|
|
|
|
prefill_link.length > MAX_PREFILL_LINK_LENGTH
|
|
|
|
end
|
|
|
|
|
|
|
|
def prefill_link
|
|
|
|
@prefill_link ||= commencer_url({ path: path }.merge(prefilled_champs_for_link))
|
|
|
|
end
|
|
|
|
|
2023-01-04 08:31:31 +01:00
|
|
|
def prefill_query
|
|
|
|
@prefill_query ||=
|
|
|
|
<<~TEXT
|
|
|
|
curl --request POST '#{api_public_v1_dossiers_url(self)}' \\
|
|
|
|
--header 'Content-Type: application/json' \\
|
|
|
|
--data '{#{prefilled_champs_for_query}}'
|
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
2022-12-19 12:32:09 +01:00
|
|
|
def prefilled_champs
|
2023-01-18 09:47:22 +01:00
|
|
|
@prefilled_champs ||= TypesDeChamp::PrefillTypeDeChamp.wrap(active_fillable_public_types_de_champ.where(id: selected_type_de_champ_ids))
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prefilled_champs_for_link
|
2023-01-18 09:47:22 +01:00
|
|
|
prefilled_champs.map { |type_de_champ| ["champ_#{type_de_champ.to_typed_id}", type_de_champ.example_value] }.to_h
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def prefilled_champs_for_query
|
2023-01-31 14:54:23 +01:00
|
|
|
prefilled_champs.map do |type_de_champ|
|
2023-02-12 11:55:28 +01:00
|
|
|
"\"champ_#{type_de_champ.to_typed_id}\": #{type_de_champ.formatted_example_value}"
|
2023-01-31 14:54:23 +01:00
|
|
|
end.join(', ')
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
2022-12-20 14:37:25 +01:00
|
|
|
|
|
|
|
def active_fillable_public_types_de_champ
|
|
|
|
active_revision.types_de_champ_public.fillable
|
|
|
|
end
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|