2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-30 14:42:36 +02:00
|
|
|
class Champs::COJOChamp < Champ
|
|
|
|
store_accessor :value_json, :accreditation_number, :accreditation_birthdate
|
|
|
|
store_accessor :data, :accreditation_success, :accreditation_first_name, :accreditation_last_name
|
|
|
|
|
|
|
|
after_validation :update_external_id
|
|
|
|
|
|
|
|
def accreditation_birthdate
|
|
|
|
Date.parse(super)
|
|
|
|
rescue ArgumentError, TypeError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def accreditation_success?
|
|
|
|
accreditation_success == true
|
|
|
|
end
|
|
|
|
|
|
|
|
def accreditation_error?
|
|
|
|
accreditation_success == false
|
|
|
|
end
|
|
|
|
|
|
|
|
def blank?
|
2023-07-10 19:15:39 +02:00
|
|
|
accreditation_success != true
|
2023-05-30 14:42:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_external_data?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def poll_external_data?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_external_data
|
|
|
|
COJOService.new.(accreditation_number:, accreditation_birthdate:)
|
|
|
|
end
|
|
|
|
|
|
|
|
def accreditation_number_input_id
|
|
|
|
"#{input_id}-accreditation_number"
|
|
|
|
end
|
|
|
|
|
|
|
|
def accreditation_birthdate_input_id
|
|
|
|
"#{input_id}-accreditation_birthdate"
|
|
|
|
end
|
|
|
|
|
|
|
|
def focusable_input_id
|
|
|
|
accreditation_number_input_id
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_external_id
|
|
|
|
if accreditation_number_changed? || accreditation_birthdate_changed?
|
2024-07-09 12:48:53 +02:00
|
|
|
if accreditation_number.present? && accreditation_birthdate.present? && /\A[\d-]+\z/.match?(accreditation_number)
|
2023-05-30 14:42:36 +02:00
|
|
|
self.external_id = { accreditation_number:, accreditation_birthdate: }.to_json
|
|
|
|
else
|
|
|
|
self.external_id = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|