update ApiEntrepriseService
There is now the `create_etablissement` method which create etablissement with EtablissementAdapter and enqueue api_entreprise jobs to retrieve all informations we can get based on SIRET
This commit is contained in:
parent
f4ebb5d107
commit
ba5fa9aa4b
2 changed files with 34 additions and 173 deletions
|
@ -1,73 +1,28 @@
|
|||
class ApiEntrepriseService
|
||||
# Retrieve all informations we can get about a SIRET.
|
||||
# create etablissement with EtablissementAdapter
|
||||
# enqueue api_entreprise jobs to retrieve
|
||||
# all informations we can get about a SIRET.
|
||||
#
|
||||
# Returns nil if the SIRET is unknown; and nested params
|
||||
# suitable for being saved into a Etablissement object otherwise.
|
||||
# Returns nil if the SIRET is unknown
|
||||
#
|
||||
# Raises a ApiEntreprise::API::RequestFailed exception on transcient errors
|
||||
# Raises a ApiEntreprise::API::RequestFailed exception on transient errors
|
||||
# (timeout, 5XX HTTP error code, etc.)
|
||||
def self.get_etablissement_params_for_siret(siret, procedure_id, user_id = nil)
|
||||
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
|
||||
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params
|
||||
def self.create_etablissement(dossier, siret, user_id = nil)
|
||||
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, dossier.procedure.id).to_params
|
||||
return nil if etablissement_params.empty?
|
||||
|
||||
if etablissement_params.present? && entreprise_params.present?
|
||||
begin
|
||||
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
|
||||
etablissement_params.merge!(association_params)
|
||||
rescue ApiEntreprise::API::RequestFailed
|
||||
end
|
||||
etablissement = dossier.build_etablissement(etablissement_params)
|
||||
etablissement.save
|
||||
|
||||
begin
|
||||
exercices_params = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_params
|
||||
etablissement_params.merge!(exercices_params)
|
||||
rescue ApiEntreprise::API::RequestFailed
|
||||
end
|
||||
|
||||
begin
|
||||
effectifs_params = ApiEntreprise::EffectifsAdapter.new(entreprise_params[:entreprise_siren], procedure_id, *get_current_valid_month_for_effectif).to_params
|
||||
etablissement_params.merge!(effectifs_params)
|
||||
rescue ApiEntreprise::API::RequestFailed
|
||||
end
|
||||
|
||||
begin
|
||||
effectifs_annuels_params = ApiEntreprise::EffectifsAnnuelsAdapter.new(entreprise_params[:entreprise_siren], procedure_id).to_params
|
||||
etablissement_params.merge!(effectifs_annuels_params)
|
||||
rescue ApiEntreprise::API::RequestFailed
|
||||
end
|
||||
|
||||
begin
|
||||
attestation_sociale_params = ApiEntreprise::AttestationSocialeAdapter.new(entreprise_params[:entreprise_siren], procedure_id).to_params
|
||||
etablissement_params.merge!(attestation_sociale_params)
|
||||
rescue ApiEntreprise::API::RequestFailed
|
||||
end
|
||||
|
||||
begin
|
||||
attestation_fiscale_params = ApiEntreprise::AttestationFiscaleAdapter.new(entreprise_params[:entreprise_siren], procedure_id, user_id).to_params
|
||||
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)
|
||||
[
|
||||
ApiEntreprise::EntrepriseJob, ApiEntreprise::AssociationJob, ApiEntreprise::ExercicesJob,
|
||||
ApiEntreprise::EffectifsJob, ApiEntreprise::EffectifsAnnuelsJob, ApiEntreprise::AttestationSocialeJob,
|
||||
ApiEntreprise::BilansBdfJob
|
||||
].each do |job|
|
||||
job.perform_later(etablissement.id, dossier.procedure.id)
|
||||
end
|
||||
end
|
||||
ApiEntreprise::AttestationFiscaleJob.perform_later(etablissement.id, dossier.procedure.id, user_id)
|
||||
|
||||
private
|
||||
|
||||
def self.get_current_valid_month_for_effectif
|
||||
today = Date.today
|
||||
date_update = Date.new(today.year, today.month, 15)
|
||||
|
||||
if today >= date_update
|
||||
[today.strftime("%Y"), today.strftime("%m")]
|
||||
else
|
||||
date = today - 1.month
|
||||
[date.strftime("%Y"), date.strftime("%m")]
|
||||
end
|
||||
etablissement
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,129 +1,44 @@
|
|||
describe ApiEntrepriseService do
|
||||
describe '#get_etablissement_params_for_siret' do
|
||||
describe '#create_etablissement' do
|
||||
before do
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/)
|
||||
.to_return(body: entreprises_body, status: entreprises_status)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{siret}?.*token=/)
|
||||
.to_return(body: etablissements_body, status: etablissements_status)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
|
||||
.to_return(body: exercices_body, status: exercices_status)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
|
||||
.to_return(body: associations_body, status: associations_status)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/#{annee}\/#{mois}\/entreprise\/#{siren}?.*token=/)
|
||||
.to_return(body: effectifs_mensuels_body, status: effectifs_mensuels_status)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_annuels_acoss_covid\/#{siren}?.*token=/)
|
||||
.to_return(body: effectifs_annuels_body, status: effectifs_annuels_status)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
|
||||
.to_return(body: attestation_sociale_body, status: attestation_sociale_status)
|
||||
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/attestations_sociales_acoss\/#{siren}?.*token=/)
|
||||
.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)) }
|
||||
after { Timecop.return }
|
||||
|
||||
let(:siren) { '418166096' }
|
||||
let(:siret) { '41816609600051' }
|
||||
let(:rna) { 'W595001988' }
|
||||
|
||||
let(:entreprises_status) { 200 }
|
||||
let(:entreprises_body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') }
|
||||
|
||||
let(:etablissements_status) { 200 }
|
||||
let(:etablissements_body) { File.read('spec/fixtures/files/api_entreprise/etablissements.json') }
|
||||
|
||||
let(:effectifs_mensuels_status) { 200 }
|
||||
let(:effectifs_mensuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs.json') }
|
||||
let(:annee) { "2020" }
|
||||
let(:mois) { "02" }
|
||||
let(:effectif_mensuel) { 100.5 }
|
||||
|
||||
let(:effectifs_annuels_status) { 200 }
|
||||
let(:effectifs_annuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs_annuels.json') }
|
||||
let(:effectif_annuel) { 100.5 }
|
||||
|
||||
let(:attestation_sociale_status) { 200 }
|
||||
let(:attestation_sociale_body) { File.read('spec/fixtures/files/api_entreprise/attestation_sociale.json') }
|
||||
let(:attestation_sociale_url) { "https://storage.entreprise.api.gouv.fr/siade/1569156881-f749d75e2bfd443316e2e02d59015f-attestation_vigilance_acoss.pdf" }
|
||||
|
||||
let(:attestation_fiscale_status) { 200 }
|
||||
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') }
|
||||
|
||||
let(:associations_status) { 200 }
|
||||
let(:associations_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
|
||||
let(:procedure) { create(:procedure, api_entreprise_token: 'un-jeton') }
|
||||
let(:result) { ApiEntrepriseService.get_etablissement_params_for_siret(siret, procedure.id) }
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
let(:subject) { ApiEntrepriseService.create_etablissement(dossier, siret, procedure.id) }
|
||||
|
||||
before do
|
||||
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles)
|
||||
.and_return(["attestations_sociales", "attestations_fiscales", "bilans_entreprise_bdf"])
|
||||
allow_any_instance_of(ApiEntrepriseToken).to receive(:roles).and_return([])
|
||||
allow_any_instance_of(ApiEntrepriseToken).to receive(:expired?).and_return(false)
|
||||
end
|
||||
|
||||
context 'when service is up' do
|
||||
it 'should fetch etablissement params' do
|
||||
expect(result[:entreprise_siren]).to eq(siren)
|
||||
expect(result[:siret]).to eq(siret)
|
||||
expect(result[:association_rna]).to eq(rna)
|
||||
expect(result[:exercices_attributes]).to_not be_empty
|
||||
expect(result[:entreprise_effectif_mensuel]).to eq(effectif_mensuel)
|
||||
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)
|
||||
expect(subject[:siret]).to eq(siret)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when exercices api down' do
|
||||
let(:exercices_status) { 400 }
|
||||
let(:exercices_body) { '' }
|
||||
|
||||
it 'should fetch etablissement params' do
|
||||
expect(result[:entreprise_siren]).to eq(siren)
|
||||
expect(result[:siret]).to eq(siret)
|
||||
expect(result[:exercices_attributes]).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when associations api down' do
|
||||
let(:associations_status) { 400 }
|
||||
let(:associations_body) { '' }
|
||||
|
||||
it 'should fetch etablissement params' do
|
||||
expect(result[:entreprise_siren]).to eq(siren)
|
||||
expect(result[:siret]).to eq(siret)
|
||||
expect(result[:association_rna]).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when entreprise api down' do
|
||||
let(:entreprises_status) { 400 }
|
||||
let(:entreprises_body) { '' }
|
||||
|
||||
it 'should raise ApiEntreprise::API::RequestFailed' do
|
||||
expect { result }.to raise_error(ApiEntreprise::API::RequestFailed)
|
||||
[
|
||||
ApiEntreprise::EntrepriseJob, ApiEntreprise::AssociationJob, ApiEntreprise::ExercicesJob,
|
||||
ApiEntreprise::EffectifsJob, ApiEntreprise::EffectifsAnnuelsJob, ApiEntreprise::AttestationSocialeJob,
|
||||
ApiEntreprise::BilansBdfJob
|
||||
].each do |job|
|
||||
it "should enqueue #{job.class.name}" do
|
||||
expect { subject }.to have_enqueued_job(job)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when etablissement api down' do
|
||||
let(:etablissements_status) { 400 }
|
||||
let(:etablissements_status) { 504 }
|
||||
let(:etablissements_body) { '' }
|
||||
|
||||
it 'should raise ApiEntreprise::API::RequestFailed' do
|
||||
expect { result }.to raise_error(ApiEntreprise::API::RequestFailed)
|
||||
expect { subject }.to raise_error(ApiEntreprise::API::RequestFailed)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -132,16 +47,7 @@ describe ApiEntrepriseService do
|
|||
let(:etablissements_body) { '' }
|
||||
|
||||
it 'should return nil' do
|
||||
expect(result).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when entreprise not found' do
|
||||
let(:etablissements_status) { 404 }
|
||||
let(:etablissements_body) { '' }
|
||||
|
||||
it 'should return nil' do
|
||||
expect(result).to be_nil
|
||||
expect(subject).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue