Merge pull request #9102 from demarches-simplifiees/8472-migrate-rna
API Entreprise: migration RNA (associations)
This commit is contained in:
commit
1a6dc10548
10 changed files with 205 additions and 149 deletions
|
@ -4,7 +4,7 @@ class APIEntreprise::API
|
|||
EXTRAIT_KBIS_NAME = "v3/infogreffe/rcs/unites_legales/%{id}/extrait_kbis"
|
||||
TVA_NAME = "v3/european_commission/unites_legales/%{id}/numero_tva"
|
||||
EXERCICES_RESOURCE_NAME = "v3/dgfip/etablissements/%{id}/chiffres_affaires"
|
||||
RNA_RESOURCE_NAME = "v2/associations/%{id}"
|
||||
RNA_RESOURCE_NAME = "v4/djepva/api-association/associations/open_data/%{id}"
|
||||
EFFECTIFS_RESOURCE_NAME = "v2/effectifs_mensuels_acoss_covid"
|
||||
EFFECTIFS_ANNUELS_RESOURCE_NAME = "v2/effectifs_annuels_acoss_covid/%{id}"
|
||||
ATTESTATION_SOCIALE_RESOURCE_NAME = "v4/urssaf/unites_legales/%{id}/attestation_vigilance"
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
require 'json_schemer'
|
||||
|
||||
class APIEntreprise::RNAAdapter < APIEntreprise::Adapter
|
||||
class InvalidSchemaError < ::StandardError
|
||||
def initialize(errors)
|
||||
super(errors.map(&:to_json).join("\n"))
|
||||
end
|
||||
end
|
||||
# Doc métier : https://entreprise.api.gouv.fr/catalogue/djepva/associations_open_data
|
||||
# Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Informations-generales/paths/~1v4~1djepva~1api-association~1associations~1open_data~1%7Bsiren_or_rna%7D/get
|
||||
|
||||
private
|
||||
|
||||
|
@ -14,39 +9,22 @@ class APIEntreprise::RNAAdapter < APIEntreprise::Adapter
|
|||
end
|
||||
|
||||
def process_params
|
||||
params = data_source[:association]
|
||||
return {} if params.nil?
|
||||
data, meta = data_source.values_at(:data, :meta)
|
||||
return {} if data.nil?
|
||||
|
||||
Sentry.with_scope do |scope|
|
||||
scope.set_tags(siret: @siret)
|
||||
scope.set_extras(source: params)
|
||||
scope.set_extras(source: data)
|
||||
|
||||
params = params.slice(*attr_to_fetch) if @depreciated
|
||||
params[:rna] = data_source.dig(:association, :id)
|
||||
|
||||
if params[:rna].present? && valid_params?(params)
|
||||
params = params.transform_keys { |k| :"association_#{k}" }.deep_stringify_keys
|
||||
|
||||
raise InvalidSchemaError.new(schemer.validate(params).to_a) unless schemer.valid?(params)
|
||||
|
||||
params
|
||||
else
|
||||
{}
|
||||
{
|
||||
"association_rna" => data[:rna],
|
||||
"association_titre" => data[:nom],
|
||||
"association_objet" => data[:activites][:objet],
|
||||
"association_date_creation" => data[:date_creation],
|
||||
# see: https://mattermost.incubateur.net/betagouv/pl/r6txumw9cpyx58rt7iq5dte9qe
|
||||
"association_date_declaration" => meta[:date_derniere_mise_a_jour_rna],
|
||||
"association_date_publication" => data[:date_publication_journal_officiel]
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def schemer
|
||||
@schemer ||= JSONSchemer.schema(Rails.root.join('app/schemas/association.json'))
|
||||
end
|
||||
|
||||
def attr_to_fetch
|
||||
[
|
||||
:titre,
|
||||
:objet,
|
||||
:date_creation,
|
||||
:date_declaration,
|
||||
:date_publication
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,7 +25,7 @@ describe Champs::RNAController, type: :controller do
|
|||
|
||||
before do
|
||||
sign_in user
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{rna}/)
|
||||
.to_return(body: body, status: status)
|
||||
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
@ -103,43 +103,18 @@ describe Champs::RNAController, type: :controller do
|
|||
let(:rna) { 'W595001988' }
|
||||
let(:status) { 200 }
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
let(:expected_data) do
|
||||
{
|
||||
"association_id" => "W595001988",
|
||||
"association_titre" => "UN SUR QUATRE",
|
||||
"association_objet" => "valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement",
|
||||
"association_siret" => nil,
|
||||
"association_date_creation" => "2014-01-23",
|
||||
"association_date_declaration" => "2014-01-24",
|
||||
"association_date_publication" => "2014-02-08",
|
||||
"association_date_dissolution" => "0001-01-01",
|
||||
"association_adresse_siege" => {
|
||||
"complement" => "",
|
||||
"numero_voie" => "61",
|
||||
"type_voie" => "RUE",
|
||||
"libelle_voie" => "des Noyers",
|
||||
"distribution" => "_",
|
||||
"code_insee" => "93063",
|
||||
"code_postal" => "93230",
|
||||
"commune" => "Romainville"
|
||||
},
|
||||
"association_code_civilite_dirigeant" => "PM",
|
||||
"association_civilite_dirigeant" => "Monsieur le Président",
|
||||
"association_code_etat" => "A",
|
||||
"association_etat" => "Active",
|
||||
"association_code_groupement" => "S",
|
||||
"association_groupement" => "simple",
|
||||
"association_mise_a_jour" => 1392295833,
|
||||
"association_rna" => "W595001988"
|
||||
}
|
||||
end
|
||||
|
||||
subject! { get :show, params: params, format: :turbo_stream }
|
||||
|
||||
it 'populates the data and RNA on the model' do
|
||||
champ.reload
|
||||
expect(champ.value).to eq(rna)
|
||||
expect(champ.data).to eq(expected_data)
|
||||
expect(champ.data["association_titre"]).to eq("LA PRÉVENTION ROUTIERE")
|
||||
expect(champ.data["association_objet"]).to eq("L'association a pour objet de promouvoir la pratique du sport de haut niveau et de contribuer à la formation des jeunes sportifs.")
|
||||
expect(champ.data["association_date_creation"]).to eq("2015-01-01")
|
||||
expect(champ.data["association_date_declaration"]).to eq("2019-01-01")
|
||||
expect(champ.data["association_date_publication"]).to eq("2018-01-01")
|
||||
expect(champ.data["association_rna"]).to eq("W751080001")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
189
spec/fixtures/files/api_entreprise/associations.json
vendored
189
spec/fixtures/files/api_entreprise/associations.json
vendored
|
@ -1,30 +1,171 @@
|
|||
{
|
||||
"association": {
|
||||
"id": "W595001988",
|
||||
"titre": "UN SUR QUATRE",
|
||||
"objet": "valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement",
|
||||
"siret": null,
|
||||
"date_creation": "2014-01-23",
|
||||
"date_declaration": "2014-01-24",
|
||||
"date_publication": "2014-02-08",
|
||||
"date_dissolution": "0001-01-01",
|
||||
"data": {
|
||||
"rna": "W751080001",
|
||||
"ancien_id": "1234567890",
|
||||
"siren": "751080001",
|
||||
"nom": "LA PRÉVENTION ROUTIERE",
|
||||
"active": true,
|
||||
"sigle": "LPR",
|
||||
"reconnue_utilite_publique": true,
|
||||
"siret_siege": "75108000100013",
|
||||
"forme_juridique": {
|
||||
"code": "9220",
|
||||
"libelle": "Association déclarée"
|
||||
},
|
||||
"regime": "Loi 1901",
|
||||
"groupement": "Simple",
|
||||
"eligibilite_cec": true,
|
||||
"raison_non_eligibilite_cec": "L’association n’a pas trois ans d’existence",
|
||||
"impots_commerciaux": true,
|
||||
"date_creation": "2015-01-01",
|
||||
"date_dissolution": "2016-01-01",
|
||||
"date_publication_reconnue_utilite_publique": "2017-01-01",
|
||||
"date_publication_journal_officiel": "2018-01-01",
|
||||
"adresse_siege": {
|
||||
"complement": "",
|
||||
"numero_voie": "61",
|
||||
"type_voie": "RUE",
|
||||
"libelle_voie": "des Noyers",
|
||||
"distribution": "_",
|
||||
"code_insee": "93063",
|
||||
"code_postal": "93230",
|
||||
"commune": "Romainville"
|
||||
"numero_voie": "33",
|
||||
"type_voie": "rue",
|
||||
"libelle_voie": "de Modagor",
|
||||
"distribution": "dummy",
|
||||
"code_insee": "75108",
|
||||
"code_postal": "75009",
|
||||
"commune": "Paris"
|
||||
},
|
||||
"code_civilite_dirigeant": "PM",
|
||||
"civilite_dirigeant": "Monsieur le Président",
|
||||
"code_etat": "A",
|
||||
"etat": "Active",
|
||||
"code_groupement": "S",
|
||||
"groupement": "simple",
|
||||
"mise_a_jour": 1392295833
|
||||
"adresse_gestion": {
|
||||
"complement": "",
|
||||
"numero_voie": "33",
|
||||
"type_voie": "rue",
|
||||
"libelle_voie": "de Modagor",
|
||||
"distribution": "dummy",
|
||||
"code_insee": "75108",
|
||||
"code_postal": "75009",
|
||||
"commune": "Paris"
|
||||
},
|
||||
"date_extraction_donnees": 1427210585
|
||||
"alsace_moselle": {
|
||||
"tribunal_instance": "Tribunal d'instance de Strasbourg",
|
||||
"volume": "122",
|
||||
"folio": "082",
|
||||
"date_publication_registre_association": "2019-01-01"
|
||||
},
|
||||
"composition_reseau": [
|
||||
{
|
||||
"nom": "Association des parents d'élèves de l'école primaire de la ville",
|
||||
"rna": "W123456789",
|
||||
"siret": "12345678901234",
|
||||
"telephone": "0123456789",
|
||||
"courriel": "reseau@association.fr",
|
||||
"objet": "Promouvoir l'engagement des parents d'élèves dans l'éducation de leurs enfants",
|
||||
"adresse": {
|
||||
"complement": "",
|
||||
"numero_voie": "33",
|
||||
"type_voie": "rue",
|
||||
"libelle_voie": "de Modagor",
|
||||
"distribution": "dummy",
|
||||
"code_insee": "75108",
|
||||
"code_postal": "75009",
|
||||
"commune": "Paris"
|
||||
},
|
||||
"site_web": "reseau-association.fr"
|
||||
}
|
||||
],
|
||||
"agrements": [
|
||||
{
|
||||
"numero": "05.11.2018",
|
||||
"date_attribution": "2018-11-05",
|
||||
"type": "Formation",
|
||||
"niveau": "local",
|
||||
"attributeur": "Ministère de l'Education nationale",
|
||||
"url": "https://associations.api.gouv.fr/agrements/05.11.2018.pdf"
|
||||
}
|
||||
],
|
||||
"activites": {
|
||||
"objet": "L'association a pour objet de promouvoir la pratique du sport de haut niveau et de contribuer à la formation des jeunes sportifs.",
|
||||
"objet_social1": {
|
||||
"code": "017055",
|
||||
"libelle": "accompagnement, aide aux malades"
|
||||
},
|
||||
"objet_social2": {
|
||||
"code": "009040",
|
||||
"libelle": "clubs troisième âge"
|
||||
},
|
||||
"champ_action_territorial": "départemental",
|
||||
"activite_principale": {
|
||||
"code": "88.99B",
|
||||
"libelle": "Action sociale sans hébergement n.c.a.",
|
||||
"annee": "2012"
|
||||
},
|
||||
"tranche_effectif": {
|
||||
"code": "51",
|
||||
"libelle": "2 000 à 4 999 salariés",
|
||||
"annee": "2013"
|
||||
},
|
||||
"economie_sociale_et_solidaire": true,
|
||||
"date_appartenance_ess": "2019-01-01"
|
||||
},
|
||||
"etablissements": [
|
||||
{
|
||||
"siren": "123456789",
|
||||
"siret": "12345678900001",
|
||||
"actif": true,
|
||||
"siege": true,
|
||||
"nom": "Croix rouge française",
|
||||
"telephone": "0123456789",
|
||||
"courriel": "contact@association.fr",
|
||||
"date_debut_activite": "2019-01-01",
|
||||
"adresse": {
|
||||
"complement": "",
|
||||
"numero_voie": "33",
|
||||
"type_voie": "rue",
|
||||
"libelle_voie": "de Modagor",
|
||||
"distribution": "dummy",
|
||||
"code_insee": "75108",
|
||||
"code_postal": "75009",
|
||||
"commune": "Paris"
|
||||
},
|
||||
"activite_principale": {
|
||||
"code": "88.99B",
|
||||
"libelle": "Action sociale sans hébergement n.c.a."
|
||||
},
|
||||
"tranche_effectif": {
|
||||
"code": "51",
|
||||
"libelle": "2 000 à 4 999 salariés"
|
||||
},
|
||||
"rhs": [
|
||||
{
|
||||
"annee": "2019",
|
||||
"nombre_benevoles": 10,
|
||||
"nombre_volontaires": 11,
|
||||
"nombre_salaries": 12,
|
||||
"nombre_salaries_etpt": 13,
|
||||
"nombre_emplois_aides": 14,
|
||||
"nombre_personnels_detaches": 15,
|
||||
"nombre_adherents": {
|
||||
"hommes": 16,
|
||||
"femmes": 17,
|
||||
"total": 33
|
||||
}
|
||||
}
|
||||
],
|
||||
"comptes": [
|
||||
{
|
||||
"annee": "2019",
|
||||
"commisaire_aux_comptes": true,
|
||||
"montant_dons": 1000,
|
||||
"cause_subventions": "Subventions d'investissement",
|
||||
"montant_subventions": 1200,
|
||||
"montant_aides_sur_3ans": 1230,
|
||||
"total_charges": 1234,
|
||||
"total_resultat": 2000,
|
||||
"total_produits": 3000
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"links": {},
|
||||
"meta": {
|
||||
"internal_id": "1234567890",
|
||||
"date_derniere_mise_a_jour_sirene": "2019-01-01",
|
||||
"date_derniere_mise_a_jour_rna": "2019-01-01"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ RSpec.describe APIEntreprise::AssociationJob, type: :job do
|
|||
let(:status) { 200 }
|
||||
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{siret}/)
|
||||
.to_return(body: body, status: status)
|
||||
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
@ -16,7 +16,7 @@ RSpec.describe APIEntreprise::AssociationJob, type: :job do
|
|||
|
||||
it 'updates etablissement' do
|
||||
subject
|
||||
expect(Etablissement.find(etablissement.id).association_rna).to eq('W595001988')
|
||||
expect(Etablissement.find(etablissement.id).association_rna).to eq("W751080001")
|
||||
end
|
||||
|
||||
context "when the etablissement has been deleted" do
|
||||
|
|
|
@ -147,7 +147,7 @@ describe APIEntreprise::API do
|
|||
|
||||
describe '.rna' do
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{siren}/)
|
||||
.to_return(status: status, body: body)
|
||||
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ describe APIEntreprise::RNAAdapter do
|
|||
subject { adapter.to_params }
|
||||
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{rna}/)
|
||||
.to_return(body: body, status: status)
|
||||
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
@ -26,29 +26,11 @@ describe APIEntreprise::RNAAdapter do
|
|||
|
||||
it '#to_params return valid hash' do
|
||||
expect(subject).to be_an_instance_of(Hash)
|
||||
expect(subject["association_rna"]).to eq('W595001988')
|
||||
expect(subject["association_titre"]).to eq('UN SUR QUATRE')
|
||||
expect(subject["association_objet"]).to eq("valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement")
|
||||
expect(subject["association_date_declaration"]).to eq('2014-01-24')
|
||||
end
|
||||
end
|
||||
|
||||
context "when responds with invalid schema" do
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations_invalid.json') }
|
||||
let(:status) { 200 }
|
||||
|
||||
it '#to_params raise exception' do
|
||||
expect { subject }.to raise_exception(APIEntreprise::RNAAdapter::InvalidSchemaError)
|
||||
end
|
||||
end
|
||||
|
||||
context "when depreciated adapter is used" do
|
||||
let(:adapter) { described_class.new(rna, procedure_id, true) }
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
let(:status) { 200 }
|
||||
|
||||
it '#to_params filter the keys' do
|
||||
expect(subject.keys).to eq(["association_titre", "association_objet", "association_date_creation", "association_date_declaration", "association_date_publication", "association_rna"])
|
||||
expect(subject["association_rna"]).to eq("W751080001")
|
||||
expect(subject["association_titre"]).to eq("LA PRÉVENTION ROUTIERE")
|
||||
expect(subject["association_objet"]).to eq("L'association a pour objet de promouvoir la pratique du sport de haut niveau et de contribuer à la formation des jeunes sportifs.")
|
||||
expect(subject["association_date_declaration"]).to eq("2019-01-01")
|
||||
expect(subject["association_date_publication"]).to eq("2018-01-01")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ RSpec.describe RNAChampAssociationFetchableConcern do
|
|||
let!(:champ) { create(:champ_rna, data: "not nil data", value: 'W173847273') }
|
||||
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\//)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{rna}/)
|
||||
.to_return(body: body, status: status)
|
||||
allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
@ -63,32 +63,12 @@ RSpec.describe RNAChampAssociationFetchableConcern do
|
|||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
|
||||
it_behaves_like "an association fetcher", true, nil, 'W595001988', {
|
||||
"association_id" => "W595001988",
|
||||
"association_titre" => "UN SUR QUATRE",
|
||||
"association_objet" => "valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement",
|
||||
"association_siret" => nil,
|
||||
"association_date_creation" => "2014-01-23",
|
||||
"association_date_declaration" => "2014-01-24",
|
||||
"association_date_publication" => "2014-02-08",
|
||||
"association_date_dissolution" => "0001-01-01",
|
||||
"association_adresse_siege" => {
|
||||
"complement" => "",
|
||||
"numero_voie" => "61",
|
||||
"type_voie" => "RUE",
|
||||
"libelle_voie" => "des Noyers",
|
||||
"distribution" => "_",
|
||||
"code_insee" => "93063",
|
||||
"code_postal" => "93230",
|
||||
"commune" => "Romainville"
|
||||
},
|
||||
"association_code_civilite_dirigeant" => "PM",
|
||||
"association_civilite_dirigeant" => "Monsieur le Président",
|
||||
"association_code_etat" => "A",
|
||||
"association_etat" => "Active",
|
||||
"association_code_groupement" => "S",
|
||||
"association_groupement" => "simple",
|
||||
"association_mise_a_jour" => 1392295833,
|
||||
"association_rna" => "W595001988"
|
||||
"association_titre" => "LA PRÉVENTION ROUTIERE",
|
||||
"association_objet" => "L'association a pour objet de promouvoir la pratique du sport de haut niveau et de contribuer à la formation des jeunes sportifs.",
|
||||
"association_date_creation" => "2015-01-01",
|
||||
"association_date_declaration" => "2019-01-01",
|
||||
"association_date_publication" => "2018-01-01",
|
||||
"association_rna" => "W751080001"
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -70,7 +70,7 @@ describe 'Prefilling a dossier (with a GET request):', js: true do
|
|||
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\//)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{rna_value}/)
|
||||
.to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/associations.json'))
|
||||
end
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ describe 'Prefilling a dossier (with a POST request):', js: true do
|
|||
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\//)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{rna_value}/)
|
||||
.to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/associations.json'))
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue