From 1302b68d91e3fc02bc772a566f270be1c4b8fb59 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 4 Oct 2018 16:22:19 +0200 Subject: [PATCH] Only reject api entreprise data if required fields are missing --- app/lib/api_entreprise/adapter.rb | 6 ++++++ app/lib/api_entreprise/api.rb | 5 +---- app/lib/api_entreprise/entreprise_adapter.rb | 9 +++++++-- .../api_entreprise/etablissement_adapter.rb | 13 +++++++++---- app/lib/api_entreprise/exercices_adapter.rb | 6 +++++- app/lib/api_entreprise/rna_adapter.rb | 19 ++++++++++++++----- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/app/lib/api_entreprise/adapter.rb b/app/lib/api_entreprise/adapter.rb index bf0645b0f..422509d88 100644 --- a/app/lib/api_entreprise/adapter.rb +++ b/app/lib/api_entreprise/adapter.rb @@ -1,4 +1,6 @@ class ApiEntreprise::Adapter + UNAVAILABLE = 'Donnée indisponible' + def initialize(siret, procedure_id) @siret = siret @procedure_id = procedure_id @@ -17,4 +19,8 @@ class ApiEntreprise::Adapter {} end end + + def valid_params?(params) + !params.has_value?(UNAVAILABLE) + end end diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index a722c29d7..0abc968da 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -34,10 +34,7 @@ class ApiEntreprise::API params: params, timeout: TIMEOUT) - # Responses with a 206 codes are sometimes not useable, - # as the RNA calls often return a 206 with an error message, - # not a partial response - if response.success? && response.code != 206 + if response.success? JSON.parse(response.body, symbolize_names: true) else raise RestClient::ResourceNotFound diff --git a/app/lib/api_entreprise/entreprise_adapter.rb b/app/lib/api_entreprise/entreprise_adapter.rb index b32305c47..0798a0d41 100644 --- a/app/lib/api_entreprise/entreprise_adapter.rb +++ b/app/lib/api_entreprise/entreprise_adapter.rb @@ -8,8 +8,13 @@ class ApiEntreprise::EntrepriseAdapter < ApiEntreprise::Adapter def process_params params = data_source[:entreprise].slice(*attr_to_fetch) - params[:date_creation] = Time.at(params[:date_creation]).to_datetime - params.transform_keys { |k| :"entreprise_#{k}" } + + if valid_params?(params) + params[:date_creation] = Time.at(params[:date_creation]).to_datetime + params.transform_keys { |k| :"entreprise_#{k}" } + else + {} + end end def attr_to_fetch diff --git a/app/lib/api_entreprise/etablissement_adapter.rb b/app/lib/api_entreprise/etablissement_adapter.rb index a1eacd7c6..58f63c689 100644 --- a/app/lib/api_entreprise/etablissement_adapter.rb +++ b/app/lib/api_entreprise/etablissement_adapter.rb @@ -7,10 +7,15 @@ class ApiEntreprise::EtablissementAdapter < ApiEntreprise::Adapter def process_params params = data_source[:etablissement].slice(*attr_to_fetch) - adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n") - params.merge!(params[:adresse].slice(*address_attr_to_fetch)) - params[:adresse] = adresse_line - params + + if valid_params?(params) + adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n") + params.merge!(params[:adresse].slice(*address_attr_to_fetch)) + params[:adresse] = adresse_line + params + else + {} + end end def attr_to_fetch diff --git a/app/lib/api_entreprise/exercices_adapter.rb b/app/lib/api_entreprise/exercices_adapter.rb index 4807e205f..e19d65dac 100644 --- a/app/lib/api_entreprise/exercices_adapter.rb +++ b/app/lib/api_entreprise/exercices_adapter.rb @@ -10,7 +10,11 @@ class ApiEntreprise::ExercicesAdapter < ApiEntreprise::Adapter exercice.slice(*attr_to_fetch) end - { exercices_attributes: exercices_array } + if exercices_array == exercices_array.select { |params| valid_params?(params) } + { exercices_attributes: exercices_array } + else + {} + end end def attr_to_fetch diff --git a/app/lib/api_entreprise/rna_adapter.rb b/app/lib/api_entreprise/rna_adapter.rb index 9d0612788..227549518 100644 --- a/app/lib/api_entreprise/rna_adapter.rb +++ b/app/lib/api_entreprise/rna_adapter.rb @@ -6,17 +6,26 @@ class ApiEntreprise::RNAAdapter < ApiEntreprise::Adapter end def process_params - if data_source[:association][:id].present? - params = data_source[:association].slice(*attr_to_fetch) - params[:rna] = data_source[:association][:id] - params.transform_keys { |k| :"association_#{k}" } - else + # Responses with a 206 codes are sometimes not useable, + # as the RNA calls often return a 206 with an error message, + # not a partial response + if !data_source.key?(:association) {} + else + params = data_source[:association].slice(*attr_to_fetch) + + if params[:id].present? && valid_params?(params) + params[:rna] = params[:id] + params.except(:id).transform_keys { |k| :"association_#{k}" } + else + {} + end end end def attr_to_fetch [ + :id, :titre, :objet, :date_creation,