diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index e70b65282..41ae7692c 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -3,7 +3,7 @@ class APIEntreprise::API ETABLISSEMENT_RESOURCE_NAME = "v2/etablissements/%{id}" EXTRAIT_KBIS_NAME = "v3/infogreffe/rcs/unites_legales/%{id}/extrait_kbis" TVA_NAME = "v3/european_commission/unites_legales/%{id}/numero_tva" - EXERCICES_RESOURCE_NAME = "v2/exercices/%{id}" + EXERCICES_RESOURCE_NAME = "v3/dgfip/etablissements/%{id}/chiffres_affaires" RNA_RESOURCE_NAME = "v2/associations/%{id}" EFFECTIFS_RESOURCE_NAME = "v2/effectifs_mensuels_acoss_covid" EFFECTIFS_ANNUELS_RESOURCE_NAME = "v2/effectifs_annuels_acoss_covid/%{id}" diff --git a/app/lib/api_entreprise/exercices_adapter.rb b/app/lib/api_entreprise/exercices_adapter.rb index f983e069d..e34d4200c 100644 --- a/app/lib/api_entreprise/exercices_adapter.rb +++ b/app/lib/api_entreprise/exercices_adapter.rb @@ -1,4 +1,7 @@ class APIEntreprise::ExercicesAdapter < APIEntreprise::Adapter + # Doc métier : https://entreprise.api.gouv.fr/catalogue/dgfip/chiffres_affaires + # Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Informations-financieres/paths/~1v3~1dgfip~1etablissements~1%7Bsiret%7D~1chiffres_affaires/get + private def get_resource @@ -6,22 +9,25 @@ class APIEntreprise::ExercicesAdapter < APIEntreprise::Adapter end def process_params - exercices_array = data_source[:exercices].map do |exercice| - exercice.slice(*attr_to_fetch) - end + data = data_source[:data] + Sentry.with_scope do |scope| + scope.set_tags(siret: @siret) + scope.set_extras(source: data) - if exercices_array.all? { |params| valid_params?(params) } - { exercices_attributes: exercices_array } - else - {} - end - end + if data + exercices_array = data.map do |exercice| + { + ca: exercice[:data][:chiffre_affaires].to_s, + date_fin_exercice: Date.parse(exercice[:data][:date_fin_exercice]) + } + end - def attr_to_fetch - [ - :ca, - :date_fin_exercice, - :date_fin_exercice_timestamp - ] + if exercices_array.all? { |params| valid_params?(params) } + { exercices_attributes: exercices_array } + else + {} + end + end + end end end diff --git a/spec/fixtures/files/api_entreprise/exercices.json b/spec/fixtures/files/api_entreprise/exercices.json index f6bc58504..dc9bbb3b3 100644 --- a/spec/fixtures/files/api_entreprise/exercices.json +++ b/spec/fixtures/files/api_entreprise/exercices.json @@ -1,19 +1,22 @@ { - "exercices":[ + "data": [ { - "ca":"21009417", - "date_fin_exercice":"2013-12-31T00:00:00+01:00", - "date_fin_exercice_timestamp": 1388444400 + "data": { + "chiffre_affaires": 900001, + "date_fin_exercice": "2015-12-01" + }, + "links": {}, + "meta": {} }, { - "ca":"18968298", - "date_fin_exercice":"2012-12-31T00:00:00+01:00", - "date_fin_exercice_timestamp": 1356908400 - }, - { - "ca":"17768838", - "date_fin_exercice":"2011-12-31T00:00:00+01:00", - "date_fin_exercice_timestamp": 1325286000 + "data": { + "chiffre_affaires": 1900051, + "date_fin_exercice": "2016-12-01" + }, + "links": {}, + "meta": {} } - ] + ], + "meta": {}, + "links": {} } diff --git a/spec/jobs/api_entreprise/exercices_job_spec.rb b/spec/jobs/api_entreprise/exercices_job_spec.rb index db1359563..ac299308a 100644 --- a/spec/jobs/api_entreprise/exercices_job_spec.rb +++ b/spec/jobs/api_entreprise/exercices_job_spec.rb @@ -6,7 +6,7 @@ RSpec.describe APIEntreprise::ExercicesJob, type: :job do let(:status) { 200 } before do - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\//) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/dgfip\/etablissements\/#{siret}\/chiffres_affaires/) .to_return(body: body, status: status) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) end @@ -16,6 +16,6 @@ RSpec.describe APIEntreprise::ExercicesJob, type: :job do it 'updates etablissement' do subject ca_list = Etablissement.find(etablissement.id).exercices.map(&:ca) - expect(ca_list).to contain_exactly('21009417', '18968298', '17768838') + expect(ca_list).to contain_exactly('900001', '1900051') end end diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index f4fd189c8..f296e963b 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -115,7 +115,7 @@ describe APIEntreprise::API do describe '.exercices' do before do - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\//) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/dgfip\/etablissements\/#{siret}\/chiffres_affaires/) .to_return(status: status, body: body) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) end diff --git a/spec/lib/api_entreprise/exercices_adapter_spec.rb b/spec/lib/api_entreprise/exercices_adapter_spec.rb index 72ecf95eb..9cbd045d3 100644 --- a/spec/lib/api_entreprise/exercices_adapter_spec.rb +++ b/spec/lib/api_entreprise/exercices_adapter_spec.rb @@ -4,7 +4,7 @@ describe APIEntreprise::ExercicesAdapter do subject { described_class.new(siret, procedure.id).to_params } before do - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\//) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/dgfip\/etablissements\/#{siret}\/chiffres_affaires/) .to_return(body: File.read('spec/fixtures/files/api_entreprise/exercices.json', status: 200)) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) end @@ -12,12 +12,11 @@ describe APIEntreprise::ExercicesAdapter do it { is_expected.to be_an_instance_of(Hash) } it 'contains several exercices attributes' do - expect(subject[:exercices_attributes].size).to eq(3) + expect(subject[:exercices_attributes].size).to eq(2) end it 'contains informations in each exercices_attributes' do - expect(subject[:exercices_attributes][0][:ca]).to eq('21009417') - expect(subject[:exercices_attributes][0][:date_fin_exercice]).to eq("2013-12-31T00:00:00+01:00") - expect(subject[:exercices_attributes][0][:date_fin_exercice_timestamp]).to eq(1388444400) + expect(subject[:exercices_attributes][0][:ca]).to eq('900001') + expect(subject[:exercices_attributes][0][:date_fin_exercice].year).to eq(2015) end end