2021-09-24 14:21:30 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: champs
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# data :jsonb
|
|
|
|
# fetch_external_data_exceptions :string is an Array
|
2022-12-01 12:00:21 +01:00
|
|
|
# prefilled :boolean default(FALSE)
|
2021-09-24 14:21:30 +02:00
|
|
|
# private :boolean default(FALSE), not null
|
2022-02-04 14:40:16 +01:00
|
|
|
# rebased_at :datetime
|
2021-09-24 14:21:30 +02:00
|
|
|
# type :string
|
|
|
|
# value :string
|
|
|
|
# value_json :jsonb
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2022-09-21 15:20:42 +02:00
|
|
|
# dossier_id :integer
|
2021-09-24 14:21:30 +02:00
|
|
|
# etablissement_id :integer
|
|
|
|
# external_id :string
|
|
|
|
# parent_id :bigint
|
2022-12-16 12:39:51 +01:00
|
|
|
# row_id :string
|
2022-09-21 15:20:42 +02:00
|
|
|
# type_de_champ_id :integer
|
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
|
2021-10-06 12:03:12 +02:00
|
|
|
validates :numero_allocataire, format: { with: /\A\d{1,7}\z/ }, if: -> { code_postal.present? && validation_context != :brouillon }
|
|
|
|
validates :code_postal, format: { with: /\A\w{5}\z/ }, if: -> { numero_allocataire.present? && validation_context != :brouillon }
|
2021-10-05 14:03:30 +02:00
|
|
|
|
2021-09-24 14:21:30 +02:00
|
|
|
store_accessor :value_json, :numero_allocataire, :code_postal
|
|
|
|
|
2021-09-21 14:49:54 +02:00
|
|
|
def blank?
|
|
|
|
external_id.nil?
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:21:30 +02:00
|
|
|
def fetch_external_data?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_external_data
|
2021-10-06 12:03:12 +02:00
|
|
|
if valid?
|
|
|
|
APIParticulier::CnafAdapter.new(
|
|
|
|
procedure.api_particulier_token,
|
|
|
|
numero_allocataire,
|
|
|
|
code_postal,
|
|
|
|
procedure.api_particulier_sources
|
|
|
|
).to_params
|
|
|
|
end
|
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
|
|
|
|
end
|