create bilans_bdf_adapter

This commit is contained in:
Christophe Robillard 2020-04-30 08:18:17 +02:00
parent 725c387f91
commit 85b2e9ef44
5 changed files with 133 additions and 0 deletions

View file

@ -7,6 +7,7 @@ class ApiEntreprise::API
EFFECTIFS_ANNUELS_RESOURCE_NAME = "effectifs_annuels_acoss_covid"
ATTESTATION_SOCIALE_RESOURCE_NAME = "attestations_sociales_acoss"
ATTESTATION_FISCALE_RESOURCE_NAME = "attestations_fiscales_dgfip"
BILANS_BDF_RESOURCE_NAME = "bilans_entreprises_bdf"
TIMEOUT = 15
@ -51,6 +52,11 @@ class ApiEntreprise::API
call(ATTESTATION_FISCALE_RESOURCE_NAME, siren, procedure_id, user_id) if procedure.api_entreprise_role?("attestations_fiscales")
end
def self.bilans_bdf(siren, procedure_id)
procedure = Procedure.find(procedure_id)
call(BILANS_BDF_RESOURCE_NAME, siren, procedure_id) if procedure.api_entreprise_role?("bilans_entreprise_bdf")
end
private
def self.call(resource_name, siret_or_siren, procedure_id, user_id = nil)

View file

@ -0,0 +1,23 @@
class ApiEntreprise::BilansBdfAdapter < ApiEntreprise::Adapter
def initialize(siren, procedure_id)
@siren = siren
@procedure_id = procedure_id
end
private
def get_resource
ApiEntreprise::API.bilans_bdf(@siren, @procedure_id)
end
def process_params
if data_source[:bilans].present?
{
entreprise_bilans_bdf: data_source[:bilans],
entreprise_bilans_bdf_monnaie: data_source[:monnaie]
}
else
{}
end
end
end

View file

@ -0,0 +1,51 @@
{
"monnaie": "kEuros",
"bilans": [
{
"duree_exercice": "12",
"valeur_ajoutee_bdf": "7848792",
"resultat_exercice": "347126",
"capitaux_propres_et_assimiles": "5928663",
"total_provisions_pour_risques_et_charges": "1957919",
"dettes1_emprunts_obligataires_et_convertibles": "0",
"dettes2_autres_emprunts_obligataires": "6552306",
"total_dettes_stables": "6552306",
"emprunts_et_dettes_financieres_divers": "430634",
"groupes_et_associes": "0",
"besoin_en_fonds_de_roulement": "-721507",
"disponibilites": "1983051",
"total_passif": "18478051",
"evolution_valeur_ajoutee_bdf": "",
"evolution_resultat_exercice": "",
"evolution_capitaux_propres_et_assimiles": "",
"evolution_total_provisions_pour_risques_et_charges": "",
"evolution_dettes1_emprunts_obligataires_et_convertibles": "",
"evolution_dettes2_autres_emprunts_obligataires": "",
"evolution_emprunts_et_dettes_financieres_divers": "",
"evolution_groupes_et_associes": "",
"evolution_besoin_en_fonds_de_roulement": "",
"evolution_disponibilites": "",
"evolution_total_passif": "",
"chiffre_affaires_ht": "12030700",
"capacite_autofinancement": "891914",
"date_arret_exercice": "201512",
"dettes3_emprunts_et_dettes_aupres_des_etablissements_de_credit": "0",
"dettes4_maturite_a_un_an_au_plus": "0",
"autres_fonds_propres": "0",
"capital_social_inclus_dans_capitaux_propres_et_assimiles": "3800000",
"excedent_brut_exploitation": "-1876863",
"evolution_chiffre_affaires_ht": "",
"evolution_capacite_autofinancement": "",
"evolution_dettes3_emprunts_et_dettes_aupres_des_etablissements_de_credit": "",
"evolution_dettes4_maturite_a_un_an_au_plus": "",
"evolution_autres_fonds_propres": "",
"evolution_capital_social_inclus_dans_capitaux_propres_et_assimiles": "",
"evolution_excedent_brut_exploitation": "",
"evolution_fonds_roulement_net_global": "",
"evolution_ratio_fonds_roulement_net_global_sur_besoin_en_fonds_de_roulement": "",
"evolution_total_dettes_stables": "",
"fonds_roulement_net_global": "2464585",
"ratio_fonds_roulement_net_global_sur_besoin_en_fonds_de_roulement": "-"
}, "bilan 2", "bilan 3"
]
}

View file

@ -213,4 +213,31 @@ describe ApiEntreprise::API do
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end
end
describe '.bilans_bdf' do
let(:procedure) { create(:procedure, api_entreprise_token: token) }
let(:siren) { '418166096' }
let(:status) { 200 }
let(:body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
before do
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles).and_return(roles)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=#{token}/)
.to_return(body: body, status: status)
end
subject { described_class.bilans_bdf(siren, procedure.id) }
context 'when token not authorized' do
let(:roles) { ["entreprises"] }
it { expect(subject).to eq(nil) }
end
context 'when token is authorized' do
let(:roles) { ["bilans_entreprise_bdf"] }
it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end
end
end

View file

@ -0,0 +1,26 @@
describe ApiEntreprise::BilansBdfAdapter 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\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=/)
.to_return(body: body, status: status)
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles).and_return(["bilans_entreprise_bdf"])
end
context "when the SIREN is valid" do
let(:body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
let(:status) { 200 }
it '#to_params class est une Hash ?' do
expect(subject).to be_an_instance_of(Hash)
end
it "returns bilans bdf" do
expect(subject[:entreprise_bilans_bdf][0][:valeur_ajoutee_bdf]).to eq("7848792")
end
end
end