2017-06-29 14:18:12 +02:00
module NewUser
class DossiersController < UserController
2018-06-26 18:04:04 +02:00
helper_method :new_demarche_url
2018-03-29 15:25:05 +02:00
before_action :ensure_ownership! , except : [ :index , :modifier , :update ]
before_action :ensure_ownership_or_invitation! , only : [ :modifier , :update ]
2018-06-08 15:51:46 +02:00
before_action :ensure_dossier_can_be_updated , only : [ :update_identite , :update ]
2018-03-29 16:49:01 +02:00
before_action :forbid_invite_submission! , only : [ :update ]
2017-06-29 14:18:12 +02:00
2017-06-29 14:18:59 +02:00
def attestation
send_data ( dossier . attestation . pdf . read , filename : 'attestation.pdf' , type : 'application/pdf' )
end
2018-02-08 17:13:15 +01:00
def identite
@dossier = dossier
@user = current_user
end
2018-02-08 17:13:15 +01:00
def update_identite
@dossier = dossier
individual_updated = @dossier . individual . update ( individual_params )
dossier_updated = @dossier . update ( dossier_params )
if individual_updated && dossier_updated
flash . notice = " Identité enregistrée "
if @dossier . procedure . module_api_carto . use_api_carto
redirect_to users_dossier_carte_path ( @dossier . id )
else
2018-02-21 18:36:06 +01:00
redirect_to modifier_dossier_path ( @dossier )
2018-02-08 17:13:15 +01:00
end
else
flash . now . alert = @dossier . errors . full_messages
render :identite
end
end
2018-02-21 18:32:07 +01:00
def modifier
@dossier = dossier_with_champs
# TODO: remove when the champs are unifed
if ! @dossier . autorisation_donnees
if dossier . procedure . for_individual
redirect_to identite_dossier_path ( @dossier )
else
redirect_to users_dossier_path ( @dossier )
end
end
end
# FIXME: remove PiecesJustificativesService
# delegate draft save logic to champ ?
def update
@dossier = dossier_with_champs
errors = PiecesJustificativesService . upload! ( @dossier , current_user , params )
2018-02-27 17:25:45 +01:00
if champs_params [ :dossier ] && ! @dossier . update ( champs_params [ :dossier ] )
2018-02-21 18:32:07 +01:00
errors += @dossier . errors . full_messages
end
if ! draft?
errors += @dossier . champs . select ( & :mandatory_and_blank? )
. map { | c | " Le champ #{ c . libelle . truncate ( 200 ) } doit être rempli. " }
errors += PiecesJustificativesService . missing_pj_error_messages ( @dossier )
end
if errors . present?
flash . now . alert = errors
render :modifier
elsif draft?
flash . now . notice = 'Votre brouillon a bien été sauvegardé.'
render :modifier
2018-03-29 16:45:24 +02:00
elsif @dossier . brouillon?
@dossier . en_construction!
2018-05-25 18:05:28 +02:00
NotificationMailer . send_initiated_notification ( @dossier ) . deliver_later
2018-03-29 16:45:24 +02:00
redirect_to merci_dossier_path ( @dossier )
2018-05-30 18:26:23 +02:00
elsif current_user . owns? ( dossier )
2018-03-29 16:45:24 +02:00
redirect_to users_dossier_recapitulatif_path ( @dossier )
2018-02-21 18:32:07 +01:00
else
2018-03-29 16:45:24 +02:00
redirect_to users_dossiers_invite_path ( @dossier . invite_for_user ( current_user ) )
2018-02-21 18:32:07 +01:00
end
end
2018-02-27 09:49:58 +01:00
def merci
@dossier = current_user . dossiers . includes ( :procedure ) . find ( params [ :id ] )
end
2018-03-28 15:02:37 +02:00
def index
2018-03-30 18:17:47 +02:00
@user_dossiers = current_user . dossiers . includes ( :procedure ) . page ( page )
@dossiers_invites = current_user . dossiers_invites . includes ( :procedure ) . page ( page )
2018-04-03 10:49:16 +02:00
@current_tab = current_tab ( @user_dossiers . count , @dossiers_invites . count )
@dossiers = case @current_tab
when 'mes-dossiers'
@user_dossiers
when 'dossiers-invites'
@dossiers_invites
end
2018-03-28 15:02:37 +02:00
end
2018-05-24 15:55:47 +02:00
def ask_deletion
2018-05-30 11:36:48 +02:00
dossier = current_user . dossiers . includes ( :user , procedure : :administrateur ) . find ( params [ :id ] )
2018-06-13 13:58:14 +02:00
if ! dossier . instruction_commencee?
2018-06-13 13:59:02 +02:00
dossier . delete_and_keep_track
2018-06-13 13:58:14 +02:00
flash . notice = 'Votre dossier a bien été supprimé.'
redirect_to users_dossiers_path
else
flash . notice = " L'instruction de votre dossier a commencé, il n'est plus possible de supprimer votre dossier. Si vous souhaitez annuler l'instruction contactez votre administration par la messagerie de votre dossier. "
redirect_to users_dossier_path ( dossier )
end
2018-05-24 15:55:47 +02:00
end
2018-06-26 18:04:04 +02:00
def new_demarche_url
" https://doc.demarches-simplifiees.fr/listes-des-demarches "
end
2017-06-29 14:18:12 +02:00
private
2018-06-08 15:51:46 +02:00
def ensure_dossier_can_be_updated
if ! dossier . can_be_updated_by_the_user?
flash . alert = 'Votre dossier ne peut plus être modifié'
redirect_to users_dossiers_path
end
end
2018-03-30 18:17:47 +02:00
def page
[ params [ :page ] . to_i , 1 ] . max
end
2018-04-03 10:49:16 +02:00
def current_tab ( mes_dossiers_count , dossiers_invites_count )
if dossiers_invites_count == 0
'mes-dossiers'
elsif mes_dossiers_count == 0
'dossiers-invites'
else
params [ :current_tab ] . presence || 'mes-dossiers'
end
end
2018-02-27 17:25:45 +01:00
# FIXME: require(:dossier) when all the champs are united
2018-02-21 18:32:07 +01:00
def champs_params
2018-04-03 17:53:14 +02:00
params . permit ( dossier : {
champs_attributes : [
2018-06-27 14:31:34 +02:00
:id , :value , :primary_value , :secondary_value , :piece_justificative_file , value : [ ] ,
2018-04-03 17:53:14 +02:00
etablissement_attributes : Champs :: SiretChamp :: ETABLISSEMENT_ATTRIBUTES
]
} )
2018-02-21 18:32:07 +01:00
end
2017-06-29 14:18:12 +02:00
def dossier
2018-02-08 17:13:15 +01:00
Dossier . find ( params [ :id ] || params [ :dossier_id ] )
2017-06-29 14:18:12 +02:00
end
2018-02-21 18:32:07 +01:00
def dossier_with_champs
2018-05-30 18:54:12 +02:00
Dossier . with_ordered_champs . find ( params [ :id ] )
2018-02-21 18:32:07 +01: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!
2018-05-30 18:26:23 +02:00
if passage_en_construction? && ! current_user . owns? ( dossier )
2018-03-29 16:49:01 +02:00
forbidden!
end
end
2018-03-29 15:25:05 +02:00
def forbidden!
flash [ :alert ] = " Vous n'avez pas accès à ce dossier "
redirect_to root_path
end
2018-02-08 17:13:15 +01:00
def individual_params
params . require ( :individual ) . permit ( :gender , :nom , :prenom , :birthdate )
end
def dossier_params
params . require ( :dossier ) . permit ( :autorisation_donnees )
end
2018-02-21 18:32:07 +01:00
2018-03-29 16:49:01 +02:00
def passage_en_construction?
dossier . brouillon? && ! draft?
end
2018-02-21 18:32:07 +01:00
def draft?
params [ :submit_action ] == 'draft'
end
2017-06-29 14:18:12 +02:00
end
end