2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2023-03-02 01:14:34 +01:00
|
|
|
attr_reader :identity_items_selected
|
2022-12-19 12:32:09 +01:00
|
|
|
|
|
|
|
def initialize(procedure)
|
|
|
|
super(procedure)
|
|
|
|
@selected_type_de_champ_ids = []
|
2023-03-02 01:14:34 +01:00
|
|
|
@identity_items_selected = []
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(attributes)
|
2023-02-13 09:17:22 +01:00
|
|
|
@selected_type_de_champ_ids = attributes[:selected_type_de_champ_ids]&.split(' ') || []
|
2023-03-02 01:14:34 +01:00
|
|
|
@identity_items_selected = attributes[:identity_items_selected]&.split(' ') || []
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def types_de_champ
|
2023-02-21 16:00:58 +01:00
|
|
|
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision.types_de_champ_public.fillable.partition(&:prefillable?).flatten, active_revision)
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
2023-03-02 01:14:34 +01:00
|
|
|
def include?(entity)
|
|
|
|
selected_type_de_champ_ids.include?(entity) || identity_items_selected.include?(entity)
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_too_long?
|
|
|
|
prefill_link.length > MAX_PREFILL_LINK_LENGTH
|
|
|
|
end
|
|
|
|
|
|
|
|
def prefill_link
|
2023-03-02 01:14:34 +01:00
|
|
|
@prefill_link ||= CGI.unescape(commencer_url({ path: path }.merge(prefilled_champs_as_params).merge(prefilled_identity_as_params)))
|
2022-12-19 12:32:09 +01:00
|
|
|
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' \\
|
2023-03-02 01:14:34 +01:00
|
|
|
--data '#{prefilled_identity_as_params.merge(prefilled_champs_as_params).to_json}'
|
2023-01-04 08:31:31 +01:00
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
2022-12-19 12:32:09 +01:00
|
|
|
def prefilled_champs
|
2023-02-21 16:00:58 +01:00
|
|
|
@prefilled_champs ||= TypesDeChamp::PrefillTypeDeChamp.wrap(active_fillable_public_types_de_champ.where(id: selected_type_de_champ_ids), active_revision)
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-02-15 17:39:19 +01:00
|
|
|
def active_fillable_public_types_de_champ
|
|
|
|
active_revision.types_de_champ_public.fillable
|
|
|
|
end
|
|
|
|
|
|
|
|
def prefilled_champs_as_params
|
2023-02-22 19:32:25 +01:00
|
|
|
prefilled_champs.map { |type_de_champ| ["champ_#{type_de_champ.to_typed_id_for_query}", type_de_champ.example_value] }.to_h
|
2022-12-20 14:37:25 +01:00
|
|
|
end
|
2023-03-02 01:14:34 +01:00
|
|
|
|
|
|
|
def prefilled_identity_as_params
|
|
|
|
identity_items_selected.map { |item| ["identite_#{item}", I18n.t("views.prefill_descriptions.edit.examples.#{item}")] }.to_h
|
|
|
|
end
|
2022-12-19 12:32:09 +01:00
|
|
|
end
|