diff --git a/lib/tasks/2018_05_14_add_annotation_privee_to_procedure.rake b/lib/tasks/2018_05_14_add_annotation_privee_to_procedure.rake new file mode 100644 index 000000000..5db1d468a --- /dev/null +++ b/lib/tasks/2018_05_14_add_annotation_privee_to_procedure.rake @@ -0,0 +1,54 @@ +namespace :'2018_05_14_add_annotation_privee_to_procedure' do + task add: :environment do + procedure_id = ENV['PROCEDURE_ID'] || 3723 + add_an_annotation_privee(procedure_id) + update_description(procedure_id) + end + + def add_an_annotation_privee(procedure_id) + new_tdc_order_place = 7 + + TypeDeChamp + .where(procedure_id: procedure_id, private: true) + .where('order_place >= ?', new_tdc_order_place) + .each do |tdc| + tdc.update_attribute(:order_place, tdc.order_place + 1) + end + + new_tdc = TypesDeChamp::TextTypeDeChamp.create( + procedure_id: procedure_id, + private: true, + libelle: 'URL Espace de consultation', + order_place: new_tdc_order_place, + type_champ: 'text', + description: 'L’instructeur renseigne l’URL du site de dépôt des observations ou la page web de la préfecture où est mentionné l’email pour déposer les commentaires' + ) + + Dossier.includes(champs: :type_de_champ).where(procedure_id: procedure_id).all.each do |dossier| + Champs::TextChamp.create( + dossier: dossier, + type_de_champ: new_tdc, + private: true + ) + end + end + + def update_description(procedure_id) + TypeDeChamp.find_by( + private: true, + description: "L'instructeur en préfecture saisie la date de publication de l'avis à consultation du public ", + procedure_id: procedure_id + )&.update( + description: "L'instructeur en préfecture saisie la première date de publication de l'avis à consultation du public" + ) + + TypeDeChamp.find_by( + private: false, + libelle: "Fichier(s) Etude d'impact", + procedure_id: procedure_id + )&.update( + libelle: "Fichier Etude d'impact", + description: "Vous devez télé-charger votre fichier constituant le document dit \"étude d'impact\" de votre projet.\r\nObligatoire." + ) + end +end diff --git a/spec/lib/rake/2018_05_14_add_annotation_privee_to_procedure_spec.rb b/spec/lib/rake/2018_05_14_add_annotation_privee_to_procedure_spec.rb new file mode 100644 index 000000000..70d377cf6 --- /dev/null +++ b/spec/lib/rake/2018_05_14_add_annotation_privee_to_procedure_spec.rb @@ -0,0 +1,41 @@ +describe '2018_05_14_add_annotation_privee_to_procedure' do + let!(:user) { create(:user) } + let!(:procedure) do + procedure = create(:procedure) + 10.times do |i| + TypesDeChamp::NumberTypeDeChamp.create( + procedure: procedure, + private: false, + libelle: 'libelle', + order_place: i, + type_champ: 'number' + ) + + TypesDeChamp::NumberTypeDeChamp.create( + procedure: procedure, + private: true, + libelle: 'libelle', + order_place: i, + type_champ: 'number' + ) + end + procedure + end + let!(:dossier) { Dossier.create(procedure: procedure, user: user, state: 'brouillon') } + let(:rake_task) { Rake::Task['2018_05_14_add_annotation_privee_to_procedure:add'] } + + before(:all) { TPS::Application.load_tasks } + + before do + ENV['PROCEDURE_ID'] = procedure.id.to_s + rake_task.invoke + procedure.reload + end + + after { rake_task.reenable } + + it { expect(procedure.types_de_champ.count).to eq(10) } + it { expect(procedure.types_de_champ_private.count).to eq(11) } + it { expect(dossier.champs_private.includes(:type_de_champ).map(&:order_place).sort).to match((0..10).to_a) } + it { expect(procedure.types_de_champ_private.find_by(order_place: 7).type_champ).to eq('text') } +end