2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2019-03-25 10:53:45 +01:00
module Users
2017-06-29 14:18:12 +02:00
class DossiersController < UserController
2018-07-17 11:40:19 +02:00
include DossierHelper
2023-03-01 18:30:10 +01:00
include TurboChampsConcern
2024-01-24 10:04:31 +01:00
include LockableConcern
2018-07-17 11:40:19 +02:00
2019-01-07 12:17:11 +01:00
layout 'procedure_context' , only : [ :identite , :update_identite , :siret , :update_siret ]
2024-07-22 11:12:38 +02:00
ACTIONS_ALLOWED_TO_ANY_USER = [ :index , :new , :transferer_all , :deleted_dossiers ]
2023-05-30 11:39:53 +02:00
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [ :show , :destroy , :demande , :messagerie , :brouillon , :submit_brouillon , :submit_en_construction , :modifier , :modifier_legacy , :update , :create_commentaire , :papertrail , :restore , :champ ]
2018-09-20 17:39:15 +02:00
2022-05-07 17:49:27 +02:00
before_action :ensure_ownership! , except : ACTIONS_ALLOWED_TO_ANY_USER + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
2018-09-20 17:39:15 +02:00
before_action :ensure_ownership_or_invitation! , only : ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
2023-05-30 11:39:53 +02:00
before_action :ensure_dossier_can_be_updated , only : [ :update_identite , :update_siret , :brouillon , :submit_brouillon , :submit_en_construction , :modifier , :modifier_legacy , :update , :champ ]
2023-03-21 18:24:39 +01:00
before_action :ensure_dossier_can_be_filled , only : [ :brouillon , :modifier , :submit_brouillon , :submit_en_construction , :update ]
2023-04-26 16:54:50 +02:00
before_action :ensure_dossier_can_be_viewed , only : [ :show ]
2022-10-11 12:21:06 +02:00
before_action :forbid_invite_submission! , only : [ :submit_brouillon ]
before_action :forbid_closed_submission! , only : [ :submit_brouillon ]
2023-05-11 16:52:10 +02:00
before_action :set_dossier_as_editing_fork , only : [ :submit_en_construction ]
2018-10-25 18:05:47 +02:00
before_action :show_demarche_en_test_banner
2018-11-01 13:00:35 +01:00
before_action :store_user_location! , only : :new
2017-06-29 14:18:12 +02:00
2024-01-24 10:04:31 +01:00
around_action only : :submit_en_construction do | _controller , action |
lock_action ( " lock-submit-en-construction- #{ @dossier . id } " , & action )
end
2018-07-04 11:49:24 +02:00
def index
2023-07-24 16:48:05 +02:00
ordered_dossiers = Dossier . includes ( :procedure ) . order_by_updated_at
user_revisions = ProcedureRevision . where ( dossiers : current_user . dossiers . visible_by_user )
invite_revisions = ProcedureRevision . where ( dossiers : current_user . dossiers_invites . visible_by_user )
all_dossier_procedures = Procedure . where ( revisions : user_revisions . or ( invite_revisions ) )
@procedures_for_select = all_dossier_procedures
. distinct ( :procedure_id )
. order ( :libelle )
. pluck ( :libelle , :id )
2023-07-19 17:57:24 +02:00
2023-07-26 17:51:04 +02:00
@procedure_id = params [ :procedure_id ]
2023-07-19 17:57:24 +02:00
if @procedure_id . present?
2023-07-24 16:48:05 +02:00
ordered_dossiers = ordered_dossiers . where ( procedures : { id : @procedure_id } )
2023-07-19 17:57:24 +02:00
end
2022-03-09 10:27:43 +01:00
2023-07-26 17:51:04 +02:00
@search_terms = params [ :q ]
if @search_terms . present?
dossiers_filter_by_search = DossierSearchService . matching_dossiers_for_user ( @search_terms , current_user ) . page
ordered_dossiers = ordered_dossiers . merge ( dossiers_filter_by_search )
end
2023-11-28 17:43:44 +01:00
@dossiers_visibles = ordered_dossiers . visible_by_user . preload ( :etablissement , :individual , :invites )
2022-03-09 10:27:43 +01:00
2023-07-26 17:51:04 +02:00
@user_dossiers = current_user . dossiers . state_not_termine . merge ( @dossiers_visibles )
@dossiers_traites = current_user . dossiers . state_termine . merge ( @dossiers_visibles )
@dossiers_invites = current_user . dossiers_invites . merge ( @dossiers_visibles )
2024-07-18 14:15:29 +02:00
@dossiers_supprimes = ( current_user . dossiers . hidden_by_user . or ( current_user . dossiers . hidden_by_expired ) ) . merge ( ordered_dossiers )
2024-02-05 12:21:14 +01:00
@dossier_transferes = @dossiers_visibles . where ( dossier_transfer_id : DossierTransfer . for_email ( current_user . email ) )
2023-07-26 17:51:04 +02:00
@dossiers_close_to_expiration = current_user . dossiers . close_to_expiration . merge ( @dossiers_visibles )
2023-07-19 17:57:24 +02:00
2024-07-18 14:15:29 +02:00
@statut = statut ( @user_dossiers , @dossiers_traites , @dossiers_invites , @dossiers_supprimes , @dossier_transferes , @dossiers_close_to_expiration , params [ :statut ] )
2023-04-11 15:50:17 +02:00
2023-04-26 11:05:15 +02:00
@dossiers = case @statut
when 'en-cours'
@user_dossiers
when 'traites'
@dossiers_traites
when 'dossiers-invites'
@dossiers_invites
2024-07-18 14:15:29 +02:00
when 'dossiers-supprimes'
@dossiers_supprimes
2023-04-26 11:05:15 +02:00
when 'dossiers-transferes'
2023-06-28 11:53:41 +02:00
@dossier_transferes
2023-04-26 11:05:15 +02:00
when 'dossiers-expirant'
@dossiers_close_to_expiration
end . page ( page )
2023-04-18 12:03:10 +02:00
@first_brouillon_recently_updated = current_user . dossiers . visible_by_user . brouillons_recently_updated . first
2023-04-28 15:28:04 +02:00
@filter = DossiersFilter . new ( current_user , params )
2023-05-04 12:39:06 +02:00
@dossiers = @filter . filter_procedures ( @dossiers ) . page ( page )
2018-07-04 11:49:24 +02:00
end
2018-08-07 11:58:10 +02:00
def show
2024-03-08 17:14:21 +01:00
pj_service = PiecesJustificativesService . new ( user_profile : current_user , export_template : nil )
2019-12-15 22:10:35 +01:00
respond_to do | format |
format . pdf do
2023-10-03 18:29:55 +02:00
@dossier = dossier_with_champs ( pj_template : false )
2024-03-07 08:39:32 +01:00
@acls = pj_service . acl_for_dossier_export ( @dossier . procedure )
2021-02-17 19:02:31 +01:00
render ( template : 'dossiers/show' , formats : [ :pdf ] )
2019-12-15 22:10:35 +01:00
end
2023-10-03 18:29:55 +02:00
format . all do
@dossier = dossier
end
2019-12-15 22:10:35 +01:00
end
2018-08-07 11:58:10 +02:00
end
2018-08-29 16:57:01 +02:00
def demande
2023-10-03 18:07:20 +02:00
@dossier = dossier_with_champs ( pj_template : false )
2018-08-14 15:06:44 +02:00
end
2018-09-05 13:56:12 +02:00
def messagerie
@dossier = dossier
@commentaire = Commentaire . new
end
2017-06-29 14:18:59 +02:00
def attestation
2019-11-05 16:44:28 +01:00
if dossier . attestation & . pdf & . attached?
2023-05-04 10:31:38 +02:00
redirect_to dossier . attestation . pdf . url , allow_other_host : true
2019-11-05 16:44:28 +01:00
else
2021-04-17 18:49:00 +02:00
flash . notice = t ( '.no_longer_available' )
2019-11-05 16:44:28 +01:00
redirect_to dossier_path ( dossier )
2019-08-27 17:43:33 +02:00
end
2017-06-29 14:18:59 +02:00
end
2022-05-04 08:57:14 +02:00
def papertrail
raise ActionController :: BadRequest if dossier . brouillon?
@dossier = dossier
end
2024-03-25 14:08:46 +01:00
def set_accuse_lecture_agreement_at
@dossier = dossier
@dossier . update! ( accuse_lecture_agreement_at : Time . zone . now )
flash . notice = 'Accusé de lecture accepté'
redirect_back ( fallback_location : demande_dossier_path ( @dossier ) )
end
2018-02-08 17:13:15 +01:00
def identite
@dossier = dossier
@user = current_user
2023-05-25 15:19:55 +02:00
@no_description = true
2024-04-12 17:59:50 +02:00
respond_to do | format |
format . html
format . turbo_stream do
2024-05-15 14:36:52 +02:00
@dossier . for_tiers = params [ :dossier ] [ :for_tiers ]
2024-04-12 17:59:50 +02:00
end
end
2018-02-08 17:13:15 +01:00
end
2018-02-08 17:13:15 +01:00
def update_identite
@dossier = dossier
2023-05-25 15:19:55 +02:00
@no_description = true
2024-07-11 17:50:02 +02:00
email = dossier_params . dig ( 'individual_attributes' , 'email' )
2018-02-08 17:13:15 +01:00
2023-11-23 16:38:01 +01:00
if @dossier . update ( dossier_params ) && @dossier . individual . valid?
2024-07-10 10:57:26 +02:00
# verify for_tiers email
if email . present?
User . create_or_promote_to_tiers ( email , SecureRandom . hex , @dossier )
end
2021-10-04 10:04:07 +02:00
@dossier . update! ( autorisation_donnees : true , identity_updated_at : Time . zone . now )
2021-04-17 18:49:00 +02:00
flash . notice = t ( '.identity_saved' )
2018-02-08 17:13:15 +01:00
2023-07-07 14:16:44 +02:00
if dossier . en_construction?
redirect_to demande_dossier_path ( @dossier )
else
redirect_to brouillon_dossier_path ( @dossier )
end
2018-02-08 17:13:15 +01:00
else
2023-11-23 16:38:01 +01:00
flash . now . alert = @dossier . individual . errors . full_messages + @dossier . errors . full_messages
2018-02-08 17:13:15 +01:00
render :identite
end
end
2018-10-04 09:43:19 +02:00
def siret
@dossier = dossier
2023-05-25 15:19:55 +02:00
@no_description = true
2018-10-04 09:43:19 +02:00
end
2018-10-15 18:41:53 +02:00
def update_siret
@dossier = dossier
2023-05-25 15:19:55 +02:00
@no_description = true
2018-10-15 18:41:53 +02:00
# We use the user as the holder model object for the siret value
# (so that we can restore it on the form in case of error).
#
# This is the only remaining use of User#siret: it could be refactored away.
# However some existing users have a siret but no associated etablissement,
# so we would need to analyze the legacy data and decide what to do with it.
current_user . siret = siret_params [ :siret ]
siret_model = Siret . new ( siret : siret_params [ :siret ] )
if ! siret_model . valid?
return render_siret_error ( siret_model . errors . full_messages )
end
sanitized_siret = siret_model . siret
2022-09-16 00:32:17 +02:00
etablissement = begin
APIEntrepriseService . create_etablissement ( @dossier , sanitized_siret , current_user . id )
rescue = > error
2024-10-09 12:47:26 +02:00
if error . is_a? ( APIEntreprise :: API :: Error :: ServiceUnavailable ) || ( error . try ( :network_error? ) && ! APIEntrepriseService . api_insee_up? )
2022-09-16 00:32:17 +02:00
# TODO: notify ops
APIEntrepriseService . create_etablissement_as_degraded_mode ( @dossier , sanitized_siret , current_user . id )
else
Sentry . capture_exception ( error , extra : { dossier_id : @dossier . id , siret : } )
# probably random error, invite user to retry
return render_siret_error ( t ( 'errors.messages.siret_network_error' ) )
end
end
2020-05-18 10:30:08 +02:00
if etablissement . nil?
2018-10-15 18:41:53 +02:00
return render_siret_error ( t ( 'errors.messages.siret_unknown' ) )
end
current_user . update! ( siret : sanitized_siret )
@dossier . update! ( autorisation_donnees : true )
redirect_to etablissement_dossier_path
end
2018-10-15 12:25:25 +02:00
def etablissement
@dossier = dossier
# Redirect if the user attempts to access the page URL directly
if ! @dossier . etablissement
2021-04-17 18:49:00 +02:00
flash . alert = t ( '.no_establishment' )
2018-10-15 12:25:25 +02:00
return redirect_to siret_dossier_path ( @dossier )
end
end
2018-09-06 09:09:23 +02:00
def brouillon
2023-02-27 10:48:59 +01:00
session . delete ( :prefill_token )
session . delete ( :prefill_params )
2018-02-21 18:32:07 +01:00
@dossier = dossier_with_champs
2023-10-26 16:02:23 +02:00
@dossier . validate ( :champs_public_value )
2018-02-21 18:32:07 +01:00
end
2022-10-11 12:21:06 +02:00
def submit_brouillon
2022-10-13 15:54:38 +02:00
@dossier = dossier_with_champs ( pj_template : false )
2024-06-05 17:40:35 +02:00
submit_dossier_and_compute_errors
2018-02-21 18:32:07 +01:00
2024-06-05 17:40:35 +02:00
if @dossier . errors . blank? && @dossier . can_passer_en_construction?
2020-07-01 17:31:08 +02:00
@dossier . passer_en_construction!
2022-12-02 18:15:43 +01:00
@dossier . process_declarative!
2023-06-06 10:50:46 +02:00
@dossier . process_sva_svr!
2020-04-23 11:52:21 +02:00
@dossier . groupe_instructeur . instructeurs . with_instant_email_dossier_notifications . each do | instructeur |
2020-04-09 09:22:37 +02:00
DossierMailer . notify_new_dossier_depose_to_instructeur ( @dossier , instructeur . email ) . deliver_later
end
2022-10-11 12:21:06 +02:00
redirect_to merci_dossier_path ( @dossier )
else
respond_to do | format |
format . html { render :brouillon }
format . turbo_stream
2022-10-11 10:57:14 +02:00
end
2018-02-21 18:32:07 +01:00
end
end
2020-03-19 14:47:09 +01:00
def extend_conservation
2022-04-08 13:16:08 +02:00
dossier . extend_conservation ( dossier . procedure . duree_conservation_dossiers_dans_ds . months )
2021-12-03 16:09:51 +01:00
flash [ :notice ] = t ( 'views.users.dossiers.archived_dossier' , duree_conservation_dossiers_dans_ds : dossier . procedure . duree_conservation_dossiers_dans_ds )
redirect_back ( fallback_location : dossier_path ( @dossier ) )
2020-03-19 14:47:09 +01:00
end
2024-07-23 15:06:23 +02:00
def extend_conservation_and_restore
2024-07-25 14:40:22 +02:00
dossier . extend_conservation_and_restore ( dossier . procedure . duree_conservation_dossiers_dans_ds . months , current_user )
flash [ :notice ] = t ( 'views.users.dossiers.archived_dossier' , duree_conservation_dossiers_dans_ds : dossier . procedure . duree_conservation_dossiers_dans_ds )
redirect_back ( fallback_location : dossier_path ( @dossier ) )
2024-07-23 15:06:23 +02:00
end
2018-09-05 18:23:10 +02:00
def modifier
@dossier = dossier_with_champs
end
2023-04-21 18:34:42 +02:00
# Transition to en_construction forks,
# so users editing en_construction dossiers won't completely break their changes.
# TODO: remove me after fork en_construction feature deploy (PR #8790)
def modifier_legacy
respond_to do | format |
format . turbo_stream do
flash . alert = " Une mise à jour de cette page est nécessaire pour poursuivre, veuillez la recharger (touche F5). Attention: le dernier champ modifié n’ a pas été sauvegardé, vous devrez le ressaisir. "
end
end
end
2023-03-21 18:24:39 +01:00
def submit_en_construction
@dossier = dossier_with_champs ( pj_template : false )
2023-11-22 12:06:21 +01:00
editing_fork_origin = @dossier . editing_fork_origin
if cast_bool ( params . dig ( :dossier , :pending_correction ) )
editing_fork_origin . resolve_pending_correction
end
2024-06-05 17:40:35 +02:00
submit_dossier_and_compute_errors
2022-10-11 12:21:06 +02:00
2024-06-05 17:40:35 +02:00
if @dossier . errors . blank? && @dossier . can_passer_en_construction?
2023-03-21 18:24:39 +01:00
editing_fork_origin . merge_fork ( @dossier )
2023-11-22 12:06:21 +01:00
editing_fork_origin . submit_en_construction!
2023-04-11 18:31:16 +02:00
2023-03-21 18:24:39 +01:00
redirect_to dossier_path ( editing_fork_origin )
else
respond_to do | format |
2023-05-09 18:04:34 +02:00
format . html do
render :modifier
end
2023-03-21 18:24:39 +01:00
format . turbo_stream do
2024-03-27 11:39:15 +01:00
@to_show , @to_hide , @to_update = champs_to_turbo_update ( champs_public_attributes_params , dossier . champs . filter ( & :public? ) )
2023-03-21 18:24:39 +01:00
render :update , layout : false
end
2022-10-11 12:21:06 +02:00
end
end
end
2018-09-06 11:39:46 +02:00
def update
2023-03-21 18:24:39 +01:00
@dossier = dossier . en_construction? ? dossier . find_editing_fork ( dossier . user ) : dossier
2022-10-13 15:54:38 +02:00
@dossier = dossier_with_champs ( pj_template : false )
2024-06-17 16:41:51 +02:00
@can_passer_en_construction_was , @can_passer_en_construction_is = @dossier . track_can_passer_en_construction do
update_dossier_and_compute_errors
end
2022-09-08 11:26:18 +02:00
respond_to do | format |
2022-10-11 10:57:14 +02:00
format . turbo_stream do
2024-03-27 11:39:15 +01:00
@to_show , @to_hide , @to_update = champs_to_turbo_update ( champs_public_attributes_params , dossier . champs . filter ( & :public? ) )
2023-03-21 18:24:39 +01:00
render :update , layout : false
2022-10-11 10:57:14 +02:00
end
2018-09-06 11:39:46 +02:00
end
end
2018-02-27 09:49:58 +01:00
def merci
@dossier = current_user . dossiers . includes ( :procedure ) . find ( params [ :id ] )
end
2023-05-30 11:39:53 +02:00
def champ
@dossier = dossier_with_champs ( pj_template : false )
2024-03-28 12:42:29 +01:00
type_de_champ = @dossier . find_type_de_champ_by_stable_id ( params [ :stable_id ] , :public )
champ = @dossier . project_champ ( type_de_champ , params [ :row_id ] )
2023-05-30 11:39:53 +02:00
respond_to do | format |
format . turbo_stream do
@to_show , @to_hide = [ ]
@to_update = [ champ ]
render :update , layout : false
end
end
end
2018-09-05 13:56:12 +02:00
def create_commentaire
2021-11-30 18:56:12 +01:00
@commentaire = CommentaireService . create ( current_user , dossier , commentaire_params )
2018-09-05 13:56:12 +02:00
2021-11-30 18:56:12 +01:00
if @commentaire . errors . empty?
2024-09-10 16:09:43 +02:00
timestamps = [ :last_commentaire_updated_at , :updated_at ]
timestamps << :last_commentaire_piece_jointe_updated_at if @commentaire . piece_jointe . attached?
@commentaire . dossier . touch ( * timestamps )
2024-01-03 13:23:48 +01:00
2021-04-17 18:49:00 +02:00
flash . notice = t ( '.message_send' )
2018-09-05 13:56:12 +02:00
redirect_to messagerie_dossier_path ( dossier )
else
flash . now . alert = @commentaire . errors . full_messages
render :messagerie
end
end
2023-04-03 20:00:52 +02:00
def destroy
2019-02-06 19:11:55 +01:00
if dossier . can_be_deleted_by_user?
2023-04-03 20:00:52 +02:00
if current_user . owns? ( dossier )
dossier . hide_and_keep_track! ( current_user , :user_request )
elsif current_user . invite? ( dossier )
current_user . invites . where ( dossier : ) . destroy_all
end
2021-12-07 15:38:37 +01:00
flash . notice = t ( 'users.dossiers.ask_deletion.soft_deleted_dossier' )
2018-06-27 14:47:02 +02:00
redirect_to dossiers_path
2018-06-13 13:58:14 +02:00
else
2021-12-07 15:38:37 +01:00
flash . alert = t ( 'users.dossiers.ask_deletion.undergoingreview' )
redirect_to dossiers_path
2018-06-13 13:58:14 +02:00
end
2018-05-24 15:55:47 +02:00
end
2018-11-01 13:00:35 +01:00
def new
erase_user_location!
2020-01-06 12:53:20 +01:00
begin
2021-06-24 11:57:05 +02:00
procedure = if params [ :brouillon ]
Procedure . publiees . or ( Procedure . brouillons ) . find ( params [ :procedure_id ] )
2020-01-06 12:53:20 +01:00
else
2021-06-24 11:57:05 +02:00
Procedure . publiees . find ( params [ :procedure_id ] )
2020-01-06 12:53:20 +01:00
end
rescue ActiveRecord :: RecordNotFound
flash . alert = t ( 'errors.messages.procedure_not_found' )
2021-06-24 11:57:05 +02:00
return redirect_to dossiers_path
2018-11-01 13:00:35 +01:00
end
2020-01-07 17:59:11 +01:00
dossier = Dossier . new (
2021-06-24 11:57:05 +02:00
revision : params [ :brouillon ] ? procedure . draft_revision : procedure . active_revision ,
2020-01-06 12:55:22 +01:00
user : current_user ,
state : Dossier . states . fetch ( :brouillon )
)
2024-09-27 14:44:07 +02:00
dossier . build_default_values
2020-01-07 17:59:11 +01:00
dossier . save!
2022-12-21 17:18:19 +01:00
DossierMailer . with ( dossier : ) . notify_new_draft . deliver_later
2018-11-01 13:00:35 +01:00
if dossier . procedure . for_individual
redirect_to identite_dossier_path ( dossier )
else
redirect_to siret_dossier_path ( id : dossier . id )
end
end
2019-03-13 16:48:38 +01:00
def dossier_for_help
dossier_id = params [ :id ] || params [ :dossier_id ]
2022-03-09 10:27:43 +01:00
@dossier || ( dossier_id . present? && Dossier . visible_by_user . find_by ( id : dossier_id . to_i ) )
2019-03-13 16:48:38 +01:00
end
2021-09-07 10:37:42 +02:00
def transferer
@transfer = DossierTransfer . new ( dossiers : [ dossier ] )
end
2022-01-05 10:40:31 +01:00
def restore
2022-05-07 17:49:27 +02:00
dossier . restore ( current_user )
2022-01-05 10:40:31 +01:00
flash . notice = t ( 'users.dossiers.restore' )
redirect_to dossiers_path
end
2022-11-08 16:26:03 +01:00
def clone
cloned_dossier = @dossier . clone
2022-12-21 17:18:19 +01:00
DossierMailer . with ( dossier : cloned_dossier ) . notify_new_draft . deliver_later
2022-11-08 16:26:03 +01:00
flash . notice = t ( 'users.dossiers.cloned_success' )
redirect_to brouillon_dossier_path ( cloned_dossier )
2022-12-05 18:14:31 +01:00
rescue ActiveRecord :: RecordInvalid = > e
flash . alert = e . record . errors . full_messages
2022-11-08 16:26:03 +01:00
redirect_to dossier_path ( @dossier )
end
2024-07-22 11:12:38 +02:00
def deleted_dossiers
@deleted_dossiers = current_user . deleted_dossiers . includes ( :procedure ) . order_by_updated_at . page ( page )
end
2017-06-29 14:18:12 +02:00
private
2020-12-07 15:10:26 +01:00
# if the status tab is filled, then this tab
# else first filled tab
2021-11-10 17:36:24 +01:00
# else en-cours
2024-07-18 14:15:29 +02:00
def statut ( mes_dossiers , dossiers_traites , dossiers_invites , dossiers_supprimes , dossier_transferes , dossiers_close_to_expiration , params_statut )
2020-12-07 15:10:26 +01:00
tabs = {
2023-11-30 15:34:34 +01:00
'en-cours' = > mes_dossiers ,
'traites' = > dossiers_traites ,
'dossiers-invites' = > dossiers_invites ,
2024-07-18 14:15:29 +02:00
'dossiers-supprimes' = > dossiers_supprimes ,
2023-11-30 15:34:34 +01:00
'dossiers-transferes' = > dossier_transferes ,
'dossiers-expirant' = > dossiers_close_to_expiration
2020-12-07 15:10:26 +01:00
}
2023-11-30 15:34:34 +01:00
if tabs [ params_statut ] & . present?
2020-12-07 15:10:26 +01:00
params_statut
2020-11-26 15:13:32 +01:00
else
2023-11-30 15:34:34 +01:00
tab = tabs . find { | _tab , scope | scope . present? }
tab & . first || 'en-cours'
2020-11-26 15:13:32 +01:00
end
2020-11-17 13:25:35 +01:00
end
2018-11-01 13:00:35 +01:00
def store_user_location!
store_location_for ( :user , request . fullpath )
end
def erase_user_location!
2019-01-14 16:16:23 +01:00
clear_stored_location_for ( :user )
2018-11-01 13:00:35 +01:00
end
2018-10-25 18:05:47 +02:00
def show_demarche_en_test_banner
2021-06-24 11:57:05 +02:00
if @dossier . present? && @dossier . revision . draft?
2021-09-15 19:53:36 +02:00
flash . now . alert = t ( 'users.dossiers.test_procedure' )
2018-10-25 18:05:47 +02:00
end
end
2018-06-08 15:51:46 +02:00
def ensure_dossier_can_be_updated
2019-02-06 18:20:35 +01:00
if ! dossier . can_be_updated_by_user?
2021-09-15 19:53:36 +02:00
flash . alert = t ( 'users.dossiers.no_longer_editable' )
2023-04-26 16:54:50 +02:00
redirect_to dossier_path ( dossier )
end
end
def ensure_dossier_can_be_filled
if ! dossier . autorisation_donnees
if dossier . procedure . for_individual
2023-07-07 14:16:44 +02:00
flash . alert = t ( 'users.dossiers.fill_identity.individual' )
2023-04-26 16:54:50 +02:00
redirect_to identite_dossier_path ( dossier )
else
2023-07-07 14:16:44 +02:00
flash . alert = t ( 'users.dossiers.fill_identity.siret' )
2023-04-26 16:54:50 +02:00
redirect_to siret_dossier_path ( dossier )
end
end
end
def ensure_dossier_can_be_viewed
if dossier . brouillon?
redirect_to brouillon_dossier_path ( dossier )
2018-06-08 15:51:46 +02:00
end
end
2018-03-30 18:17:47 +02:00
def page
[ params [ :page ] . to_i , 1 ] . max
end
2022-11-29 11:30:06 +01:00
def champs_public_params
2024-03-27 11:39:15 +01:00
champ_attributes = [
2023-05-30 14:42:36 +02:00
:id ,
:value ,
:value_other ,
:external_id ,
2024-05-06 21:44:41 +02:00
:code ,
2023-05-30 14:42:36 +02:00
:primary_value ,
:secondary_value ,
:numero_allocataire ,
:code_postal ,
:identifiant ,
:numero_fiscal ,
:reference_avis ,
:ine ,
:piece_justificative_file ,
:code_departement ,
:accreditation_number ,
:accreditation_birthdate ,
2024-02-13 13:40:33 +01:00
:feature ,
2023-05-30 14:42:36 +02:00
value : [ ]
2024-03-27 11:39:15 +01:00
]
# Strong attributes do not support records (indexed hash); they only support hashes with
# static keys. We create a static hash based on the available keys.
public_ids = params . dig ( :dossier , :champs_public_attributes ) & . keys || [ ]
champs_public_attributes = public_ids . map { [ _1 , champ_attributes ] } . to_h
params . require ( :dossier ) . permit ( champs_public_attributes : )
end
def champs_public_attributes_params
champs_public_params . fetch ( :champs_public_attributes )
2018-02-21 18:32:07 +01:00
end
2022-05-07 17:49:27 +02:00
def dossier_scope
2023-05-30 11:39:53 +02:00
if action_name == 'update' || action_name == 'champ'
2023-03-21 18:24:39 +01:00
Dossier . visible_by_user . or ( Dossier . for_procedure_preview ) . or ( Dossier . for_editing_fork )
2022-05-07 17:49:27 +02:00
elsif action_name == 'restore'
Dossier . hidden_by_user
2024-07-30 14:06:30 +02:00
elsif action_name == 'extend_conservation_and_restore' || ( action_name == 'show' && request . format . pdf? )
2024-06-12 16:32:42 +02:00
Dossier . visible_by_user . or ( Dossier . hidden_by_expired )
2022-05-07 17:49:27 +02:00
else
Dossier . visible_by_user
end
2022-03-09 10:27:43 +01:00
end
2022-05-07 17:49:27 +02:00
def dossier
2023-03-15 18:25:12 +01:00
@dossier || = dossier_scope . find ( params [ :id ] || params [ :dossier_id ] ) . tap do
set_sentry_dossier ( _1 )
end
2017-06-29 14:18:12 +02:00
end
2022-10-13 15:54:38 +02:00
def dossier_with_champs ( pj_template : true )
DossierPreloader . load_one ( dossier , pj_template : )
2018-02-21 18:32:07 +01:00
end
2023-05-11 16:52:10 +02:00
def set_dossier_as_editing_fork
@dossier = dossier . find_editing_fork ( dossier . user )
return if @dossier . present?
flash [ :alert ] = t ( 'users.dossiers.en_construction_submitted' )
redirect_to dossier_path ( dossier )
end
2018-09-06 13:35:59 +02:00
def update_dossier_and_compute_errors
2024-05-13 16:53:33 +02:00
@dossier . update_champs_attributes ( champs_public_attributes_params , :public , updated_by : current_user . email )
2024-07-23 20:24:03 +02:00
updated_champs = @dossier . champs . filter ( & :changed_for_autosave? )
if updated_champs . present?
2022-11-29 11:30:06 +01:00
@dossier . last_champ_updated_at = Time . zone . now
end
2023-09-19 11:07:50 +02:00
2023-10-26 16:02:23 +02:00
# We save the dossier without validating fields, and if it is successful and the client
# requests it, we ask for field validation errors.
2024-07-23 20:24:03 +02:00
if @dossier . save
if updated_champs . any? ( & :used_by_routing_rules? )
@update_contact_information = true
RoutingEngine . compute ( @dossier )
end
if params [ :validate ] . present?
@dossier . valid? ( :champs_public_value )
end
2022-11-29 11:30:06 +01:00
end
2022-10-11 12:21:06 +02:00
2023-10-26 16:02:23 +02:00
@dossier . errors
2022-10-11 12:21:06 +02:00
end
def submit_dossier_and_compute_errors
2023-10-26 16:02:23 +02:00
@dossier . validate ( :champs_public_value )
2024-06-05 17:40:35 +02:00
@dossier . check_mandatory_and_visible_champs
2023-10-26 16:02:23 +02:00
2023-11-22 12:06:21 +01:00
if @dossier . editing_fork_origin & . pending_correction?
@dossier . editing_fork_origin . validate ( :champs_public_value )
@dossier . editing_fork_origin . errors . where ( :pending_correction ) . each do | error |
2024-06-05 17:40:35 +02:00
@dossier . errors . import ( error )
2023-11-22 12:06:21 +01:00
end
end
2023-04-28 15:32:34 +02:00
end
2017-06-29 14:18:12 +02:00
def ensure_ownership!
2018-05-30 18:26:23 +02:00
if ! current_user . owns? ( dossier )
2018-03-29 15:25:05 +02:00
forbidden!
2017-06-29 14:18:12 +02:00
end
end
2018-02-08 17:13:15 +01:00
2018-03-29 15:25:05 +02:00
def ensure_ownership_or_invitation!
2018-05-30 18:31:02 +02:00
if ! current_user . owns_or_invite? ( dossier )
2018-03-29 15:25:05 +02:00
forbidden!
end
end
2018-03-29 16:49:01 +02:00
def forbid_invite_submission!
2022-10-11 12:21:06 +02:00
if ! current_user . owns? ( dossier )
2018-03-29 16:49:01 +02:00
forbidden!
end
end
2018-09-06 13:29:56 +02:00
def forbid_closed_submission!
2022-10-11 12:21:06 +02:00
if ! dossier . can_transition_to_en_construction?
2018-09-06 13:29:56 +02:00
forbidden!
end
end
2018-03-29 15:25:05 +02:00
def forbidden!
2023-07-19 11:50:38 +02:00
flash [ :alert ] = t ( 'users.dossiers.no_access_html' , email : current_user . email )
2018-03-29 15:25:05 +02:00
redirect_to root_path
end
2018-10-15 18:41:53 +02:00
def render_siret_error ( error_message )
flash . alert = error_message
render :siret
end
2023-11-20 10:59:57 +01:00
def dossier_params
2023-12-14 16:25:33 +01:00
params . require ( :dossier ) . permit ( :for_tiers , :mandataire_first_name , :mandataire_last_name , individual_attributes : [ :gender , :nom , :prenom , :birthdate , :email , :notification_method ] )
2018-02-08 17:13:15 +01:00
end
2018-10-15 18:41:53 +02:00
def siret_params
params . require ( :user ) . permit ( :siret )
end
2018-09-05 13:56:12 +02:00
def commentaire_params
2024-02-13 08:09:58 +01:00
params . require ( :commentaire ) . permit ( :body , piece_jointe : [ ] )
2018-09-05 13:56:12 +02:00
end
2017-06-29 14:18:12 +02:00
end
end