Merge pull request #9099 from demarches-simplifiees/8472-migrate-exercices

API Entreprise : migration "exercices"
This commit is contained in:
Colin Darie 2023-05-30 08:49:57 +00:00 committed by GitHub
commit 947ce7a859
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 37 deletions

View file

@ -3,7 +3,7 @@ class APIEntreprise::API
ETABLISSEMENT_RESOURCE_NAME = "v2/etablissements/%{id}" ETABLISSEMENT_RESOURCE_NAME = "v2/etablissements/%{id}"
EXTRAIT_KBIS_NAME = "v3/infogreffe/rcs/unites_legales/%{id}/extrait_kbis" EXTRAIT_KBIS_NAME = "v3/infogreffe/rcs/unites_legales/%{id}/extrait_kbis"
TVA_NAME = "v3/european_commission/unites_legales/%{id}/numero_tva" 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}" RNA_RESOURCE_NAME = "v2/associations/%{id}"
EFFECTIFS_RESOURCE_NAME = "v2/effectifs_mensuels_acoss_covid" EFFECTIFS_RESOURCE_NAME = "v2/effectifs_mensuels_acoss_covid"
EFFECTIFS_ANNUELS_RESOURCE_NAME = "v2/effectifs_annuels_acoss_covid/%{id}" EFFECTIFS_ANNUELS_RESOURCE_NAME = "v2/effectifs_annuels_acoss_covid/%{id}"

View file

@ -1,4 +1,7 @@
class APIEntreprise::ExercicesAdapter < APIEntreprise::Adapter 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 private
def get_resource def get_resource
@ -6,22 +9,25 @@ class APIEntreprise::ExercicesAdapter < APIEntreprise::Adapter
end end
def process_params def process_params
exercices_array = data_source[:exercices].map do |exercice| data = data_source[:data]
exercice.slice(*attr_to_fetch) Sentry.with_scope do |scope|
end scope.set_tags(siret: @siret)
scope.set_extras(source: data)
if exercices_array.all? { |params| valid_params?(params) } if data
{ exercices_attributes: exercices_array } exercices_array = data.map do |exercice|
else {
{} ca: exercice[:data][:chiffre_affaires].to_s,
end date_fin_exercice: Date.parse(exercice[:data][:date_fin_exercice])
end }
end
def attr_to_fetch if exercices_array.all? { |params| valid_params?(params) }
[ { exercices_attributes: exercices_array }
:ca, else
:date_fin_exercice, {}
:date_fin_exercice_timestamp end
] end
end
end end
end end

View file

@ -1,19 +1,22 @@
{ {
"exercices":[ "data": [
{ {
"ca":"21009417", "data": {
"date_fin_exercice":"2013-12-31T00:00:00+01:00", "chiffre_affaires": 900001,
"date_fin_exercice_timestamp": 1388444400 "date_fin_exercice": "2015-12-01"
},
"links": {},
"meta": {}
}, },
{ {
"ca":"18968298", "data": {
"date_fin_exercice":"2012-12-31T00:00:00+01:00", "chiffre_affaires": 1900051,
"date_fin_exercice_timestamp": 1356908400 "date_fin_exercice": "2016-12-01"
}, },
{ "links": {},
"ca":"17768838", "meta": {}
"date_fin_exercice":"2011-12-31T00:00:00+01:00",
"date_fin_exercice_timestamp": 1325286000
} }
] ],
"meta": {},
"links": {}
} }

View file

@ -6,7 +6,7 @@ RSpec.describe APIEntreprise::ExercicesJob, type: :job do
let(:status) { 200 } let(:status) { 200 }
before 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(body: body, status: status) .to_return(body: body, status: status)
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
end end
@ -16,6 +16,6 @@ RSpec.describe APIEntreprise::ExercicesJob, type: :job do
it 'updates etablissement' do it 'updates etablissement' do
subject subject
ca_list = Etablissement.find(etablissement.id).exercices.map(&:ca) 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
end end

View file

@ -115,7 +115,7 @@ describe APIEntreprise::API do
describe '.exercices' do describe '.exercices' do
before 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) .to_return(status: status, body: body)
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
end end

View file

@ -4,7 +4,7 @@ describe APIEntreprise::ExercicesAdapter do
subject { described_class.new(siret, procedure.id).to_params } subject { described_class.new(siret, procedure.id).to_params }
before 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(body: File.read('spec/fixtures/files/api_entreprise/exercices.json', status: 200)) .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) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
end end
@ -12,12 +12,11 @@ describe APIEntreprise::ExercicesAdapter do
it { is_expected.to be_an_instance_of(Hash) } it { is_expected.to be_an_instance_of(Hash) }
it 'contains several exercices attributes' do it 'contains several exercices attributes' do
expect(subject[:exercices_attributes].size).to eq(3) expect(subject[:exercices_attributes].size).to eq(2)
end end
it 'contains informations in each exercices_attributes' do it 'contains informations in each exercices_attributes' do
expect(subject[:exercices_attributes][0][:ca]).to eq('21009417') expect(subject[:exercices_attributes][0][:ca]).to eq('900001')
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].year).to eq(2015)
expect(subject[:exercices_attributes][0][:date_fin_exercice_timestamp]).to eq(1388444400)
end end
end end