From 51972ba728319dcc78dce829fedbad2dc7d382f1 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 29 Mar 2018 15:25:09 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Entreprise/Association=20moulinettes?= =?UTF-8?q?=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...8_03_29_inline_entreprise_association.rake | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/tasks/2018_03_29_inline_entreprise_association.rake diff --git a/lib/tasks/2018_03_29_inline_entreprise_association.rake b/lib/tasks/2018_03_29_inline_entreprise_association.rake new file mode 100644 index 000000000..27518910a --- /dev/null +++ b/lib/tasks/2018_03_29_inline_entreprise_association.rake @@ -0,0 +1,53 @@ +namespace :'2018_03_29_inline_entreprise_association' do + task fix_date_fin_exercice: :environment do + Exercice.where(date_fin_exercice: nil).find_each do |exercice| + exercice.update_column(:date_fin_exercice, exercice.dateFinExercice) + end + end + + task fix_missing_entreprise: :environment do + Etablissement.includes(:entreprise).where(entreprise_siren: nil).find_each do |etablissement| + dossier_id = etablissement.dossier_id + + if !etablissement.entreprise + etablissement.entreprise = Dossier.find_by(id: dossier_id)&.entreprise + end + + etablissement.save + end + end + + task inline_entreprise_association: :environment do + Etablissement.includes(entreprise: :rna_information, exercices: []).where(entreprise_siren: nil).find_each do |etablissement| + entreprise = etablissement.entreprise + + if entreprise + etablissement.entreprise_siren = entreprise.siren + etablissement.entreprise_capital_social = entreprise.capital_social + etablissement.entreprise_numero_tva_intracommunautaire = entreprise.numero_tva_intracommunautaire + etablissement.entreprise_forme_juridique = entreprise.forme_juridique + etablissement.entreprise_forme_juridique_code = entreprise.forme_juridique_code + etablissement.entreprise_nom_commercial = entreprise.nom_commercial + etablissement.entreprise_raison_sociale = entreprise.raison_sociale + etablissement.entreprise_siret_siege_social = entreprise.siret_siege_social + etablissement.entreprise_code_effectif_entreprise = entreprise.code_effectif_entreprise + etablissement.entreprise_date_creation = entreprise.date_creation + etablissement.entreprise_nom = entreprise.nom + etablissement.entreprise_prenom = entreprise.prenom + + association = entreprise.rna_information + + if association && association.association_id + etablissement.association_rna = association.association_id + etablissement.association_titre = association.titre + etablissement.association_objet = association.objet + etablissement.association_date_creation = association.date_creation + etablissement.association_date_declaration = association.date_declaration + etablissement.association_date_publication = association.date_publication + end + + etablissement.save + end + end + end +end From 1f536c94abdcc194882216bb2fe510cfef8824f3 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 3 Apr 2018 18:26:55 +0200 Subject: [PATCH 2/4] Individual: missing individual part in old controller --- app/controllers/users/dossiers_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index a32f5d522..815b00ede 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -198,7 +198,7 @@ class Users::DossiersController < UsersController end def update_params - params.require(:dossier).permit(:id, :autorisation_donnees, individual_attributes: [:gender, :nom, :prenom, :birthdate]) + params.require(:dossier).permit(:id, :autorisation_donnees) end def individual_errors From c019cb623a4a67f66e820e7ffe578154c1cb0ed3 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 3 Apr 2018 16:38:54 +0200 Subject: [PATCH 3/4] Individual: use birthdate as a ... date :) --- app/controllers/users/dossiers_controller.rb | 4 +++- app/models/individual.rb | 22 ++++--------------- app/views/dossiers/_infos_dossier.html.haml | 2 +- .../dossiers/_identite_individual.html.haml | 2 +- spec/factories/individual.rb | 2 +- spec/features/users/dossier_creation_spec.rb | 4 ++-- spec/models/individual_spec.rb | 10 +++------ .../dossiers/show.html.haml_spec.rb | 2 +- 8 files changed, 16 insertions(+), 32 deletions(-) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 815b00ede..12fd0573a 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -92,7 +92,9 @@ class Users::DossiersController < UsersController individual.update_column :gender, @facade.dossier.france_connect_information.gender individual.update_column :nom, @facade.dossier.france_connect_information.family_name individual.update_column :prenom, @facade.dossier.france_connect_information.given_name - individual.update_column :birthdate, @facade.dossier.france_connect_information.birthdate.iso8601 + + individual.birthdate = @facade.dossier.france_connect_information.birthdate + individual.save end rescue ActiveRecord::RecordNotFound diff --git a/app/models/individual.rb b/app/models/individual.rb index b5ac55067..5e3fd2ea6 100644 --- a/app/models/individual.rb +++ b/app/models/individual.rb @@ -5,26 +5,12 @@ class Individual < ApplicationRecord validates :gender, presence: true, allow_nil: false, on: :update validates :nom, presence: true, allow_blank: false, allow_nil: false, on: :update validates :prenom, presence: true, allow_blank: false, allow_nil: false, on: :update - validates :birthdate, format: { with: /\A\d{4}\-\d{2}\-\d{2}\z/, message: "La date n'est pas au format AAAA-MM-JJ" }, allow_nil: true - before_validation :set_iso_date, if: -> { birthdate_changed? } - before_save :save_birthdate_in_datetime_format - - private - - def set_iso_date - if birthdate.present? && - birthdate =~ /\A\d{2}\/\d{2}\/\d{4}\z/ - self.birthdate = Date.parse(birthdate).iso8601 - end + def birthdate + second_birthdate end - def save_birthdate_in_datetime_format - if birthdate.present? - begin - self.second_birthdate = Date.parse(birthdate) - rescue - end - end + def birthdate=(date) + self.second_birthdate = date end end diff --git a/app/views/dossiers/_infos_dossier.html.haml b/app/views/dossiers/_infos_dossier.html.haml index c6c3cbde6..d5e0f1ec1 100644 --- a/app/views/dossiers/_infos_dossier.html.haml +++ b/app/views/dossiers/_infos_dossier.html.haml @@ -20,7 +20,7 @@ .row .col-xs-6.depositaire-label Date de naissance .col-xs-1.comments-off= "-" - .col-xs-5.depositaire-info= @facade.individual.birthdate + .col-xs-4.depositaire-info= @facade.individual.birthdate&.strftime("%d/%m/%Y") .row.margin-top-20 - if @facade.champs.present? diff --git a/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml b/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml index 27d8e44a5..c1d08eb5d 100644 --- a/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml +++ b/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml @@ -12,4 +12,4 @@ - if individual.birthdate.present? %tr %th.libelle Date de naissance : - %td= Date.parse(individual.birthdate).strftime("%d/%m/%Y") + %td= individual.birthdate&.strftime("%d/%m/%Y") diff --git a/spec/factories/individual.rb b/spec/factories/individual.rb index 05128e117..75e0c4b19 100644 --- a/spec/factories/individual.rb +++ b/spec/factories/individual.rb @@ -3,6 +3,6 @@ FactoryBot.define do gender 'M.' nom 'Julien' prenom 'Xavier' - birthdate '1991-11-01' + birthdate Date.new(1991, 11, 01) end end diff --git a/spec/features/users/dossier_creation_spec.rb b/spec/features/users/dossier_creation_spec.rb index aed39137e..84572b3d5 100644 --- a/spec/features/users/dossier_creation_spec.rb +++ b/spec/features/users/dossier_creation_spec.rb @@ -30,7 +30,7 @@ feature 'As a User I wanna create a dossier' do expect(page).to have_current_path(modifier_dossier_path(procedure_for_individual.dossiers.last)) - expect(user.dossiers.first.individual.birthdate).to eq("1987-10-14") + expect(user.dossiers.first.individual.birthdate).to eq(Date.new(1987, 10, 14)) end scenario "with a basic text input field for birthdate (type='date' unsupported)" do @@ -42,7 +42,7 @@ feature 'As a User I wanna create a dossier' do expect(page).to have_current_path(modifier_dossier_path(procedure_for_individual.dossiers.last)) - expect(user.dossiers.first.individual.birthdate).to eq("1987-10-14") + expect(user.dossiers.first.individual.birthdate).to eq(Date.new(1987, 10, 14)) end end diff --git a/spec/models/individual_spec.rb b/spec/models/individual_spec.rb index 43d627139..6b11324f1 100644 --- a/spec/models/individual_spec.rb +++ b/spec/models/individual_spec.rb @@ -4,7 +4,6 @@ describe Individual do it { is_expected.to have_db_column(:gender) } it { is_expected.to have_db_column(:nom) } it { is_expected.to have_db_column(:prenom) } - it { is_expected.to have_db_column(:birthdate) } it { is_expected.to belong_to(:dossier) } describe "#save" do @@ -21,24 +20,21 @@ describe Individual do context "and the format is dd/mm/yyy " do let(:birthdate_from_user) { "12/11/1980" } - it { expect(individual.valid?).to be true } - it { expect(individual.birthdate).to eq("1980-11-12") } + it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) } end context "and the format is ISO" do let(:birthdate_from_user) { "1980-11-12" } - it { expect(individual.valid?).to be true } - it { expect(individual.birthdate).to eq("1980-11-12") } + it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) } end context "and the format is WTF" do let(:birthdate_from_user) { "1980 1 12" } - it { expect(individual.valid?).to be false } - it { expect(individual.birthdate).to eq("1980 1 12") } + it { expect(individual.birthdate).to be_nil } it { expect(individual.second_birthdate).to be_nil } end end diff --git a/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb b/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb index b45e7123f..7ac72030c 100644 --- a/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb +++ b/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb @@ -34,6 +34,6 @@ describe 'new_gestionnaire/dossiers/show.html.haml', type: :view do it { expect(rendered).to include(individual.gender) } it { expect(rendered).to include(individual.nom) } it { expect(rendered).to include(individual.prenom) } - it { expect(rendered).to include(Date.parse(individual.birthdate).strftime("%d/%m/%Y")) } + it { expect(rendered).to include(individual.birthdate.strftime("%d/%m/%Y")) } end end From 8c1c2f32caf0515958ac9d019d2e336610a53b7e Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 3 Apr 2018 18:05:29 +0200 Subject: [PATCH 4/4] [Fix #1724] Ensure that printed json is safe --- app/helpers/application_helper.rb | 7 +++++ .../new_gestionnaire/dossiers/_map.html.haml | 6 ++--- spec/helpers/application_helper_spec.rb | 14 ++++++++++ .../new_gestionnaire/dossiers/_map_spec.rb | 27 +++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 spec/views/new_gestionnaire/dossiers/_map_spec.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index dbc16a10f..7fddc56a0 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -30,4 +30,11 @@ module ApplicationHelper root_path end end + + def ensure_safe_json(json) + JSON.parse(json).to_json + rescue Exception => e + Raven.capture_exception(e) + {} + end end diff --git a/app/views/new_gestionnaire/dossiers/_map.html.haml b/app/views/new_gestionnaire/dossiers/_map.html.haml index 6ad0d9300..85ecbafc4 100644 --- a/app/views/new_gestionnaire/dossiers/_map.html.haml +++ b/app/views/new_gestionnaire/dossiers/_map.html.haml @@ -15,6 +15,6 @@ :javascript var getPositionUrl = "#{position_gestionnaire_dossier_path(dossier.procedure, dossier)}"; - var dossierJsonLatLngs = #{dossier.json_latlngs}; - var dossierCadastres = #{dossier.cadastres.to_json}; - var dossierQuartiersPrioritaires = #{dossier.quartier_prioritaires.to_json}; + var dossierJsonLatLngs = #{raw(ensure_safe_json(dossier.json_latlngs))}; + var dossierCadastres = #{raw(ensure_safe_json(dossier.cadastres.to_json))}; + var dossierQuartiersPrioritaires = #{raw(ensure_safe_json(dossier.quartier_prioritaires.to_json))}; diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 30a7becef..22c2f9b06 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -17,4 +17,18 @@ describe ApplicationHelper do it { is_expected.to be_nil } end end + + describe "#ensure_safe_json" do + subject { ensure_safe_json(json) } + + context "with a dirty json" do + let(:json) { "alert('haha');" } + it { is_expected.to eq({}) } + end + + context 'with a correct json' do + let(:json) { '[[{"lat": 2.0, "lng": 102.0}, {"lat": 3.0, "lng": 103.0}, {"lat": 2.0, "lng": 102.0}], [{"lat": 2.0, "lng": 102.0}, {"lat": 3.0, "lng": 103.0}, {"lat": 2.0, "lng": 102.0}]]' } + it { is_expected.to eq("[[{\"lat\":2.0,\"lng\":102.0},{\"lat\":3.0,\"lng\":103.0},{\"lat\":2.0,\"lng\":102.0}],[{\"lat\":2.0,\"lng\":102.0},{\"lat\":3.0,\"lng\":103.0},{\"lat\":2.0,\"lng\":102.0}]]") } + end + end end diff --git a/spec/views/new_gestionnaire/dossiers/_map_spec.rb b/spec/views/new_gestionnaire/dossiers/_map_spec.rb new file mode 100644 index 000000000..b74392134 --- /dev/null +++ b/spec/views/new_gestionnaire/dossiers/_map_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe 'new_gestionnaire/dossiers/_map.html.haml', type: :view do + subject do + render(partial: 'new_gestionnaire/dossiers/map.html.haml', locals: { dossier: dossier }) + end + + describe "javascript variables printing" do + let(:dossier) { create(:dossier, :with_entreprise, json_latlngs: json_latlngs) } + + context 'with a correct json' do + let(:json_latlngs) { "[[{\"lat\":50.659255436656736,\"lng\":3.080635070800781},{\"lat\":50.659255436656736,\"lng\":3.079690933227539},{\"lat\":50.659962770886516,\"lng\":3.0800342559814453},{\"lat\":50.659962770886516,\"lng\":3.0811500549316406},{\"lat\":50.659255436656736,\"lng\":3.080635070800781}]]" } + + before { subject } + + it { expect(rendered).to have_content('var dossierJsonLatLngs = [[{"lat":50.659255436656736,"lng":3.080635070800781},{"lat":50.659255436656736,"lng":3.079690933227539},{"lat":50.659962770886516,"lng":3.0800342559814453},{"lat":50.659962770886516,"lng":3.0811500549316406},{"lat":50.659255436656736,"lng":3.080635070800781}]];') } + end + + context 'without a correct json' do + let(:json_latlngs) { "dossier" } + + before { subject } + + it { expect(rendered).to have_content('var dossierJsonLatLngs = {};') } + end + end +end