demarches-normaliennes/app/models/concerns/rna_champ_association_fetchable_concern.rb
2024-11-15 17:10:48 +01:00

33 lines
948 B
Ruby

# frozen_string_literal: true
module RNAChampAssociationFetchableConcern
extend ActiveSupport::Concern
attr_reader :association_fetch_error_key
def fetch_association!(rna)
self.value = rna
return clear_association!(:empty) if rna.empty?
return clear_association!(:invalid) unless valid_champ_value?
return clear_association!(:not_found) if (data = APIEntreprise::RNAAdapter.new(rna, procedure_id).to_params).blank?
update_with_external_data!(data:)
rescue APIEntreprise::API::Error, APIEntrepriseToken::TokenError => error
if APIEntrepriseService.service_unavailable_error?(error, target: :djepva)
clear_association!(:network_error)
else
Sentry.capture_exception(error, extra: { dossier_id:, rna: })
clear_association!(nil)
end
end
private
def clear_association!(error)
@association_fetch_error_key = error
self.data = nil
save(validate: false)
false
end
end