2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-13 13:47:17 +01:00
|
|
|
module RNAChampAssociationFetchableConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2023-01-14 11:53:55 +01:00
|
|
|
attr_reader :association_fetch_error_key
|
|
|
|
|
2023-01-13 13:47:17 +01:00
|
|
|
def fetch_association!(rna)
|
2023-01-13 14:52:16 +01:00
|
|
|
self.value = rna
|
2023-01-14 11:53:55 +01:00
|
|
|
|
|
|
|
return clear_association!(:empty) if rna.empty?
|
2024-04-02 11:04:00 +02:00
|
|
|
return clear_association!(:invalid) unless valid_champ_value?
|
2023-01-14 11:53:55 +01:00
|
|
|
return clear_association!(:not_found) if (data = APIEntreprise::RNAAdapter.new(rna, procedure_id).to_params).blank?
|
|
|
|
|
2024-11-15 09:33:42 +01:00
|
|
|
update_with_external_data!(data:)
|
2024-10-14 15:58:32 +02:00
|
|
|
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
|
2023-01-14 11:53:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def clear_association!(error)
|
|
|
|
@association_fetch_error_key = error
|
|
|
|
self.data = nil
|
2024-04-02 11:04:00 +02:00
|
|
|
save(validate: false)
|
2023-01-14 11:53:55 +01:00
|
|
|
false
|
2023-01-13 13:47:17 +01:00
|
|
|
end
|
|
|
|
end
|