diff --git a/app/jobs/api_entreprise/extrait_kbis_job.rb b/app/jobs/api_entreprise/extrait_kbis_job.rb new file mode 100644 index 000000000..45b80a486 --- /dev/null +++ b/app/jobs/api_entreprise/extrait_kbis_job.rb @@ -0,0 +1,7 @@ +class APIEntreprise::ExtraitKbisJob < APIEntreprise::Job + def perform(etablissement_id, procedure_id) + find_etablissement(etablissement_id) + etablissement_params = APIEntreprise::ExtraitKbisAdapter.new(etablissement.siret, procedure_id).to_params + etablissement.update!(etablissement_params) + end +end diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 8b9cd5d19..74d59fdff 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -1,6 +1,7 @@ class APIEntreprise::API ENTREPRISE_RESOURCE_NAME = "v3/insee/sirene/unites_legales/%{id}" ETABLISSEMENT_RESOURCE_NAME = "v2/etablissements/%{id}" + EXTRAIT_KBIS_NAME = "v3/infogreffe/rcs/unites_legales/%{id}/extrait_kbis" EXERCICES_RESOURCE_NAME = "v2/exercices/%{id}" RNA_RESOURCE_NAME = "v2/associations/%{id}" EFFECTIFS_RESOURCE_NAME = "v2/effectifs_mensuels_acoss_covid" @@ -32,6 +33,10 @@ class APIEntreprise::API call_with_siret(ETABLISSEMENT_RESOURCE_NAME, siret) end + def extrait_kbis(siren) + call_with_siret(EXTRAIT_KBIS_NAME, siren) + end + def exercices(siret) call_with_siret(EXERCICES_RESOURCE_NAME, siret) end diff --git a/app/lib/api_entreprise/extrait_kbis_adapter.rb b/app/lib/api_entreprise/extrait_kbis_adapter.rb new file mode 100644 index 000000000..72e12ca8c --- /dev/null +++ b/app/lib/api_entreprise/extrait_kbis_adapter.rb @@ -0,0 +1,17 @@ +class APIEntreprise::ExtraitKbisAdapter < APIEntreprise::Adapter + private + + def get_resource + api(@procedure_id).extrait_kbis(siren) + end + + def process_params + result = {} + data = data_source[:data] + if data + result[:entreprise_capital_social] = data[:capital][:montant] if data[:capital] + result[:entreprise_nom_commercial] = data[:nom_commercial] + end + result + end +end diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index 1999dc980..4be2aa943 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -47,7 +47,8 @@ class APIEntrepriseService def perform_later_fetch_jobs(etablissement, procedure_id, user_id, wait: nil) [ - APIEntreprise::EntrepriseJob, APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob, + APIEntreprise::EntrepriseJob, APIEntreprise::ExtraitKbisJob, + APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob, APIEntreprise::EffectifsJob, APIEntreprise::EffectifsAnnuelsJob, APIEntreprise::AttestationSocialeJob, APIEntreprise::BilansBdfJob ].each do |job| diff --git a/spec/fixtures/files/api_entreprise/extrait_kbis.json b/spec/fixtures/files/api_entreprise/extrait_kbis.json new file mode 100644 index 000000000..759abc1de --- /dev/null +++ b/spec/fixtures/files/api_entreprise/extrait_kbis.json @@ -0,0 +1,81 @@ +{ + "data": { + "siren": "130025265", + "date_radiation": "2019-01-01", + "date_extrait": "2022-07-11", + "date_immatriculation": "1998-07-12", + "mandataires_sociaux": [ + { + "numero_identification": "784824153", + "type": "personne_morale", + "fonction": "COMMISSAIRE AUX COMPTES SUPPLEANT", + "nom": "Henri", + "prenom": "Martin", + "date_naissance": "1965-01", + "raison_sociale": "BCRH & ASSOCIES - SOCIETE A RESPONSABILITE LIMITEE A ASSOCIE UNIQUE" + } + ], + "observations": [ + { + "numero": "123456", + "libelle": "LA SOCIETE NE CONSERVE AUCUNE ACTIVITE A SON ANCIEN SIEGE", + "date": "2000-01-01" + } + ], + "nom_commercial": "DECATHLON", + "etablissement_principal": { + "activite": "dummy", + "origine_fonds": "CREATION", + "mode_exploitation": "EXPLOITATION DIRECTE", + "code_ape": "6420Z" + }, + "capital": { + "montant": 50123.6, + "devise": "EUROS", + "code_devise": "EUR" + }, + "greffe": { + "valeur": "COLMAR", + "code": "6851" + }, + "personne_physique": { + "adresse": { + "nom_postal": "22 RUE DE LA PAIX", + "numero": "22", + "type": "RUE", + "voie": "DE LA PAIX", + "ligne_1": "dummy", + "ligne_2": "dummy", + "localite": "dummy", + "code_postal": "75016\"", + "bureau_distributeur": "dummy", + "pays": "FRANCE" + }, + "nationalite": { + "valeur": "FRANÇAISE", + "code": "FR" + }, + "nom": "MARTIN", + "prenom": "JEAN-MARIE", + "naissance": { + "pays": { + "valeur": "FRANCE", + "code": "FR" + }, + "date": "1972-01-30", + "lieu": "MONTPELLIER" + } + }, + "personne_morale": { + "forme_juridique": { + "valeur": "SOCIETE PAR ACTIONS SIMPLIFIEES", + "code": "SASh" + }, + "denomination": "2X INVEST", + "date_cloture_exercice_comptable": "02-24", + "date_fin_de_vie": "2075-05-18" + } + }, + "links": {}, + "meta": {} +} diff --git a/spec/jobs/api_entreprise/extrait_kbis_job_spec.rb b/spec/jobs/api_entreprise/extrait_kbis_job_spec.rb new file mode 100644 index 000000000..e53d665c8 --- /dev/null +++ b/spec/jobs/api_entreprise/extrait_kbis_job_spec.rb @@ -0,0 +1,22 @@ +describe APIEntreprise::ExtraitKbisJob, type: :job do + let(:etablissement) { create(:etablissement, siret: siret) } + let(:siret) { '13002526500013' } + let(:siren) { '130025265' } + let(:procedure) { create(:procedure) } + let(:procedure_id) { procedure.id } + let(:body) { File.read('spec/fixtures/files/api_entreprise/extrait_kbis.json') } + let(:status) { 200 } + subject { APIEntreprise::ExtraitKbisJob.new.perform(etablissement.id, procedure_id) } + + before do + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/infogreffe\/rcs\/unites_legales\/#{siren}\/extrait_kbis/) + .to_return(body: body, status: status) + allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) + end + + it 'updates etablissement' do + subject + expect(Etablissement.find(etablissement.id).entreprise_capital_social).to eq(50123) + expect(Etablissement.find(etablissement.id).entreprise_nom_commercial).to eq('DECATHLON') + end +end diff --git a/spec/lib/api_entreprise/extrait_kbis_adapter_spec.rb b/spec/lib/api_entreprise/extrait_kbis_adapter_spec.rb new file mode 100644 index 000000000..8f9f7ab59 --- /dev/null +++ b/spec/lib/api_entreprise/extrait_kbis_adapter_spec.rb @@ -0,0 +1,29 @@ +describe APIEntreprise::ExtraitKbisAdapter do + let(:siren) { '130025265' } + let(:procedure) { create(:procedure) } + let(:procedure_id) { procedure.id } + let(:adapter) { described_class.new(siren, procedure_id) } + subject { adapter.to_params } + + before do + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/infogreffe\/rcs\/unites_legales\/#{siren}\/extrait_kbis/) + .to_return(body: body, status: status) + allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) + end + context "when the SIRET is valid" do + let(:body) { File.read('spec/fixtures/files/api_entreprise/extrait_kbis.json') } + let(:status) { 200 } + + it '#to_params class est une Hash ?' do + expect(subject).to be_an_instance_of(Hash) + end + + it 'L\'entreprise contient bien un capital_social' do + expect(subject[:entreprise_capital_social]).to eq(50123.6) + end + + it 'L\'entreprise contient bien un nom_commercial' do + expect(subject[:entreprise_nom_commercial]).to eq('DECATHLON') + end + end +end diff --git a/spec/services/api_entreprise_service_spec.rb b/spec/services/api_entreprise_service_spec.rb index 98891871d..0da2baf1f 100644 --- a/spec/services/api_entreprise_service_spec.rb +++ b/spec/services/api_entreprise_service_spec.rb @@ -1,7 +1,8 @@ describe APIEntrepriseService do shared_examples 'schedule fetch of all etablissement params' do [ - APIEntreprise::EntrepriseJob, APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob, + APIEntreprise::EntrepriseJob, APIEntreprise::ExtraitKbisJob, + APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob, APIEntreprise::EffectifsJob, APIEntreprise::EffectifsAnnuelsJob, APIEntreprise::AttestationSocialeJob, APIEntreprise::BilansBdfJob ].each do |job|