Manage GET prefill by creating a dossier

This commit is contained in:
Damien Le Thiec 2023-02-20 15:35:14 +01:00
parent 802a23057b
commit deb51e3880
5 changed files with 30 additions and 37 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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