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

236 lines
6.8 KiB
Ruby
Raw Normal View History

2015-09-23 19:20:03 +02:00
class Users::DossiersController < UsersController
2016-01-08 11:39:04 +01:00
include SmartListing::Helper::ControllerExtensions
helper SmartListing::Helper
before_action :authenticate_user!, except: :commencer
before_action :check_siret, only: :siret_informations
2016-01-26 15:52:05 +01:00
before_action only: [:show] do
authorized_routes? self.class
end
def index
2017-04-20 10:55:54 +02:00
@liste ||= params[:liste] || 'a_traiter'
2017-04-11 17:56:11 +02:00
@user_dossiers = current_user.dossiers
@dossiers_filtered = case @liste
when 'brouillon'
2017-05-26 18:59:21 +02:00
@user_dossiers.state_brouillon.order_by_updated_at
2017-04-11 17:56:11 +02:00
when 'a_traiter'
2017-05-26 18:59:21 +02:00
@user_dossiers.state_en_construction.order_by_updated_at
2017-04-11 17:56:11 +02:00
when 'en_instruction'
2017-05-26 18:59:21 +02:00
@user_dossiers.state_en_instruction.order_by_updated_at
2017-04-11 17:56:11 +02:00
when 'termine'
2017-05-26 18:59:21 +02:00
@user_dossiers.state_termine.order_by_updated_at
2017-04-11 17:56:11 +02:00
when 'invite'
current_user.invites
else
2016-10-11 18:05:49 +02:00
return redirect_to users_dossiers_path
end
2016-01-08 11:39:04 +01:00
@dossiers = smart_listing_create :dossiers,
2017-04-11 17:56:11 +02:00
@dossiers_filtered,
partial: "users/dossiers/list",
array: true
end
def commencer
unless params[:procedure_path].nil?
2017-01-10 15:27:40 +01:00
procedure_path = ProcedurePath.where(path: params[:procedure_path]).last
if procedure_path.nil?
flash.alert = "Procédure inconnue"
return redirect_to root_path
else
procedure = procedure_path.procedure
end
end
2016-11-07 17:08:33 +01:00
if procedure.archived?
@dossier = Dossier.new(procedure: procedure)
return render 'commencer/archived'
end
redirect_to new_users_dossier_path(procedure_id: procedure.id)
rescue ActiveRecord::RecordNotFound
error_procedure
end
def new
procedure = Procedure.where(archived: false, published: true).find(params[:procedure_id])
dossier = Dossier.create(procedure: procedure, user: current_user, state: 'draft')
siret = params[:siret] || current_user.siret
update_current_user_siret! siret unless siret.nil?
redirect_to users_dossier_path(id: dossier.id)
rescue ActiveRecord::RecordNotFound
error_procedure
end
2015-11-26 12:29:34 +01:00
def show
@facade = facade
@siret = current_user.siret unless current_user.siret.nil?
2015-09-23 19:20:03 +02:00
if @facade.procedure.for_individual? && current_user.loged_in_with_france_connect?
individual = @facade.dossier.individual
individual.update_column :gender, @facade.dossier.france_connect_information.gender
individual.update_column :nom, @facade.dossier.france_connect_information.family_name
individual.update_column :prenom, @facade.dossier.france_connect_information.given_name
individual.update_column :birthdate, @facade.dossier.france_connect_information.birthdate.strftime("%d/%m/%Y")
end
2015-09-23 19:20:03 +02:00
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to url_for users_dossiers_path
2015-09-23 19:20:03 +02:00
end
def siret_informations
@facade = facade params[:dossier_id]
2015-09-23 19:20:03 +02:00
update_current_user_siret! siret
dossier = DossierService.new(@facade.dossier, siret, current_user.france_connect_information).dossier_informations!
2016-07-28 10:20:53 +02:00
if dossier.entreprise.nil? || dossier.etablissement.nil?
return errors_valid_siret
end
2015-09-23 19:20:03 +02:00
@facade = facade params[:dossier_id]
2015-09-23 19:20:03 +02:00
if @facade.procedure.individual_with_siret?
render '/dossiers/add_siret', formats: 'js'
else
render '/dossiers/new_siret', formats: 'js'
end
rescue RestClient::ResourceNotFound, RestClient::BadRequest
2015-12-03 15:02:22 +01:00
errors_valid_siret
2015-09-23 19:20:03 +02:00
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to url_for users_dossiers_path
end
def change_siret
Dossier.find(params[:dossier_id]).reset!
@facade = facade params[:dossier_id]
if @facade.procedure.individual_with_siret?
render '/dossiers/add_siret', formats: 'js'
else
render '/dossiers/new_siret', formats: 'js'
end
2015-09-23 19:20:03 +02:00
end
def update
@facade = facade params[:dossier][:id]
2015-09-23 19:20:03 +02:00
if checked_autorisation_donnees?
unless Dossier.find(@facade.dossier.id).update_attributes update_params_with_formatted_birthdate
flash.alert = @facade.dossier.errors.full_messages.join('<br />').html_safe
return redirect_to users_dossier_path(id: @facade.dossier.id)
2016-08-30 11:18:43 +02:00
end
if @facade.dossier.procedure.module_api_carto.use_api_carto
redirect_to url_for(controller: :carte, action: :show, dossier_id: @facade.dossier.id)
else
redirect_to url_for(controller: :description, action: :show, dossier_id: @facade.dossier.id)
end
2015-09-23 19:20:03 +02:00
else
flash.alert = 'Les conditions sont obligatoires.'
redirect_to users_dossier_path(id: @facade.dossier.id)
2015-09-23 19:20:03 +02:00
end
end
2016-01-26 15:52:05 +01:00
def self.route_authorization
{
states: [:draft]
}
end
def destroy
dossier = current_user.dossiers.find(params[:id])
if dossier.brouillon?
dossier.destroy
flash.notice = 'Brouillon supprimé'
end
redirect_to url_for users_dossiers_path
end
def text_summary
dossier = Dossier.find(params[:dossier_id])
render json: { textSummary: dossier.text_summary }
rescue ActiveRecord::RecordNotFound
render json: {}, status: 404
end
2015-09-23 19:20:03 +02:00
private
2015-12-03 15:02:22 +01:00
def check_siret
errors_valid_siret unless Siret.new(siret: siret).valid?
end
def errors_valid_siret
flash.alert = t('errors.messages.invalid_siret')
@facade = facade params[:dossier_id]
render '/dossiers/new_siret', formats: :js, locals: {invalid_siret: siret}
2015-12-03 15:02:22 +01:00
end
2015-09-23 19:20:03 +02:00
def update_params
2016-10-06 11:21:07 +02:00
params.require(:dossier).permit(:id, :autorisation_donnees, individual_attributes: [:gender, :nom, :prenom, :birthdate])
2015-09-23 19:20:03 +02:00
end
def update_params_with_formatted_birthdate
editable_params = update_params
# If the user was shown a date input field (if its browser supports it),
# the returned param will follow the YYYY-MM-DD pattern, which we need
# do convert to the DD/MM/YYYY pattern we use
if editable_params &&
editable_params[:individual_attributes] &&
editable_params[:individual_attributes][:birthdate] &&
editable_params[:individual_attributes][:birthdate] =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
original_birthdate = editable_params[:individual_attributes][:birthdate]
formatted_birthdate = I18n.l(original_birthdate.to_date, format: '%d/%m/%Y')
editable_params[:individual_attributes][:birthdate] = formatted_birthdate
end
editable_params
end
2015-09-23 19:20:03 +02:00
def checked_autorisation_donnees?
update_params[:autorisation_donnees] == '1'
end
def siret
create_params[:siret]
2015-09-23 19:20:03 +02:00
end
def create_params
params.require(:dossier).permit(:siret)
end
def error_procedure
2015-12-24 15:22:30 +01:00
flash.alert = t('errors.messages.procedure_not_found')
2015-12-24 15:22:30 +01:00
redirect_to url_for users_dossiers_path
end
def update_current_user_siret! siret
current_user.update_attributes(siret: siret)
end
def facade id = params[:id]
DossierFacades.new id, current_user.email
end
end