2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-09-24 14:21:30 +02:00
|
|
|
class Champs::CnafChamp < Champs::TextChamp
|
2021-10-05 14:03:30 +02:00
|
|
|
# see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/cnaf-input-validation.middleware.ts
|
2024-04-02 11:04:00 +02:00
|
|
|
|
|
|
|
validates :numero_allocataire, format: { with: /\A\d{1,7}\z/ }, if: -> { code_postal.present? && validate_champ_value_or_prefill? }
|
|
|
|
validates :code_postal, format: { with: /\A\w{5}\z/ }, if: -> { numero_allocataire.present? && validate_champ_value_or_prefill? }
|
2021-10-05 14:03:30 +02:00
|
|
|
|
2021-09-24 14:21:30 +02:00
|
|
|
store_accessor :value_json, :numero_allocataire, :code_postal
|
|
|
|
|
|
|
|
def fetch_external_data?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_external_data
|
2023-10-26 16:02:23 +02:00
|
|
|
return unless valid_champ_value?
|
|
|
|
|
|
|
|
APIParticulier::CnafAdapter.new(
|
|
|
|
procedure.api_particulier_token,
|
|
|
|
numero_allocataire,
|
|
|
|
code_postal,
|
|
|
|
procedure.api_particulier_sources
|
|
|
|
).to_params
|
2021-09-24 14:21:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def external_id
|
|
|
|
if numero_allocataire.present? && code_postal.present?
|
|
|
|
{ code_postal: code_postal, numero_allocataire: numero_allocataire }.to_json
|
|
|
|
end
|
|
|
|
end
|
2023-02-27 15:33:25 +01:00
|
|
|
|
|
|
|
def numero_allocataire_input_id
|
|
|
|
"#{input_id}-numero_alocataire"
|
|
|
|
end
|
|
|
|
|
|
|
|
def code_postal_input_id
|
|
|
|
"#{input_id}-code_postal"
|
|
|
|
end
|
|
|
|
|
|
|
|
def focusable_input_id
|
|
|
|
numero_allocataire_input_id
|
|
|
|
end
|
2021-09-24 14:21:30 +02:00
|
|
|
end
|