2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-25 10:53:45 +01:00
|
|
|
module Users
|
2018-11-01 13:00:35 +01:00
|
|
|
class CommencerController < ApplicationController
|
2019-01-16 11:57:58 +01:00
|
|
|
layout 'procedure_context'
|
|
|
|
|
2024-08-29 16:16:07 +02:00
|
|
|
before_action :path_rewrite, only: [:commencer, :commencer_test, :dossier_vide_pdf, :dossier_vide_pdf_test, :sign_in, :sign_up, :france_connect, :procedure_for_help, :closing_details]
|
|
|
|
|
|
|
|
# TODO: REMOVE THIS
|
|
|
|
# this was only added because a administration needed new urls
|
|
|
|
# check from 07/2025 if this is still needed
|
|
|
|
def path_rewrite
|
|
|
|
path_rewrite = PathRewrite.find_by(from: params[:path])
|
|
|
|
|
|
|
|
params[:path] = path_rewrite.to if path_rewrite.present?
|
|
|
|
end
|
|
|
|
|
2019-01-16 11:57:58 +01:00
|
|
|
def commencer
|
2019-07-05 10:37:29 +02:00
|
|
|
@procedure = retrieve_procedure
|
2023-06-12 12:05:48 +02:00
|
|
|
|
|
|
|
return procedure_not_found if @procedure.blank?
|
|
|
|
|
|
|
|
@revision = params[:test] ? @procedure.draft_revision : @procedure.active_revision
|
2023-02-20 15:35:14 +01:00
|
|
|
|
2023-02-27 10:48:59 +01:00
|
|
|
if params[:prefill_token].present? || commencer_page_is_reloaded?
|
|
|
|
retrieve_prefilled_dossier(params[:prefill_token] || session[:prefill_token])
|
|
|
|
elsif prefill_params_present?
|
2023-02-20 15:35:14 +01:00
|
|
|
build_prefilled_dossier
|
|
|
|
end
|
|
|
|
|
|
|
|
if user_signed_in?
|
|
|
|
set_prefilled_dossier_ownership if @prefilled_dossier&.orphan?
|
|
|
|
check_prefilled_dossier_ownership if @prefilled_dossier
|
2023-10-09 22:27:45 +02:00
|
|
|
|
|
|
|
revision = @revision.draft? ? @revision : @procedure.revisions.where.not(id: @procedure.draft_revision_id)
|
2023-10-09 22:48:36 +02:00
|
|
|
@dossiers = current_user.dossiers.select(:id, :created_at, :depose_at, :state).visible_by_user.where(revision:).order(created_at: :desc).to_a
|
|
|
|
@drafts, @not_drafts = @dossiers.partition(&:brouillon?)
|
|
|
|
@preview_dossiers = @dossiers.take(3)
|
2023-02-20 15:35:14 +01:00
|
|
|
end
|
|
|
|
|
2019-01-16 11:57:58 +01:00
|
|
|
render 'commencer/show'
|
2018-11-01 13:00:35 +01:00
|
|
|
end
|
|
|
|
|
2019-01-16 11:57:58 +01:00
|
|
|
def commencer_test
|
2023-06-12 12:05:48 +02:00
|
|
|
redirect_to commencer_path(params[:path], **extra_query_params)
|
2019-01-16 11:57:58 +01:00
|
|
|
end
|
|
|
|
|
2020-02-27 17:39:11 +01:00
|
|
|
def dossier_vide_pdf
|
2022-08-05 15:45:13 +02:00
|
|
|
@procedure = retrieve_procedure_with_closed
|
2020-02-27 17:39:11 +01:00
|
|
|
return procedure_not_found if @procedure.blank? || @procedure.brouillon?
|
|
|
|
|
2021-06-24 11:57:05 +02:00
|
|
|
generate_empty_pdf(@procedure.published_revision)
|
2020-02-27 17:39:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def dossier_vide_pdf_test
|
2022-08-05 15:45:13 +02:00
|
|
|
@procedure = retrieve_procedure_with_closed
|
2022-12-01 13:29:08 +01:00
|
|
|
return procedure_not_found if @procedure.blank?
|
2020-02-27 17:39:11 +01:00
|
|
|
|
2021-06-24 11:57:05 +02:00
|
|
|
generate_empty_pdf(@procedure.draft_revision)
|
2020-02-27 17:39:11 +01:00
|
|
|
end
|
|
|
|
|
2019-01-16 11:57:58 +01:00
|
|
|
def sign_in
|
2019-07-05 10:37:29 +02:00
|
|
|
@procedure = retrieve_procedure
|
2019-04-04 14:03:03 +02:00
|
|
|
return procedure_not_found if @procedure.blank?
|
|
|
|
|
|
|
|
store_user_location!(@procedure)
|
2019-01-16 11:57:58 +01:00
|
|
|
redirect_to new_user_session_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign_up
|
2019-07-05 10:37:29 +02:00
|
|
|
@procedure = retrieve_procedure
|
2019-04-04 14:03:03 +02:00
|
|
|
return procedure_not_found if @procedure.blank?
|
|
|
|
|
|
|
|
store_user_location!(@procedure)
|
2019-01-16 11:57:58 +01:00
|
|
|
redirect_to new_user_registration_path
|
|
|
|
end
|
|
|
|
|
2019-04-02 15:03:35 +02:00
|
|
|
def france_connect
|
2019-07-05 10:37:29 +02:00
|
|
|
@procedure = retrieve_procedure
|
2019-04-04 14:03:03 +02:00
|
|
|
return procedure_not_found if @procedure.blank?
|
|
|
|
|
|
|
|
store_user_location!(@procedure)
|
2019-04-02 15:03:35 +02:00
|
|
|
redirect_to france_connect_particulier_path
|
|
|
|
end
|
|
|
|
|
2019-03-28 11:14:01 +01:00
|
|
|
def procedure_for_help
|
2019-07-05 10:37:29 +02:00
|
|
|
retrieve_procedure
|
2019-03-28 11:14:01 +01:00
|
|
|
end
|
|
|
|
|
2024-09-17 15:59:24 +02:00
|
|
|
def nav_bar_profile = nav_bar_user_or_guest
|
2023-10-30 18:35:31 +01:00
|
|
|
|
2024-02-15 11:21:31 +01:00
|
|
|
def closing_details
|
|
|
|
@procedure = Procedure.find_by(path: params[:path])
|
2024-03-19 14:45:55 +01:00
|
|
|
|
|
|
|
return procedure_not_found if @procedure.blank?
|
|
|
|
|
2024-03-27 12:16:04 +01:00
|
|
|
redirect_to commencer_path(params[:path]) and return if !@procedure.close?
|
|
|
|
|
2024-02-15 11:21:31 +01:00
|
|
|
render 'closing_details', layout: 'closing_details'
|
|
|
|
end
|
|
|
|
|
2019-01-16 11:57:58 +01:00
|
|
|
private
|
|
|
|
|
2023-06-12 12:05:48 +02:00
|
|
|
def extra_query_params
|
|
|
|
params.slice(:prefill_token, :test).to_unsafe_h.compact
|
|
|
|
end
|
|
|
|
|
2023-02-27 10:48:59 +01:00
|
|
|
def commencer_page_is_reloaded?
|
2023-09-07 10:03:17 +02:00
|
|
|
session[:prefill_token].present? && session[:prefill_params_digest] == PrefillChamps.digest(params)
|
2023-02-27 10:48:59 +01:00
|
|
|
end
|
|
|
|
|
2023-02-20 15:35:14 +01:00
|
|
|
def prefill_params_present?
|
2023-09-07 10:03:17 +02:00
|
|
|
params.keys.find { ['champ', 'identite'].include?(_1.split('_').first) }
|
2023-02-20 15:35:14 +01:00
|
|
|
end
|
|
|
|
|
2019-07-05 10:37:29 +02:00
|
|
|
def retrieve_procedure
|
|
|
|
Procedure.publiees.or(Procedure.brouillons).find_by(path: params[:path])
|
|
|
|
end
|
|
|
|
|
2022-08-05 15:45:13 +02:00
|
|
|
def retrieve_procedure_with_closed
|
2023-01-06 11:00:42 +01:00
|
|
|
Procedure.publiees.or(Procedure.brouillons).or(Procedure.closes).order(published_at: :desc).find_by(path: params[:path])
|
2022-08-05 15:45:13 +02:00
|
|
|
end
|
|
|
|
|
2023-02-20 15:35:14 +01:00
|
|
|
def build_prefilled_dossier
|
|
|
|
@prefilled_dossier = Dossier.new(
|
|
|
|
revision: @revision,
|
|
|
|
state: Dossier.states.fetch(:brouillon),
|
|
|
|
prefilled: true
|
|
|
|
)
|
2024-09-27 14:44:07 +02:00
|
|
|
@prefilled_dossier.build_default_values
|
2023-02-20 15:35:14 +01:00
|
|
|
if @prefilled_dossier.save
|
2023-09-07 10:03:17 +02:00
|
|
|
@prefilled_dossier.prefill!(PrefillChamps.new(@prefilled_dossier, params.to_unsafe_h).to_a, PrefillIdentity.new(@prefilled_dossier, params.to_unsafe_h).to_h)
|
2023-02-20 15:35:14 +01:00
|
|
|
end
|
2023-02-28 16:54:52 +01:00
|
|
|
session[:prefill_token] = @prefilled_dossier.prefill_token
|
2023-09-07 10:03:17 +02:00
|
|
|
session[:prefill_params_digest] = PrefillChamps.digest(params)
|
2023-02-20 15:35:14 +01:00
|
|
|
end
|
|
|
|
|
2023-02-27 10:48:59 +01:00
|
|
|
def retrieve_prefilled_dossier(prefill_token)
|
|
|
|
@prefilled_dossier = Dossier.state_brouillon.prefilled.find_by!(prefill_token: prefill_token)
|
2023-01-03 14:46:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# The prefilled dossier is not owned yet, and the user is signed in: they become the new owner
|
|
|
|
def set_prefilled_dossier_ownership
|
|
|
|
@prefilled_dossier.update!(user: current_user)
|
|
|
|
DossierMailer.with(dossier: @prefilled_dossier).notify_new_draft.deliver_later
|
|
|
|
end
|
|
|
|
|
|
|
|
# The prefilled dossier is owned by another user: raise an exception
|
|
|
|
def check_prefilled_dossier_ownership
|
|
|
|
raise ActiveRecord::RecordNotFound unless @prefilled_dossier.owned_by?(current_user)
|
|
|
|
end
|
|
|
|
|
2019-04-04 14:03:03 +02:00
|
|
|
def procedure_not_found
|
2019-01-16 11:57:58 +01:00
|
|
|
procedure = Procedure.find_by(path: params[:path])
|
2019-04-04 14:03:03 +02:00
|
|
|
|
2022-07-29 10:44:30 +02:00
|
|
|
if procedure&.replaced_by_procedure
|
2023-06-12 12:05:48 +02:00
|
|
|
redirect_to commencer_path(procedure.replaced_by_procedure.path, **extra_query_params)
|
2022-07-29 10:44:30 +02:00
|
|
|
return
|
|
|
|
elsif procedure&.close?
|
2024-02-15 11:21:31 +01:00
|
|
|
redirect_to closing_details_path(procedure.path)
|
|
|
|
return
|
2019-04-04 14:03:03 +02:00
|
|
|
else
|
|
|
|
flash.alert = t('errors.messages.procedure_not_found')
|
|
|
|
end
|
|
|
|
|
2019-07-05 10:37:29 +02:00
|
|
|
redirect_to root_path
|
2019-04-04 14:03:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def store_user_location!(procedure)
|
2023-06-12 12:05:48 +02:00
|
|
|
store_location_for(:user, commencer_path(procedure.path, **extra_query_params))
|
2018-11-01 13:00:35 +01:00
|
|
|
end
|
2020-03-17 14:51:27 +01:00
|
|
|
|
2021-06-24 11:57:05 +02:00
|
|
|
def generate_empty_pdf(revision)
|
2024-03-12 13:42:06 +01:00
|
|
|
@revision = revision
|
2022-04-28 13:57:19 +02:00
|
|
|
data = render_to_string(template: 'dossiers/dossier_vide', formats: [:pdf])
|
2022-04-28 13:59:23 +02:00
|
|
|
send_data(data, filename: "#{revision.procedure.libelle}.pdf")
|
2020-03-17 14:51:27 +01:00
|
|
|
end
|
2018-11-01 13:00:35 +01:00
|
|
|
end
|
|
|
|
end
|