From deb51e38801f233589839f230a3b8e7eb07dc0d4 Mon Sep 17 00:00:00 2001 From: Damien Le Thiec Date: Mon, 20 Feb 2023 15:35:14 +0100 Subject: [PATCH] Manage GET prefill by creating a dossier --- app/controllers/users/commencer_controller.rb | 50 +++++++++++-------- app/models/prefill_description.rb | 2 +- config/routes.rb | 1 - .../users/commencer_controller_spec.rb | 10 ---- .../users/dossiers_controller_spec.rb | 4 -- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/app/controllers/users/commencer_controller.rb b/app/controllers/users/commencer_controller.rb index 14021f009..dbd4d7cbf 100644 --- a/app/controllers/users/commencer_controller.rb +++ b/app/controllers/users/commencer_controller.rb @@ -2,15 +2,22 @@ module Users class CommencerController < ApplicationController layout 'procedure_context' - before_action :retrieve_prefilled_dossier, if: -> { params[:prefill_token].present? }, only: :commencer - before_action :set_prefilled_dossier_ownership, if: -> { user_signed_in? && @prefilled_dossier&.orphan? }, only: :commencer - before_action :check_prefilled_dossier_ownership, if: -> { user_signed_in? && @prefilled_dossier }, only: :commencer - def commencer @procedure = retrieve_procedure return procedure_not_found if @procedure.blank? || @procedure.brouillon? - @revision = @procedure.published_revision + + if prefill_params_present? + build_prefilled_dossier + elsif params[:prefill_token].present? + retrieve_prefilled_dossier + end + + if user_signed_in? + set_prefilled_dossier_ownership if @prefilled_dossier&.orphan? + check_prefilled_dossier_ownership if @prefilled_dossier + end + render 'commencer/show' end @@ -22,22 +29,6 @@ module Users render 'commencer/show' end - def preremplir - @procedure = retrieve_procedure - dossier = Dossier.new( - revision: @procedure.active_revision, - groupe_instructeur: @procedure.defaut_groupe_instructeur_for_new_dossier, - state: Dossier.states.fetch(:brouillon), - prefilled: true - ) - dossier.build_default_individual - if dossier.save - dossier.prefill!(PrefillParams.new(dossier, params.to_unsafe_h).to_a) - end - - redirect_to commencer_url(@procedure.path, prefill_token: dossier.prefill_token) - end - def dossier_vide_pdf @procedure = retrieve_procedure_with_closed return procedure_not_found if @procedure.blank? || @procedure.brouillon? @@ -82,6 +73,10 @@ module Users private + def prefill_params_present? + params.keys.find { |param| param.split('_').first == "champ" } + end + def retrieve_procedure Procedure.publiees.or(Procedure.brouillons).find_by(path: params[:path]) end @@ -90,6 +85,19 @@ module Users Procedure.publiees.or(Procedure.brouillons).or(Procedure.closes).order(published_at: :desc).find_by(path: params[:path]) end + def build_prefilled_dossier + @prefilled_dossier = Dossier.new( + revision: @revision, + groupe_instructeur: @procedure.defaut_groupe_instructeur_for_new_dossier, + state: Dossier.states.fetch(:brouillon), + prefilled: true + ) + @prefilled_dossier.build_default_individual + if @prefilled_dossier.save + @prefilled_dossier.prefill!(PrefillParams.new(@prefilled_dossier, params.to_unsafe_h).to_a) + end + end + def retrieve_prefilled_dossier @prefilled_dossier = Dossier.state_brouillon.prefilled.find_by!(prefill_token: params[:prefill_token]) end diff --git a/app/models/prefill_description.rb b/app/models/prefill_description.rb index 57c47e6c9..2148b3dce 100644 --- a/app/models/prefill_description.rb +++ b/app/models/prefill_description.rb @@ -27,7 +27,7 @@ class PrefillDescription < SimpleDelegator end def prefill_link - @prefill_link ||= commencer_preremplissage_url({ path: path }.merge(prefilled_champs_for_link)) + @prefill_link ||= commencer_url({ path: path }.merge(prefilled_champs_for_link)) end def prefill_query diff --git a/config/routes.rb b/config/routes.rb index b62f27119..e295e8179 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -295,7 +295,6 @@ Rails.application.routes.draw do get '/test/:path/dossier_vide', action: :dossier_vide_pdf_test, as: :dossier_vide_test get '/test/:path', action: 'commencer_test', as: :test get '/:path', action: 'commencer' - get '/preremplir/:path', action: 'preremplir', as: :preremplissage get '/:path/dossier_vide', action: 'dossier_vide_pdf', as: :dossier_vide get '/:path/sign_in', action: 'sign_in', as: :sign_in get '/:path/sign_up', action: 'sign_up', as: :sign_up diff --git a/spec/controllers/users/commencer_controller_spec.rb b/spec/controllers/users/commencer_controller_spec.rb index f4370c526..1f4035d9c 100644 --- a/spec/controllers/users/commencer_controller_spec.rb +++ b/spec/controllers/users/commencer_controller_spec.rb @@ -15,16 +15,6 @@ describe Users::CommencerController, type: :controller do expect(assigns(:procedure)).to eq published_procedure expect(assigns(:revision)).to eq published_procedure.published_revision end - - context 'when there are query params' do - subject { get :commencer, params: { path: path, any_param: "any param" } } - - it "stores the parameters in session" do - subject - - expect(session[:stored_params]).to be_present - end - end end context 'when the path is for a draft procedure' do diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 71c773be0..f834508c8 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -1082,10 +1082,6 @@ describe Users::DossiersController, type: :controller do } } - before { session[:stored_params] = params.to_json } - - it { expect { subject }.to change { session[:stored_params] }.to(nil) } - it { expect { subject }.to change { Dossier.count }.by(1) } it "prefills the dossier's champs with the given values" do