From ba281d53cf9e56d7a90260034e8e4e5657e13740 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 25 May 2023 11:15:26 +0200 Subject: [PATCH 1/5] migrate entreprise adapter to v3 --- app/lib/api_entreprise/api.rb | 2 +- app/lib/api_entreprise/entreprise_adapter.rb | 69 +++--- .../champs/siret_controller_spec.rb | 2 +- .../users/dossiers_controller_spec.rb | 2 +- .../api_entreprise/entreprise_individual.json | 119 ++++----- .../files/api_entreprise/entreprises.json | 228 ++++-------------- .../api_entreprise/entreprise_job_spec.rb | 4 +- spec/lib/api_entreprise/api_spec.rb | 8 +- .../api_entreprise/entreprise_adapter_spec.rb | 61 ++--- ...mp_etablissement_fetchable_concern_spec.rb | 4 +- spec/services/api_entreprise_service_spec.rb | 4 +- spec/system/users/dossier_creation_spec.rb | 2 +- spec/system/users/dossier_prefill_get_spec.rb | 2 +- .../system/users/dossier_prefill_post_spec.rb | 2 +- 14 files changed, 168 insertions(+), 341 deletions(-) diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index fb5e0aa60..8b9cd5d19 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -1,5 +1,5 @@ class APIEntreprise::API - ENTREPRISE_RESOURCE_NAME = "v2/entreprises/%{id}" + ENTREPRISE_RESOURCE_NAME = "v3/insee/sirene/unites_legales/%{id}" ETABLISSEMENT_RESOURCE_NAME = "v2/etablissements/%{id}" EXERCICES_RESOURCE_NAME = "v2/exercices/%{id}" RNA_RESOURCE_NAME = "v2/associations/%{id}" diff --git a/app/lib/api_entreprise/entreprise_adapter.rb b/app/lib/api_entreprise/entreprise_adapter.rb index a81f2275d..5a97ac64c 100644 --- a/app/lib/api_entreprise/entreprise_adapter.rb +++ b/app/lib/api_entreprise/entreprise_adapter.rb @@ -6,7 +6,7 @@ class APIEntreprise::EntrepriseAdapter < APIEntreprise::Adapter end def process_params - params = data_source[:entreprise] + params = data_source[:data] return {} if params.nil? Sentry.with_scope do |scope| @@ -18,9 +18,29 @@ class APIEntreprise::EntrepriseAdapter < APIEntreprise::Adapter if params.present? && valid_params?(params) params[:date_creation] = Time.zone.at(params[:date_creation]).to_datetime if params[:date_creation].present? - if params[:raison_sociale].present? && is_individual_entreprise?(params[:forme_juridique_code]) - params[:raison_sociale] = humanize_raison_sociale(params[:raison_sociale]) + + forme_juridique = params.extract! :forme_juridique + if forme_juridique.present? + params[:forme_juridique] = forme_juridique[:forme_juridique][:libelle] + params[:forme_juridique_code] = forme_juridique[:forme_juridique][:code] end + + personne_morale_attributs = params.extract! :personne_morale_attributs + if personne_morale_attributs.present? + params[:raison_sociale] = personne_morale_attributs[:personne_morale_attributs][:raison_sociale] + end + + personne_physique_attributs = params.extract! :personne_physique_attributs + if personne_physique_attributs.present? + params[:nom] = build_nom(personne_physique_attributs) + params[:prenom] = personne_physique_attributs[:personne_physique_attributs][:prenom_usuel] + end + + tranche_effectif = params.extract! :tranche_effectif_salarie + if tranche_effectif.present? + params[:code_effectif_entreprise] = tranche_effectif[:tranche_effectif_salarie][:code] + end + params.transform_keys { |k| :"entreprise_#{k}" } else {} @@ -28,49 +48,34 @@ class APIEntreprise::EntrepriseAdapter < APIEntreprise::Adapter end end + def build_nom(personne_physique_attributs) + nom_usage = personne_physique_attributs[:personne_physique_attributs][:nom_usage]&.strip + nom_naissance = personne_physique_attributs[:personne_physique_attributs][:nom_naissance]&.strip + + return nom_usage if nom_naissance.blank? || nom_usage == nom_naissance + return nom_naissance if nom_usage.blank? + "#{nom_usage} (#{nom_naissance})" + end + def attr_to_fetch [ :siren, - :capital_social, - :numero_tva_intracommunautaire, :forme_juridique, - :forme_juridique_code, - :nom_commercial, + :personne_morale_attributs, + :personne_physique_attributs, :raison_sociale, :siret_siege_social, - :code_effectif_entreprise, - :date_creation, - :nom, - :prenom + :tranche_effectif_salarie, + :date_creation ] end def map_etat_administratif(data_source) - raw_value = data_source.dig(:entreprise, :etat_administratif, :value) # data structure will change in v3 + raw_value = data_source.dig(:data, :etat_administratif) # data structure will change in v3 case raw_value when 'A' then 'actif' when 'F', 'C' then 'fermé' end end - - def humanize_raison_sociale(params_raison_sociale) - # see SIREN official spec : https://sirene.fr/sirene/public/variable/syr-nomen-long - splitted_raison_sociale = params_raison_sociale.split(/[*,\/]/) - - nom_patronymique = splitted_raison_sociale.first - prenom = splitted_raison_sociale.last.titleize.strip - - if splitted_raison_sociale.count == 3 - nom_usage = splitted_raison_sociale.second - raison_sociale = "#{prenom} #{nom_usage} (#{nom_patronymique})" - else - raison_sociale = "#{prenom} #{nom_patronymique}" - end - raison_sociale - end - - def is_individual_entreprise?(forme_juridique_code) - forme_juridique_code == "1000" - end end diff --git a/spec/controllers/champs/siret_controller_spec.rb b/spec/controllers/champs/siret_controller_spec.rb index b520147af..84e35456f 100644 --- a/spec/controllers/champs/siret_controller_spec.rb +++ b/spec/controllers/champs/siret_controller_spec.rb @@ -31,7 +31,7 @@ describe Champs::SiretController, type: :controller do sign_in user stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) .to_return(status: api_etablissement_status, body: api_etablissement_body) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret[0..8]}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siret[0..8]}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/entreprises.json')) allow_any_instance_of(APIEntrepriseToken).to receive(:roles) .and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"]) diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 58e6ff8e6..27d1ab7a1 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -196,7 +196,7 @@ describe Users::DossiersController, type: :controller do sign_in(user) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) .to_return(status: api_etablissement_status, body: api_etablissement_body) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/) .to_return(body: Rails.root.join('spec/fixtures/files/api_entreprise/status.json').read, status: 200) allow_any_instance_of(APIEntrepriseToken).to receive(:roles) .and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"]) diff --git a/spec/fixtures/files/api_entreprise/entreprise_individual.json b/spec/fixtures/files/api_entreprise/entreprise_individual.json index 72b6e8658..35a55eb08 100644 --- a/spec/fixtures/files/api_entreprise/entreprise_individual.json +++ b/spec/fixtures/files/api_entreprise/entreprise_individual.json @@ -1,86 +1,53 @@ { - "entreprise": { + "data": { "siren": "909700890", - "capital_social": null, - "numero_tva_intracommunautaire": "FR19909700890", - "forme_juridique": "Entrepreneur individuel", - "forme_juridique_code": "1000", - "nom_commercial": "", - "procedure_collective": false, - "enseigne": null, - "libelle_naf_entreprise": "Ingénierie, études techniques", - "naf_entreprise": "7112B", - "raison_sociale": "LE LOUARN*LE LOUARN SMAIL/MARINE /", "siret_siege_social": "90970089000025", - "code_effectif_entreprise": null, - "date_creation": 1643151600, - "nom": "LE LOUARN", - "prenom": "MARINE", - "date_radiation": null, "categorie_entreprise": null, - "tranche_effectif_salarie_entreprise": { - "de": null, - "a": null, - "code": null, - "date_reference": null, - "intitule": null + "type": "personne_physique", + "personne_morale_attributs": { + "raison_sociale": null, + "sigle": null }, - "mandataires_sociaux": [], - "etat_administratif": { - "value": "A", - "date_cessation": null - }, - "diffusable_commercialement": true - }, - "etablissement_siege": { - "siege_social": true, - "siret": "90970089000025", - "naf": "7112B", - "libelle_naf": "Ingénierie, études techniques", - "date_mise_a_jour": 1657592059, - "tranche_effectif_salarie_etablissement": { - "de": null, - "a": null, - "code": null, - "date_reference": null, - "intitule": null - }, - "date_creation_etablissement": 1646694000, - "region_implantation": { - "code": "84", - "value": "Auvergne-Rhône-Alpes" - }, - "commune_implantation": { - "code": "42218", - "value": "Saint-Étienne" - }, - "pays_implantation": { - "code": "FR", - "value": "FRANCE" + "personne_physique_attributs": { + "pseudonyme": null, + "prenom_usuel": "MARINE", + "prenom_1": "MARINE", + "prenom_2": null, + "prenom_3": null, + "prenom_4": null, + "nom_usage": "LE LOUARN SMAIL", + "nom_naissance": "LE LOUARN", + "sexe": "F" }, "diffusable_commercialement": true, - "enseigne": null, - "adresse": { - "l1": "MADAME MARINE LE LOUARN SMAIL", - "l2": null, - "l3": null, - "l4": "4 RUE ETIENNE MIMARD", - "l5": null, - "l6": "42000 SAINT-ETIENNE", - "l7": "FRANCE", - "numero_voie": "4", - "type_voie": "RUE", - "nom_voie": "ETIENNE MIMARD", - "complement_adresse": null, - "code_postal": "42000", - "localite": "SAINT-ETIENNE", - "code_insee_localite": "42218", - "cedex": null + "status_diffusion": "diffusible", + "forme_juridique": { + "code": "1000", + "libelle": "Entrepreneur individuel" }, - "etat_administratif": { - "value": "A", - "date_fermeture": null - } + "activite_principale": { + "code": "71.12B", + "nomenclature": "NAFRev2", + "libelle": "Ingénierie, études techniques" + }, + "tranche_effectif_salarie": { + "de": null, + "a": null, + "code": null, + "date_reference": null, + "intitule": null + }, + "etat_administratif": "A", + "economie_sociale_et_solidaire": null, + "date_cessation": null, + "date_creation": 1643151600 }, - "gateway_error": false + "links": { + "siege_social": "https://entreprise.api.gouv.fr/v3/insee/sirene/etablissements/90970089000025", + "siege_social_adresse": "https://entreprise.api.gouv.fr/v3/insee/sirene/etablissements/90970089000025/adresse" + }, + "meta": { + "date_derniere_mise_a_jour": 1657576800, + "redirect_from_siren": null + } } diff --git a/spec/fixtures/files/api_entreprise/entreprises.json b/spec/fixtures/files/api_entreprise/entreprises.json index 90512381c..d182d0453 100644 --- a/spec/fixtures/files/api_entreprise/entreprises.json +++ b/spec/fixtures/files/api_entreprise/entreprises.json @@ -1,187 +1,53 @@ { - "entreprise": { - "siren": "418166096", - "capital_social": 462308, - "numero_tva_intracommunautaire": "FR16418166096", - "forme_juridique": "SA à directoire (s.a.i.)", - "forme_juridique_code": "5699", - "nom_commercial": "OCTO-TECHNOLOGY", - "procedure_collective": false, - "raison_sociale": "OCTO-TECHNOLOGY", - "siret_siege_social": "41816609600051", - "code_effectif_entreprise": "31", - "date_creation": 891381600, - "nom": "test_nom", - "prenom": "test_prenom", - "date_radiation": null, - "categorie_entreprise": "PME", - "tranche_effectif_salarie_entreprise": { - "de": 200, - "a": 249, - "code": "31", - "date_reference": "2014", - "intitule": "200 à 249 salariés" + "data": { + "siren": "130025265", + "siret_siege_social": "13002526500013", + "type": "personne_morale", + "personne_morale_attributs": { + "raison_sociale": "DIRECTION INTERMINISTERIELLE DU NUMERIQUE", + "sigle": "DINUM" }, - "etat_administratif": { - "value": "A", - "date_cessation": null + "personne_physique_attributs": { + "pseudonyme": "DJ Falcon", + "prenom_usuel": "Jean", + "prenom_1": "Jean", + "prenom_2": "Jacques", + "prenom_3": "Pierre", + "prenom_4": "Paul", + "nom_usage": "Dupont", + "nom_naissance": "Dubois", + "sexe": "M" }, - "exercices": [ - { - "chiffre_affaires": null, - "date_fin": null, - "date_debut": null, - "date_fin_exercice_timestamp": null - }, - { - "chiffre_affaires": null, - "date_fin": null, - "date_debut": null, - "date_fin_exercice_timestamp": null - }, - { - "chiffre_affaires": null, - "date_fin": null, - "date_debut": null, - "date_fin_exercice_timestamp": null - } - ], - "mandataires_sociaux": [ - { - "nom": "HISQUIN", - "prenom": "FRANCOIS", - "fonction": "PRESIDENT DU DIRECTOIRE", - "dirigeant": true, - "date_naissance": "1965-01-27", - "date_naissance_timestamp": -155523600, - "raison_sociale": "", - "identifiant": "", - "type": "PP" - }, - { - "nom": "BONTE", - "prenom": "NICOLAS", - "fonction": "PRESIDENT DU CONSEIL DE SURVEILLANCE", - "dirigeant": true, - "date_naissance": "1965-01-21", - "date_naissance_timestamp": -156042000, - "raison_sociale": "", - "identifiant": "", - "type": "PP" - }, - { - "nom": "BOSQUE", - "prenom": "WILLIAM", - "fonction": "VICE PRESIDENT ET MEMBRE DU CONSEIL DE SURVEILLANCE", - "dirigeant": true, - "date_naissance": "1970-12-31", - "date_naissance_timestamp": 31446000, - "raison_sociale": "", - "identifiant": "", - "type": "PP" - }, - { - "nom": "CINQUIN", - "prenom": "LUDOVIC", - "fonction": "MEMBRE DU DIRECTOIRE", - "dirigeant": true, - "date_naissance": "1972-01-25", - "date_naissance_timestamp": 65142000, - "raison_sociale": "", - "identifiant": "", - "type": "PP" - }, - { - "nom": "LUCAS", - "prenom": "JACQUES", - "fonction": "MEMBRE DU CONSEIL DE SURVEILLANCE", - "dirigeant": true, - "date_naissance": "1964-12-02", - "date_naissance_timestamp": -160362000, - "raison_sociale": "", - "identifiant": "", - "type": "PP" - }, - { - "nom": "DEGONSE", - "prenom": "GERARD", - "fonction": "MEMBRE DU CONSEIL DE SURVEILLANCE", - "dirigeant": true, - "date_naissance": "1947-07-03", - "date_naissance_timestamp": -710038800, - "raison_sociale": "", - "identifiant": "", - "type": "PP" - }, - { - "nom": "PLANTIN", - "prenom": "JEAN-FRANCOIS", - "fonction": "COMMISSAIRE AUX COMPTES TITULAIRE", - "dirigeant": true, - "date_naissance": "1959-01-27", - "date_naissance_timestamp": -344912400, - "raison_sociale": "", - "identifiant": "", - "type": "PP" - }, - { - "nom": "", - "prenom": "", - "fonction": "COMMISSAIRE AUX COMPTES SUPPLEANT", - "dirigeant": true, - "date_naissance": "", - "date_naissance_timestamp": 0, - "raison_sociale": "BCRH \u0026 ASSOCIES - SOCIETE A RESPONSABILITE LIMITEE A ASSOCIE UNIQUE", - "identifiant": "490092574", - "type": "PM" - } - ] + "categorie_entreprise": "GE", + "status_diffusion": "lorem", + "diffusable_commercialement": true, + "forme_juridique": { + "code": "7120", + "libelle": "Service central d'un ministère" + }, + "activite_principale": { + "code": "8411Z", + "libelle": "Administration publique générale", + "nomenclature": "NAFRev2" + }, + "tranche_effectif_salarie": { + "code": "51", + "intitule": "2 000 à 4 999 salariés", + "date_reference": "2016", + "de": 2000, + "a": 4999 + }, + "etat_administratif": "A", + "economie_sociale_et_solidaire": true, + "date_creation": 1634103818, + "date_cessation": 1634133818 }, - "etablissement_siege": { - "siege_social": true, - "siret": "41816609600051", - "naf": "6202A", - "libelle_naf": "Conseil en systèmes et logiciels informatiques", - "date_mise_a_jour": 1449183600, - "tranche_effectif_salarie_etablissement": { - "de": 200, - "a": 249, - "code": "31", - "date_reference": "2014", - "intitule": "200 à 249 salariés" - }, - "date_creation_etablissement": 1108594800, - "enseigne": null, - "region_implantation": { - "code": "11", - "value": "Île-de-France" - }, - "commune_implantation": { - "code": "75108", - "value": "PARIS 8" - }, - "pays_implantation": { - "code": null, - "value": null - }, - "diffusable_commercialement": null, - "adresse": { - "l1": "OCTO TECHNOLOGY", - "l2": null, - "l3": null, - "l4": "50 AVENUE DES CHAMPS ELYSEES", - "l5": null, - "l6": "75008 PARIS", - "l7": "FRANCE", - "numero_voie": "50", - "type_voie": "AV", - "nom_voie": "DES CHAMPS ELYSEES", - "complement_adresse": null, - "code_postal": "75008", - "localite": "PARIS 8", - "code_insee_localite": "75108", - "cedex": null - } + "links": { + "siege_social": "https://entreprises.api.gouv.fr/api/v3/insee/etablissements/30613890001294", + "siege_social_adresse": "https://entreprises.api.gouv.fr/api/v3/insee/etablissements/30613890001294/adresse" }, - "gateway_error": false + "meta": { + "date_derniere_mise_a_jour": 1618396818, + "redirect_from_siren": "306138900" + } } diff --git a/spec/jobs/api_entreprise/entreprise_job_spec.rb b/spec/jobs/api_entreprise/entreprise_job_spec.rb index df8fdb796..51f5e729e 100644 --- a/spec/jobs/api_entreprise/entreprise_job_spec.rb +++ b/spec/jobs/api_entreprise/entreprise_job_spec.rb @@ -8,7 +8,7 @@ RSpec.describe APIEntreprise::EntrepriseJob, type: :job do let(:status) { 200 } before do - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/) .to_return(body: body, status: status) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) end @@ -17,7 +17,7 @@ RSpec.describe APIEntreprise::EntrepriseJob, type: :job do it 'updates etablissement' do subject - expect(Etablissement.find(etablissement.id).entreprise_numero_tva_intracommunautaire).to eq('FR16418166096') + expect(Etablissement.find(etablissement.id).entreprise_raison_sociale).to eq("DIRECTION INTERMINISTERIELLE DU NUMERIQUE") end it 'convert entreprise etat_administratif source to an enum' do diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index e60627107..f4fd189c8 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -7,8 +7,8 @@ describe APIEntreprise::API do subject { described_class.new(procedure_id).entreprise(siren) } before do - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) - .to_return(status: status, body: body) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/) + .to_return(body: body, status: status) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) end @@ -68,7 +68,7 @@ describe APIEntreprise::API do it 'call api-entreprise with specfic token' do subject - expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) + expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/) end end @@ -78,7 +78,7 @@ describe APIEntreprise::API do it 'call api-entreprise with specfic token' do subject - expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) + expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/) end end end diff --git a/spec/lib/api_entreprise/entreprise_adapter_spec.rb b/spec/lib/api_entreprise/entreprise_adapter_spec.rb index 2fce728fe..a5c1679f2 100644 --- a/spec/lib/api_entreprise/entreprise_adapter_spec.rb +++ b/spec/lib/api_entreprise/entreprise_adapter_spec.rb @@ -1,12 +1,12 @@ describe APIEntreprise::EntrepriseAdapter do - let(:siren) { '418166096' } + 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\/v2\/entreprises\/#{siren}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/) .to_return(body: body, status: status) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) end @@ -24,48 +24,28 @@ describe APIEntreprise::EntrepriseAdapter do expect(subject[:entreprise_siren]).to eq(siren) end - it 'L\'entreprise contient bien un capital_social' do - expect(subject[:entreprise_capital_social]).to eq(462308) - end - - it 'L\'entreprise contient bien un numero_tva_intracommunautaire' do - expect(subject[:entreprise_numero_tva_intracommunautaire]).to eq('FR16418166096') - end - it 'L\'entreprise contient bien une forme_juridique' do - expect(subject[:entreprise_forme_juridique]).to eq('SA à directoire (s.a.i.)') + expect(subject[:entreprise_forme_juridique]).to eq("Service central d'un ministère") end it 'L\'entreprise contient bien un forme_juridique_code' do - expect(subject[:entreprise_forme_juridique_code]).to eq('5699') - end - - it 'L\'entreprise contient bien un nom_commercial' do - expect(subject[:entreprise_nom_commercial]).to eq('OCTO-TECHNOLOGY') + expect(subject[:entreprise_forme_juridique_code]).to eq('7120') end it 'L\'entreprise contient bien une raison_sociale' do - expect(subject[:entreprise_raison_sociale]).to eq('OCTO-TECHNOLOGY') + expect(subject[:entreprise_raison_sociale]).to eq('DIRECTION INTERMINISTERIELLE DU NUMERIQUE') end it 'L\'entreprise contient bien un siret_siege_social' do - expect(subject[:entreprise_siret_siege_social]).to eq('41816609600051') + expect(subject[:entreprise_siret_siege_social]).to eq('13002526500013') end it 'L\'entreprise contient bien un code_effectif_entreprise' do - expect(subject[:entreprise_code_effectif_entreprise]).to eq('31') + expect(subject[:entreprise_code_effectif_entreprise]).to eq('51') end it 'L\'entreprise contient bien une date_creation' do - expect(subject[:entreprise_date_creation]).to eq('Wed, 01 Apr 1998 00:00:00.000000000 +0200') - end - - it 'L\'entreprise contient bien un nom' do - expect(subject[:entreprise_nom]).to eq('test_nom') - end - - it 'L\'entreprise contient bien un prenom' do - expect(subject[:entreprise_prenom]).to eq('test_prenom') + expect(subject[:entreprise_date_creation].to_i).to eq(1634103818) end it 'L\'entreprise contient bien un etat administratif' do @@ -76,7 +56,7 @@ describe APIEntreprise::EntrepriseAdapter do context "when date_creation is empty" do let(:body) do hash = JSON.parse(super()) - hash["entreprise"]["date_creation"] = nil + hash["data"]["date_creation"] = nil JSON.generate(hash) end @@ -109,14 +89,23 @@ describe APIEntreprise::EntrepriseAdapter do let(:body) { File.read('spec/fixtures/files/api_entreprise/entreprise_individual.json') } let(:status) { 200 } - context 'Attributs Entreprises' do - it 'L\'entreprise contient bien un forme_juridique_code' do - expect(subject[:entreprise_forme_juridique_code]).to eq('1000') - end + it 'L\'entreprise contient bien un forme_juridique_code' do + expect(subject[:entreprise_forme_juridique_code]).to eq('1000') + end - it 'L\'entreprise contient bien une raison_sociale' do - expect(subject[:entreprise_raison_sociale]).to eq('Marine LE LOUARN SMAIL (LE LOUARN)') - end + # Suppression du champ raison_sociale pour les personnes physiques + # en v3 + # https://entreprise.api.gouv.fr/developpeurs/guide-migration + it 'L\'entreprise ne contient pas de raison_sociale' do + expect(subject[:entreprise_raison_sociale]).to be_nil + end + + it 'L\'entreprise contient bien un nom' do + expect(subject[:entreprise_nom]).to eq('LE LOUARN SMAIL (LE LOUARN)') + end + + it 'L\'entreprise contient bien un prenom' do + expect(subject[:entreprise_prenom]).to eq('MARINE') end end end diff --git a/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb b/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb index 82014e173..364689184 100644 --- a/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb +++ b/spec/models/concern/siret_champ_etablissement_fetchable_concern_spec.rb @@ -8,7 +8,7 @@ RSpec.describe SiretChampEtablissementFetchableConcern do before do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) .to_return(status: api_etablissement_status, body: api_etablissement_body) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret[0..8]}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siret[0..8]}/) .to_return(body: File.read('spec/fixtures/files/api_entreprise/entreprises.json'), status: 200) allow_any_instance_of(APIEntrepriseToken).to receive(:roles) .and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"]) @@ -104,7 +104,7 @@ RSpec.describe SiretChampEtablissementFetchableConcern do it "fetches the entreprise raison sociale" do fetch_etablissement! - expect(champ.reload.etablissement.entreprise_raison_sociale).to eq("OCTO-TECHNOLOGY") + expect(champ.reload.etablissement.entreprise_raison_sociale).to eq("DIRECTION INTERMINISTERIELLE DU NUMERIQUE") end end end diff --git a/spec/services/api_entreprise_service_spec.rb b/spec/services/api_entreprise_service_spec.rb index 5e8a165cf..98891871d 100644 --- a/spec/services/api_entreprise_service_spec.rb +++ b/spec/services/api_entreprise_service_spec.rb @@ -15,12 +15,12 @@ describe APIEntrepriseService do before do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) .to_return(body: etablissements_body, status: etablissements_status) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret[0..8]}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siret[0..8]}/) .to_return(body: entreprises_body, status: entreprises_status) end let(:siret) { '41816609600051' } - let(:raison_sociale) { "OCTO-TECHNOLOGY" } + let(:raison_sociale) { "DIRECTION INTERMINISTERIELLE DU NUMERIQUE" } let(:etablissements_status) { 200 } let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') } let(:entreprises_status) { 200 } diff --git a/spec/system/users/dossier_creation_spec.rb b/spec/system/users/dossier_creation_spec.rb index c2fcbae6d..ca2c8f3e4 100644 --- a/spec/system/users/dossier_creation_spec.rb +++ b/spec/system/users/dossier_creation_spec.rb @@ -70,7 +70,7 @@ describe 'Creating a new dossier:' do before do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/etablissements.json')) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/entreprises.json')) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/#{siret}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/exercices.json')) diff --git a/spec/system/users/dossier_prefill_get_spec.rb b/spec/system/users/dossier_prefill_get_spec.rb index fb9e05d4b..0c428bef2 100644 --- a/spec/system/users/dossier_prefill_get_spec.rb +++ b/spec/system/users/dossier_prefill_get_spec.rb @@ -67,7 +67,7 @@ describe 'Prefilling a dossier (with a GET request):', js: true do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\//) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/etablissements.json')) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret_value[0..8]}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siret_value[0..8]}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/entreprises.json')) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//) diff --git a/spec/system/users/dossier_prefill_post_spec.rb b/spec/system/users/dossier_prefill_post_spec.rb index b88562380..ea52c2529 100644 --- a/spec/system/users/dossier_prefill_post_spec.rb +++ b/spec/system/users/dossier_prefill_post_spec.rb @@ -44,7 +44,7 @@ describe 'Prefilling a dossier (with a POST request):', js: true do stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret_value}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/etablissements.json')) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siret_value[0..8]}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siret_value[0..8]}/) .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/entreprises.json')) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//) From f04e121a6f5fbcc1175b2bda4b541fa718d28c91 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 25 May 2023 14:46:34 +0200 Subject: [PATCH 2/5] add extrait_kbis to handle nom_commercial and capital_social --- app/jobs/api_entreprise/extrait_kbis_job.rb | 7 ++ app/lib/api_entreprise/api.rb | 5 ++ .../api_entreprise/extrait_kbis_adapter.rb | 17 ++++ app/services/api_entreprise_service.rb | 3 +- .../files/api_entreprise/extrait_kbis.json | 81 +++++++++++++++++++ .../api_entreprise/extrait_kbis_job_spec.rb | 22 +++++ .../extrait_kbis_adapter_spec.rb | 29 +++++++ spec/services/api_entreprise_service_spec.rb | 3 +- 8 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 app/jobs/api_entreprise/extrait_kbis_job.rb create mode 100644 app/lib/api_entreprise/extrait_kbis_adapter.rb create mode 100644 spec/fixtures/files/api_entreprise/extrait_kbis.json create mode 100644 spec/jobs/api_entreprise/extrait_kbis_job_spec.rb create mode 100644 spec/lib/api_entreprise/extrait_kbis_adapter_spec.rb 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| From 76a8b721cef54d6b164a3b70a1c9555ebeb68f05 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 25 May 2023 15:29:16 +0200 Subject: [PATCH 3/5] add tva adapter and tva job --- app/jobs/api_entreprise/tva_job.rb | 7 ++++++ app/lib/api_entreprise/api.rb | 5 ++++ app/lib/api_entreprise/tva_adapter.rb | 15 +++++++++++ app/services/api_entreprise_service.rb | 2 +- spec/fixtures/files/api_entreprise/tva.json | 7 ++++++ spec/jobs/api_entreprise/tva_job_spec.rb | 21 ++++++++++++++++ spec/lib/api_entreprise/tva_adapter_spec.rb | 26 ++++++++++++++++++++ spec/services/api_entreprise_service_spec.rb | 2 +- 8 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 app/jobs/api_entreprise/tva_job.rb create mode 100644 app/lib/api_entreprise/tva_adapter.rb create mode 100644 spec/fixtures/files/api_entreprise/tva.json create mode 100644 spec/jobs/api_entreprise/tva_job_spec.rb create mode 100644 spec/lib/api_entreprise/tva_adapter_spec.rb diff --git a/app/jobs/api_entreprise/tva_job.rb b/app/jobs/api_entreprise/tva_job.rb new file mode 100644 index 000000000..630409fb0 --- /dev/null +++ b/app/jobs/api_entreprise/tva_job.rb @@ -0,0 +1,7 @@ +class APIEntreprise::TvaJob < APIEntreprise::Job + def perform(etablissement_id, procedure_id) + find_etablissement(etablissement_id) + etablissement_params = APIEntreprise::TvaAdapter.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 74d59fdff..e70b65282 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -2,6 +2,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" + TVA_NAME = "v3/european_commission/unites_legales/%{id}/numero_tva" EXERCICES_RESOURCE_NAME = "v2/exercices/%{id}" RNA_RESOURCE_NAME = "v2/associations/%{id}" EFFECTIFS_RESOURCE_NAME = "v2/effectifs_mensuels_acoss_covid" @@ -37,6 +38,10 @@ class APIEntreprise::API call_with_siret(EXTRAIT_KBIS_NAME, siren) end + def tva(siren) + call_with_siret(TVA_NAME, siren) + end + def exercices(siret) call_with_siret(EXERCICES_RESOURCE_NAME, siret) end diff --git a/app/lib/api_entreprise/tva_adapter.rb b/app/lib/api_entreprise/tva_adapter.rb new file mode 100644 index 000000000..3215f7642 --- /dev/null +++ b/app/lib/api_entreprise/tva_adapter.rb @@ -0,0 +1,15 @@ +class APIEntreprise::TvaAdapter < APIEntreprise::Adapter + private + + def get_resource + api(@procedure_id).tva(siren) + end + + def process_params + result = {} + if data_source[:data] + result[:entreprise_numero_tva_intracommunautaire] = data_source[:data][:tva_number] + end + result + end +end diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index 4be2aa943..585c4f8db 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -47,7 +47,7 @@ class APIEntrepriseService def perform_later_fetch_jobs(etablissement, procedure_id, user_id, wait: nil) [ - APIEntreprise::EntrepriseJob, APIEntreprise::ExtraitKbisJob, + APIEntreprise::EntrepriseJob, APIEntreprise::ExtraitKbisJob, APIEntreprise::TvaJob, APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob, APIEntreprise::EffectifsJob, APIEntreprise::EffectifsAnnuelsJob, APIEntreprise::AttestationSocialeJob, APIEntreprise::BilansBdfJob diff --git a/spec/fixtures/files/api_entreprise/tva.json b/spec/fixtures/files/api_entreprise/tva.json new file mode 100644 index 000000000..b5eafde08 --- /dev/null +++ b/spec/fixtures/files/api_entreprise/tva.json @@ -0,0 +1,7 @@ +{ + "data": { + "tva_number": "FR48672039971" + }, + "links": {}, + "meta": {} +} diff --git a/spec/jobs/api_entreprise/tva_job_spec.rb b/spec/jobs/api_entreprise/tva_job_spec.rb new file mode 100644 index 000000000..2a6881888 --- /dev/null +++ b/spec/jobs/api_entreprise/tva_job_spec.rb @@ -0,0 +1,21 @@ +describe APIEntreprise::TvaJob, type: :job do + let(:etablissement) { create(:etablissement, siret: siret) } + let(:siret) { '41816609600069' } + let(:siren) { '418166096' } + let(:procedure) { create(:procedure) } + let(:procedure_id) { procedure.id } + let(:body) { File.read('spec/fixtures/files/api_entreprise/tva.json') } + let(:status) { 200 } + subject { APIEntreprise::TvaJob.new.perform(etablissement.id, procedure_id) } + + before do + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/european_commission\/unites_legales\/#{siren}\/numero_tva/) + .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_numero_tva_intracommunautaire).to eq("FR48672039971") + end +end diff --git a/spec/lib/api_entreprise/tva_adapter_spec.rb b/spec/lib/api_entreprise/tva_adapter_spec.rb new file mode 100644 index 000000000..df76bfcc3 --- /dev/null +++ b/spec/lib/api_entreprise/tva_adapter_spec.rb @@ -0,0 +1,26 @@ +describe APIEntreprise::TvaAdapter do + let(:siren) { '418166096' } + 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\/european_commission\/unites_legales\/#{siren}\/numero_tva/) + .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/tva.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 numero_tva_intracommunautaire' do + expect(subject[:entreprise_numero_tva_intracommunautaire]).to eq("FR48672039971") + end + end +end diff --git a/spec/services/api_entreprise_service_spec.rb b/spec/services/api_entreprise_service_spec.rb index 0da2baf1f..835e35094 100644 --- a/spec/services/api_entreprise_service_spec.rb +++ b/spec/services/api_entreprise_service_spec.rb @@ -1,7 +1,7 @@ describe APIEntrepriseService do shared_examples 'schedule fetch of all etablissement params' do [ - APIEntreprise::EntrepriseJob, APIEntreprise::ExtraitKbisJob, + APIEntreprise::EntrepriseJob, APIEntreprise::ExtraitKbisJob, APIEntreprise::TvaJob, APIEntreprise::AssociationJob, APIEntreprise::ExercicesJob, APIEntreprise::EffectifsJob, APIEntreprise::EffectifsAnnuelsJob, APIEntreprise::AttestationSocialeJob, APIEntreprise::BilansBdfJob From f3a4aec0bf3bbc54137b91a34885d2ddafc3f3c8 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Fri, 26 May 2023 10:06:01 +0200 Subject: [PATCH 4/5] add reference doc for api --- app/lib/api_entreprise/attestation_sociale_adapter.rb | 3 +++ app/lib/api_entreprise/entreprise_adapter.rb | 3 +++ app/lib/api_entreprise/extrait_kbis_adapter.rb | 3 +++ app/lib/api_entreprise/tva_adapter.rb | 3 +++ 4 files changed, 12 insertions(+) diff --git a/app/lib/api_entreprise/attestation_sociale_adapter.rb b/app/lib/api_entreprise/attestation_sociale_adapter.rb index d2dd46b52..bae32627b 100644 --- a/app/lib/api_entreprise/attestation_sociale_adapter.rb +++ b/app/lib/api_entreprise/attestation_sociale_adapter.rb @@ -1,4 +1,7 @@ class APIEntreprise::AttestationSocialeAdapter < APIEntreprise::Adapter + # Doc métier : https://entreprise.api.gouv.fr/catalogue/urssaf/attestation_vigilance + # Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Attestations-sociales-et-fiscales/paths/~1v4~1urssaf~1unites_legales~1%7Bsiren%7D~1attestation_vigilance/get + def initialize(siret, procedure_id) @siret = siret @procedure_id = procedure_id diff --git a/app/lib/api_entreprise/entreprise_adapter.rb b/app/lib/api_entreprise/entreprise_adapter.rb index 5a97ac64c..41df33da0 100644 --- a/app/lib/api_entreprise/entreprise_adapter.rb +++ b/app/lib/api_entreprise/entreprise_adapter.rb @@ -1,4 +1,7 @@ class APIEntreprise::EntrepriseAdapter < APIEntreprise::Adapter + # Doc métier : https://entreprise.api.gouv.fr/catalogue/insee/unites_legales + # Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Informations-generales/paths/~1v3~1insee~1sirene~1unites_legales~1%7Bsiren%7D/get + private def get_resource diff --git a/app/lib/api_entreprise/extrait_kbis_adapter.rb b/app/lib/api_entreprise/extrait_kbis_adapter.rb index 72e12ca8c..06d3f2c7d 100644 --- a/app/lib/api_entreprise/extrait_kbis_adapter.rb +++ b/app/lib/api_entreprise/extrait_kbis_adapter.rb @@ -1,4 +1,7 @@ class APIEntreprise::ExtraitKbisAdapter < APIEntreprise::Adapter + # Doc métier : https://entreprise.api.gouv.fr/catalogue/infogreffe/rcs/extrait + # Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Informations-generales/paths/~1v3~1infogreffe~1rcs~1unites_legales~1%7Bsiren%7D~1extrait_kbis/get + private def get_resource diff --git a/app/lib/api_entreprise/tva_adapter.rb b/app/lib/api_entreprise/tva_adapter.rb index 3215f7642..eb48f1790 100644 --- a/app/lib/api_entreprise/tva_adapter.rb +++ b/app/lib/api_entreprise/tva_adapter.rb @@ -1,4 +1,7 @@ class APIEntreprise::TvaAdapter < APIEntreprise::Adapter + # Doc métier : https://entreprise.api.gouv.fr/catalogue/commission_europeenne/numero_tva + # Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Informations-generales/paths/~1v3~1european_commission~1unites_legales~1%7Bsiren%7D~1numero_tva/get + private def get_resource From 2a84f04bedcf98060f2808b93caee7a26da58605 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Fri, 26 May 2023 12:16:44 +0200 Subject: [PATCH 5/5] add sentry instrumentation for entreprise adapters --- .../attestation_sociale_adapter.rb | 19 +++++++++++++------ .../api_entreprise/extrait_kbis_adapter.rb | 13 +++++++++---- app/lib/api_entreprise/tva_adapter.rb | 14 ++++++++++---- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/lib/api_entreprise/attestation_sociale_adapter.rb b/app/lib/api_entreprise/attestation_sociale_adapter.rb index bae32627b..b9a5c47a6 100644 --- a/app/lib/api_entreprise/attestation_sociale_adapter.rb +++ b/app/lib/api_entreprise/attestation_sociale_adapter.rb @@ -14,12 +14,19 @@ class APIEntreprise::AttestationSocialeAdapter < APIEntreprise::Adapter end def process_params - if data_source[:data] && data_source[:data][:document_url].present? - { - entreprise_attestation_sociale_url: data_source[:data][:document_url] - } - else - {} + data = data_source[:data] + + Sentry.with_scope do |scope| + scope.set_tags(siret: @siret) + scope.set_extras(source: data) + + if data && data[:document_url].present? + { + entreprise_attestation_sociale_url: data[:document_url] + } + else + {} + end end end end diff --git a/app/lib/api_entreprise/extrait_kbis_adapter.rb b/app/lib/api_entreprise/extrait_kbis_adapter.rb index 06d3f2c7d..afa4559b1 100644 --- a/app/lib/api_entreprise/extrait_kbis_adapter.rb +++ b/app/lib/api_entreprise/extrait_kbis_adapter.rb @@ -11,10 +11,15 @@ class APIEntreprise::ExtraitKbisAdapter < APIEntreprise::Adapter 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] + + Sentry.with_scope do |scope| + scope.set_tags(siret: @siret) + scope.set_extras(source: data) + if data + result[:entreprise_capital_social] = data[:capital][:montant] if data[:capital] + result[:entreprise_nom_commercial] = data[:nom_commercial] + end + result end - result end end diff --git a/app/lib/api_entreprise/tva_adapter.rb b/app/lib/api_entreprise/tva_adapter.rb index eb48f1790..cfe6dfc56 100644 --- a/app/lib/api_entreprise/tva_adapter.rb +++ b/app/lib/api_entreprise/tva_adapter.rb @@ -9,10 +9,16 @@ class APIEntreprise::TvaAdapter < APIEntreprise::Adapter end def process_params - result = {} - if data_source[:data] - result[:entreprise_numero_tva_intracommunautaire] = data_source[:data][:tva_number] + Sentry.with_scope do |scope| + data = data_source[:data] + scope.set_tags(siret: @siret) + scope.set_extras(source: data) + + result = {} + if data + result[:entreprise_numero_tva_intracommunautaire] = data[:tva_number] + end + result end - result end end