From 27f9a820cc59652c5f9095b8e2affdcf33f05ded Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 27 Apr 2022 16:28:22 +0200 Subject: [PATCH 1/9] feat(dossier): add preview flag --- app/models/dossier.rb | 4 +++- .../20220315124100_add_preview_to_dossiers.rb | 6 ++++++ db/schema.rb | 3 ++- ...efault_for_procedure_preview_on_dossiers.rake | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20220315124100_add_preview_to_dossiers.rb create mode 100644 lib/tasks/deployment/20220427195148_set_default_for_procedure_preview_on_dossiers.rake diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 08b59df06..520190c88 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -26,6 +26,7 @@ # last_commentaire_updated_at :datetime # motivation :text # private_search_terms :text +# for_procedure_preview :boolean # processed_at :datetime # search_terms :text # state :string @@ -205,13 +206,14 @@ class Dossier < ApplicationRecord scope :not_archived, -> { where(archived: false) } scope :hidden_by_user, -> { where.not(hidden_by_user_at: nil) } scope :hidden_by_administration, -> { where.not(hidden_by_administration_at: nil) } - scope :visible_by_user, -> { where(hidden_by_user_at: nil) } + scope :visible_by_user, -> { where(for_procedure_preview: false).or(where(for_procedure_preview: nil)).where(hidden_by_user_at: nil) } scope :visible_by_administration, -> { state_not_brouillon .where(hidden_by_administration_at: nil) .merge(visible_by_user.or(state_not_en_construction)) } scope :visible_by_user_or_administration, -> { visible_by_user.or(visible_by_administration) } + scope :for_procedure_preview, -> { where(for_procedure_preview: true) } scope :order_by_updated_at, -> (order = :desc) { order(updated_at: order) } scope :order_by_created_at, -> (order = :asc) { order(depose_at: order, created_at: order, id: order) } diff --git a/db/migrate/20220315124100_add_preview_to_dossiers.rb b/db/migrate/20220315124100_add_preview_to_dossiers.rb new file mode 100644 index 000000000..cff3ee065 --- /dev/null +++ b/db/migrate/20220315124100_add_preview_to_dossiers.rb @@ -0,0 +1,6 @@ +class AddPreviewToDossiers < ActiveRecord::Migration[6.1] + def change + add_column :dossiers, :for_procedure_preview, :boolean + change_column_default :dossiers, :for_procedure_preview, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 63fc7466c..f28167ade 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_04_25_140107) do +ActiveRecord::Schema.define(version: 2022_04_26_140107) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -314,6 +314,7 @@ ActiveRecord::Schema.define(version: 2022_04_25_140107) do t.datetime "en_construction_close_to_expiration_notice_sent_at" t.interval "en_construction_conservation_extension", default: "PT0S" t.datetime "en_instruction_at" + t.boolean "for_procedure_preview", default: false t.bigint "groupe_instructeur_id" t.datetime "groupe_instructeur_updated_at" t.datetime "hidden_at" diff --git a/lib/tasks/deployment/20220427195148_set_default_for_procedure_preview_on_dossiers.rake b/lib/tasks/deployment/20220427195148_set_default_for_procedure_preview_on_dossiers.rake new file mode 100644 index 000000000..d43bcd5da --- /dev/null +++ b/lib/tasks/deployment/20220427195148_set_default_for_procedure_preview_on_dossiers.rake @@ -0,0 +1,16 @@ +namespace :after_party do + desc 'Deployment task: set_default_for_procedure_preview_on_dossiers' + task set_default_for_procedure_preview_on_dossiers: :environment do + puts "Running deploy task 'set_default_for_procedure_preview_on_dossiers'" + + Dossier + .where(for_procedure_preview: nil) + .in_batches(of: 5_000) + .update_all(for_procedure_preview: false) + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end From 68ca9efba0997c2e78e913199c281b35428b5035 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 7 Apr 2022 11:40:53 +0200 Subject: [PATCH 2/9] feat(dossier): use persisted dossier for preview --- .../administrateurs/procedures_controller.rb | 2 +- app/models/dossier.rb | 4 ++-- app/models/procedure_revision.rb | 13 +++++++++++++ .../administrateurs/procedures_controller_spec.rb | 6 +++++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 2388508db..96d6847fb 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -44,7 +44,7 @@ module Administrateurs end def apercu - @dossier = procedure_without_control.draft_revision.new_dossier + @dossier = procedure_without_control.draft_revision.dossier_for_preview(current_user) @tab = apercu_tab end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 520190c88..34c36b010 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -544,7 +544,7 @@ class Dossier < ApplicationRecord end def can_transition_to_en_construction? - brouillon? && procedure.dossier_can_transition_to_en_construction? + brouillon? && procedure.dossier_can_transition_to_en_construction? && !for_procedure_preview? end def can_repasser_en_instruction? @@ -1246,7 +1246,7 @@ class Dossier < ApplicationRecord end def send_draft_notification_email - if brouillon? && !procedure.declarative? + if brouillon? && !procedure.declarative? && !for_procedure_preview? DossierMailer.notify_new_draft(self).deliver_later end end diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 09d425847..942ee2ea7 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -132,6 +132,19 @@ class ProcedureRevision < ApplicationRecord ) end + def dossier_for_preview(user) + dossier = Dossier + .create_with(groupe_instructeur: procedure.defaut_groupe_instructeur_for_new_dossier) + .find_or_initialize_by(revision: self, user: user, for_procedure_preview: true, state: Dossier.states.fetch(:brouillon)) + + if dossier.new_record? + dossier.build_default_individual + dossier.save! + end + + dossier + end + private def compare_attestation_template(from_at, to_at) diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index a53d204fc..b34663ef1 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -23,7 +23,11 @@ describe Administrateurs::ProceduresController, type: :controller do get :apercu, params: { id: procedure.id } end - it { expect(response).to have_http_status(:ok) } + it do + expect(response).to have_http_status(:ok) + expect(procedure.dossiers.visible_by_user).to be_empty + expect(procedure.dossiers.for_procedure_preview).not_to be_empty + end end let(:procedure_params) { From 5c77bc139afa36812f98258c6d730dce177713e6 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 7 Apr 2022 11:41:35 +0200 Subject: [PATCH 3/9] refactor(carto): remove preview special case --- app/javascript/components/MapEditor/hooks.ts | 17 ++++------------- app/javascript/components/MapEditor/index.tsx | 10 ++++------ .../dossiers/editable_champs/_carte.html.haml | 4 +--- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/app/javascript/components/MapEditor/hooks.ts b/app/javascript/components/MapEditor/hooks.ts index 8c941ac58..9380a28fc 100644 --- a/app/javascript/components/MapEditor/hooks.ts +++ b/app/javascript/components/MapEditor/hooks.ts @@ -23,7 +23,7 @@ export type DeleteFeatures = (params: { export function useFeatureCollection( initialFeatureCollection: FeatureCollection, - { url, enabled = true }: { url: string; enabled: boolean } + { url }: { url: string } ) { const [error, onError] = useError(); const [featureCollection, setFeatureCollection] = useState( @@ -96,9 +96,6 @@ export function useFeatureCollection( source = SOURCE_SELECTION_UTILISATEUR, external = false }) => { - if (!enabled) { - return; - } try { const newFeatures: Feature[] = []; for (const feature of features) { @@ -117,7 +114,7 @@ export function useFeatureCollection( onError('Le polygone dessiné n’est pas valide.'); } }, - [enabled, url, updateFeatureCollection, addFeatures, onError] + [url, updateFeatureCollection, addFeatures, onError] ); const updateFeatures = useCallback( @@ -126,9 +123,6 @@ export function useFeatureCollection( source = SOURCE_SELECTION_UTILISATEUR, external = false }) => { - if (!enabled) { - return; - } try { const newFeatures: Feature[] = []; for (const feature of features) { @@ -154,14 +148,11 @@ export function useFeatureCollection( onError('Le polygone dessiné n’est pas valide.'); } }, - [enabled, url, updateFeatureCollection, addFeatures, onError] + [url, updateFeatureCollection, addFeatures, onError] ); const deleteFeatures = useCallback( async ({ features, external = false }) => { - if (!enabled) { - return; - } try { const deletedFeatures = []; for (const feature of features) { @@ -183,7 +174,7 @@ export function useFeatureCollection( onError('Le polygone n’a pas pu être supprimé.'); } }, - [enabled, url, updateFeatureCollection, removeFeatures, onError] + [url, updateFeatureCollection, removeFeatures, onError] ); return { diff --git a/app/javascript/components/MapEditor/index.tsx b/app/javascript/components/MapEditor/index.tsx index a5d81741e..d5acd5710 100644 --- a/app/javascript/components/MapEditor/index.tsx +++ b/app/javascript/components/MapEditor/index.tsx @@ -16,19 +16,17 @@ import { FlashMessage } from '../shared/FlashMessage'; export default function MapEditor({ featureCollection: initialFeatureCollection, url, - options, - preview + options }: { featureCollection: FeatureCollection; url: string; - preview: boolean; options: { layers: string[] }; }) { const [cadastreEnabled, setCadastreEnabled] = useState(false); const { featureCollection, error, ...actions } = useFeatureCollection( initialFeatureCollection, - { url, enabled: !preview } + { url } ); return ( @@ -54,14 +52,14 @@ export default function MapEditor({ {options.layers.includes('cadastres') ? ( <>