fetch and store bilans bdf

This commit is contained in:
Christophe Robillard 2020-04-30 08:43:34 +02:00
parent 85b2e9ef44
commit b12226c667
5 changed files with 33 additions and 3 deletions

View file

@ -46,6 +46,13 @@ class ApiEntrepriseService
etablissement_params.merge!(attestation_fiscale_params)
rescue ApiEntreprise::API::RequestFailed
end
begin
bilans_bdf_params = ApiEntreprise::BilansBdfAdapter.new(entreprise_params[:entreprise_siren], procedure_id).to_params
etablissement_params.merge!(bilans_bdf_params)
rescue ApiEntreprise::API::RequestFailed
end
etablissement_params.merge(entreprise_params)
end
end

View file

@ -0,0 +1,6 @@
class AddBilansBdfToEtablissements < ActiveRecord::Migration[5.2]
def change
add_column :etablissements, :entreprise_bilans_bdf, :jsonb
add_column :etablissements, :entreprise_bilans_bdf_monnaie, :string
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_04_23_171759) do
ActiveRecord::Schema.define(version: 2020_04_29_191305) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -316,6 +316,8 @@ ActiveRecord::Schema.define(version: 2020_04_23_171759) do
t.decimal "entreprise_effectif_mensuel"
t.decimal "entreprise_effectif_annuel"
t.string "entreprise_effectif_annuel_annee"
t.jsonb "entreprise_bilans_bdf"
t.string "entreprise_bilans_bdf_monnaie"
t.index ["dossier_id"], name: "index_etablissements_on_dossier_id"
end

View file

@ -227,6 +227,9 @@ describe Users::DossiersController, type: :controller do
let(:api_entreprise_attestation_fiscale_status) { 200 }
let(:api_entreprise_attestation_fiscale_body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') }
let(:api_entreprise_bilans_bdf_status) { 200 }
let(:api_entreprise_bilans_bdf_body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
def stub_api_entreprise_requests
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
.to_return(status: api_etablissement_status, body: api_etablissement_body)
@ -248,12 +251,15 @@ describe Users::DossiersController, type: :controller do
.to_return(body: api_entreprise_attestation_fiscale_body, status: api_entreprise_attestation_fiscale_status)
stub_request(:get, "https://storage.entreprise.api.gouv.fr/siade/1569156756-f6b7779f99fa95cd60dc03c04fcb-attestation_fiscale_dgfip.pdf")
.to_return(body: "body attestation", status: 200)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=/)
.to_return(body: api_entreprise_bilans_bdf_body, status: api_entreprise_bilans_bdf_status)
end
before do
sign_in(user)
stub_api_entreprise_requests
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles).and_return(["attestations_fiscales", "attestations_sociales"])
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles)
.and_return(["attestations_fiscales", "attestations_sociales", "bilans_entreprise_bdf"])
end
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
after { Timecop.return }
@ -351,6 +357,7 @@ describe Users::DossiersController, type: :controller do
expect(dossier.etablissement.entreprise_effectif_annuel).to be_present
expect(dossier.etablissement.entreprise_attestation_sociale).to be_attached
expect(dossier.etablissement.entreprise_attestation_fiscale).to be_attached
expect(dossier.etablissement.entreprise_bilans_bdf).to be_present
end
end
end

View file

@ -19,6 +19,8 @@ describe ApiEntrepriseService do
.to_return(body: attestation_sociale_body, status: attestation_sociale_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_fiscales_dgfip\/#{siren}?.*token=/)
.to_return(body: attestation_fiscale_body, status: attestation_fiscale_status)
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}?.*token=/)
.to_return(body: bilans_bdf_body, status: bilans_bdf_status)
end
before { Timecop.freeze(Time.zone.local(2020, 3, 14)) }
@ -52,6 +54,10 @@ describe ApiEntrepriseService do
let(:attestation_fiscale_body) { File.read('spec/fixtures/files/api_entreprise/attestation_fiscale.json') }
let(:attestation_fiscale_url) { "https://storage.entreprise.api.gouv.fr/siade/1569156756-f6b7779f99fa95cd60dc03c04fcb-attestation_fiscale_dgfip.pdf" }
let(:bilans_bdf_status) { 200 }
let(:bilans_bdf_body) { File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json') }
let(:bilans_bdf) { JSON.parse(bilans_bdf_body, symbolize_names: true)[:bilans] }
let(:exercices_status) { 200 }
let(:exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') }
@ -62,7 +68,8 @@ describe ApiEntrepriseService do
let(:result) { ApiEntrepriseService.get_etablissement_params_for_siret(siret, procedure.id) }
before do
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles).and_return(["attestations_sociales", "attestations_fiscales"])
allow_any_instance_of(Procedure).to receive(:api_entreprise_roles)
.and_return(["attestations_sociales", "attestations_fiscales", "bilans_entreprise_bdf"])
end
context 'when service is up' do
@ -75,6 +82,7 @@ describe ApiEntrepriseService do
expect(result[:entreprise_effectif_annuel]).to eq(effectif_annuel)
expect(result[:entreprise_attestation_sociale_url]).to eq(attestation_sociale_url)
expect(result[:entreprise_attestation_fiscale_url]).to eq(attestation_fiscale_url)
expect(result[:entreprise_bilans_bdf]).to eq(bilans_bdf)
end
end