From db3ef195ad8ff27194b625791b687847caee3888 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Tue, 7 Apr 2020 19:29:14 +0200 Subject: [PATCH] fetch and show effectif mensuel - fetch and store effectif mensuel (at the creation of the dossier or when updating a siret field - show effectif mensuel only for instructeurs --- app/helpers/application_helper.rb | 8 ++++++++ app/services/api_entreprise_service.rb | 20 +++++++++++++++++++ .../dossiers/_identite_entreprise.html.haml | 6 ++++++ ...dd_effectifs_mensuels_to_etablissements.rb | 7 +++++++ db/schema.rb | 3 +++ .../users/dossiers_controller_spec.rb | 13 ++++++++++-- spec/features/users/dossier_creation_spec.rb | 6 +++++- spec/services/api_entreprise_service_spec.rb | 12 +++++++++++ 8 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20200407135256_add_effectifs_mensuels_to_etablissements.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index acdabec8a..262f142a5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -130,6 +130,14 @@ module ApplicationHelper datetime.present? ? I18n.l(datetime) : '' end + def try_format_mois_effectif(etablissement) + if etablissement.effectif_mois.present? && etablissement.effectif_annee.present? + [etablissement.effectif_mois, etablissement.effectif_annee].join('/') + else + '' + end + end + def dismiss_outdated_browser_banner cookies[:dismissed_outdated_browser_banner] = { value: 'true', diff --git a/app/services/api_entreprise_service.rb b/app/services/api_entreprise_service.rb index 32fa8ada7..2b48acf8a 100644 --- a/app/services/api_entreprise_service.rb +++ b/app/services/api_entreprise_service.rb @@ -23,7 +23,27 @@ class ApiEntrepriseService 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 + etablissement_params.merge(entreprise_params) end end + + 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 + end end diff --git a/app/views/shared/dossiers/_identite_entreprise.html.haml b/app/views/shared/dossiers/_identite_entreprise.html.haml index f1d18f84d..b71041c74 100644 --- a/app/views/shared/dossiers/_identite_entreprise.html.haml +++ b/app/views/shared/dossiers/_identite_entreprise.html.haml @@ -27,6 +27,12 @@ %tr %th.libelle Date de création : %td= try_format_date(etablissement.entreprise.date_creation) + - if profile == 'instructeur' + %tr + %th.libelle + Effectif mensuel + = try_format_mois_effectif(etablissement) + %td= etablissement.effectif_mensuel %tr %th.libelle Effectif de l'organisation : %td= effectif(etablissement) diff --git a/db/migrate/20200407135256_add_effectifs_mensuels_to_etablissements.rb b/db/migrate/20200407135256_add_effectifs_mensuels_to_etablissements.rb new file mode 100644 index 000000000..926a46c41 --- /dev/null +++ b/db/migrate/20200407135256_add_effectifs_mensuels_to_etablissements.rb @@ -0,0 +1,7 @@ +class AddEffectifsMensuelsToEtablissements < ActiveRecord::Migration[5.2] + def change + add_column :etablissements, :effectif_mois, :string + add_column :etablissements, :effectif_annee, :string + add_column :etablissements, :effectif_mensuel, :decimal + end +end diff --git a/db/schema.rb b/db/schema.rb index 4ea488b03..91ca9849b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -310,6 +310,9 @@ ActiveRecord::Schema.define(version: 2020_04_09_075320) do t.datetime "created_at" t.datetime "updated_at" t.boolean "diffusable_commercialement" + t.string "effectif_mois" + t.string "effectif_annee" + t.decimal "effectif_mensuel" t.index ["dossier_id"], name: "index_etablissements_on_dossier_id" end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index dfe54c93b..98bc45d37 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -207,6 +207,11 @@ describe Users::DossiersController, type: :controller do let(:api_entreprise_status) { 200 } let(:api_entreprise_body) { File.read('spec/fixtures/files/api_entreprise/entreprises.json') } + let(:api_entreprise_effectifs_mensuels_status) { 200 } + let(:api_entreprise_effectifs_mensuels_body) { File.read('spec/fixtures/files/api_entreprise/effectifs.json') } + let(:annee) { "2020" } + let(:mois) { "02" } + let(:api_exercices_status) { 200 } let(:api_exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') } @@ -222,12 +227,16 @@ describe Users::DossiersController, type: :controller do .to_return(status: api_exercices_status, body: api_exercices_body) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/#{siret}?.*token=/) .to_return(status: api_association_status, body: api_association_body) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/#{annee}\/#{mois}\/entreprise\/#{siren}?.*token=/) + .to_return(body: api_entreprise_effectifs_mensuels_body, status: api_entreprise_effectifs_mensuels_status) end before do sign_in(user) stub_api_entreprise_requests end + before { Timecop.freeze(Time.zone.local(2020, 3, 14)) } + after { Timecop.return } subject! { post :update_siret, params: { id: dossier.id, user: { siret: params_siret } } } @@ -266,8 +275,7 @@ describe Users::DossiersController, type: :controller do end context 'with a valid SIRET' do - let(:params_siret) { '440 117 620 01530' } - + let(:params_siret) { '418 166 096 00051' } context 'When API-Entreprise is down' do let(:api_etablissement_status) { 502 } let(:api_body_status) { File.read('spec/fixtures/files/api_entreprise/exercices_unavailable.json') } @@ -319,6 +327,7 @@ describe Users::DossiersController, type: :controller do expect(dossier.etablissement.entreprise).to be_present expect(dossier.etablissement.exercices).to be_present expect(dossier.etablissement.association?).to be(true) + expect(dossier.etablissement.effectif_mensuel).to be_present end end end diff --git a/spec/features/users/dossier_creation_spec.rb b/spec/features/users/dossier_creation_spec.rb index 6a1a357a8..5849cd1b4 100644 --- a/spec/features/users/dossier_creation_spec.rb +++ b/spec/features/users/dossier_creation_spec.rb @@ -1,6 +1,6 @@ feature 'Creating a new dossier:' do let(:user) { create(:user) } - let(:siret) { '40307130100044' } + let(:siret) { '41816609600051' } let(:siren) { siret[0...9] } context 'when the user is already signed in' do @@ -74,7 +74,11 @@ feature 'Creating a new dossier:' do .to_return(status: 200, body: File.read('spec/fixtures/files/api_entreprise/exercices.json')) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/associations\/#{siret}?.*token=/) .to_return(status: 404, body: '') + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/effectifs_mensuels_acoss_covid\/2020\/02\/entreprise\/#{siren}?.*token=/) + .to_return(status: 404, body: '') end + before { Timecop.freeze(Time.zone.local(2020, 3, 14)) } + after { Timecop.return } scenario 'the user can enter the SIRET of its etablissement and create a new draft' do visit commencer_path(path: procedure.path) diff --git a/spec/services/api_entreprise_service_spec.rb b/spec/services/api_entreprise_service_spec.rb index 887333b33..af5aec64b 100644 --- a/spec/services/api_entreprise_service_spec.rb +++ b/spec/services/api_entreprise_service_spec.rb @@ -9,8 +9,13 @@ describe ApiEntrepriseService do .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) end + before { Timecop.freeze(Time.zone.local(2020, 3, 14)) } + after { Timecop.return } + let(:siren) { '418166096' } let(:siret) { '41816609600051' } let(:rna) { 'W595001988' } @@ -21,6 +26,12 @@ describe ApiEntrepriseService do 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(:exercices_status) { 200 } let(:exercices_body) { File.read('spec/fixtures/files/api_entreprise/exercices.json') } @@ -35,6 +46,7 @@ describe ApiEntrepriseService do expect(result[:siret]).to eq(siret) expect(result[:association_rna]).to eq(rna) expect(result[:exercices_attributes]).to_not be_empty + expect(result[:effectif_mensuel]).to eq(effectif_mensuel) end end