demarches-normaliennes/app/controllers/users/description_controller.rb

132 lines
4.1 KiB
Ruby
Raw Normal View History

class Users::DescriptionController < UsersController
2016-01-26 15:52:05 +01:00
before_action only: [:show] do
authorized_routes? self.class
end
before_action :check_autorisation_donnees, only: [:show]
before_action :check_starter_dossier_informations, only: [:show]
2015-08-10 11:05:06 +02:00
def show
@dossier ||= current_user_dossier.decorate
@procedure = @dossier.procedure
@champs = @dossier.ordered_champs
2017-03-29 22:17:50 +02:00
@headers = @champs.select { |c| c.type_champ == 'header_section' }
unless @dossier.can_be_en_construction?
flash[:alert] = t('errors.messages.procedure_archived')
end
2015-08-18 10:43:36 +02:00
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to url_for(root_path)
2015-08-10 11:05:06 +02:00
end
def update
dossier = current_user_dossier
procedure = dossier.procedure
return head :forbidden unless dossier.can_be_en_construction?
ChampsService.save_champs(dossier.champs, params) if params[:champs]
if procedure.cerfa_flag? && params[:cerfa_pdf]
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: dossier, user: current_user)
return redirect_to_description_with_errors(dossier, cerfa.errors.full_messages) unless cerfa.save
end
2015-08-10 11:05:06 +02:00
errors_upload = PiecesJustificativesService.upload!(dossier, current_user, params)
return redirect_to_description_with_errors(dossier, errors_upload) if errors_upload.any?
2015-08-10 11:05:06 +02:00
2017-12-04 16:17:15 +01:00
if params[:champs] && !brouillon_submission?
errors =
ChampsService.build_error_messages(dossier.champs) +
PiecesJustificativesService.missing_pj_error_messages(dossier)
return redirect_to_description_with_errors(dossier, errors) if errors.any?
end
2017-12-04 16:17:15 +01:00
if brouillon_submission?
flash.notice = 'Votre brouillon a bien été sauvegardé.'
redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon)
else
2017-12-04 16:17:15 +01:00
if dossier.brouillon?
NotificationMailer.send_notification(dossier, procedure.initiated_mail_template).deliver_now!
dossier.en_construction!
end
2016-10-27 14:02:38 +02:00
flash.notice = 'Félicitations, votre demande a bien été enregistrée.'
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: dossier.id)
end
2015-08-10 11:05:06 +02:00
end
def pieces_justificatives
invite = current_user.invite? params[:dossier_id]
@dossier ||= Dossier.find(params[:dossier_id]) if invite
@dossier ||= current_user_dossier
if @dossier.procedure.cerfa_flag?
unless params[:cerfa_pdf].nil?
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user)
unless cerfa.save
flash.alert = cerfa.errors.full_messages
end
end
end
if !((errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)).empty?)
if flash.alert.nil?
flash.alert = errors_upload
else
flash.alert = [flash.alert] + errors_upload
end
else
flash.notice = 'Nouveaux fichiers envoyés' if flash.alert.nil?
@dossier.next_step! 'user', 'update'
end
return redirect_to users_dossiers_invite_path(id: current_user.invites.find_by_dossier_id(@dossier.id).id) if invite
redirect_to users_dossier_recapitulatif_path
end
2016-01-26 15:52:05 +01:00
def self.route_authorization
{
2017-12-04 16:17:15 +01:00
states: [:brouillon, :en_construction]
2016-01-26 15:52:05 +01:00
}
end
2015-08-10 11:05:06 +02:00
private
def redirect_to_description_with_errors(dossier, errors)
errors_to_display = if errors.count > 3
errors.take(3) + ['...']
else
errors
end
flash.alert = errors_to_display
redirect_to users_dossier_description_path(dossier_id: dossier.id)
end
2017-12-04 16:17:15 +01:00
def brouillon_submission?
params[:submit] && params[:submit].keys.first == 'brouillon'
end
def check_autorisation_donnees
2017-01-03 12:53:27 +01:00
@dossier = current_user_dossier
redirect_to url_for(users_dossier_path(@dossier.id)) if @dossier.autorisation_donnees.nil? || !@dossier.autorisation_donnees
end
def check_starter_dossier_informations
@dossier ||= current_user_dossier
if (@dossier.procedure.for_individual? && @dossier.individual.nil?) ||
(!@dossier.procedure.for_individual? && @dossier.entreprise.nil?)
redirect_to url_for(users_dossier_path(@dossier.id))
end
end
2015-08-10 11:05:06 +02:00
end