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

427 lines
13 KiB
Ruby
Raw Normal View History

2019-03-25 10:53:45 +01:00
module Users
class DossiersController < UserController
include DossierHelper
layout 'procedure_context', only: [:identite, :update_identite, :siret, :update_siret]
2018-11-01 13:00:35 +01:00
ACTIONS_ALLOWED_TO_ANY_USER = [:index, :recherche, :new]
2019-05-29 12:05:28 +02:00
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :demande, :messagerie, :brouillon, :update_brouillon, :modifier, :update, :create_commentaire]
before_action :ensure_ownership!, except: ACTIONS_ALLOWED_TO_ANY_USER + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
before_action :ensure_ownership_or_invitation!, only: ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
2019-05-29 12:05:28 +02:00
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_brouillon, :modifier, :update]
before_action :forbid_invite_submission!, only: [:update_brouillon]
before_action :forbid_closed_submission!, only: [:update_brouillon]
before_action :show_demarche_en_test_banner
2018-11-01 13:00:35 +01:00
before_action :store_user_location!, only: :new
def index
@user_dossiers = current_user.dossiers.includes(:procedure).order_by_updated_at.page(page)
@dossiers_invites = current_user.dossiers_invites.includes(:procedure).order_by_updated_at.page(page)
2020-12-07 15:10:26 +01:00
@dossiers_supprimes = current_user.deleted_dossiers.order_by_updated_at.page(page)
@statut = statut(@user_dossiers, @dossiers_invites, @dossiers_supprimes, params[:statut])
end
def show
if dossier.brouillon?
redirect_to brouillon_dossier_path(dossier)
return
end
@dossier = dossier
2019-12-15 22:10:35 +01:00
respond_to do |format|
format.pdf do
@include_infos_administration = false
render(file: 'dossiers/show', formats: [:pdf])
end
format.all
end
end
def demande
2018-08-14 15:06:44 +02:00
@dossier = dossier
end
2018-09-05 13:56:12 +02:00
def messagerie
@dossier = dossier
@commentaire = Commentaire.new
end
def attestation
if dossier.attestation&.pdf&.attached?
redirect_to dossier.attestation.pdf.service_url
else
flash.notice = "L'attestation n'est plus disponible sur ce dossier."
redirect_to dossier_path(dossier)
end
end
def identite
@dossier = dossier
@user = current_user
end
2018-02-08 17:13:15 +01:00
def update_identite
@dossier = dossier
2018-10-02 13:51:56 +02:00
if @dossier.individual.update(individual_params)
@dossier.update!(autorisation_donnees: true)
2018-02-08 17:13:15 +01:00
flash.notice = "Identité enregistrée"
redirect_to brouillon_dossier_path(@dossier)
2018-02-08 17:13:15 +01:00
else
2018-10-02 13:51:56 +02:00
flash.now.alert = @dossier.individual.errors.full_messages
2018-02-08 17:13:15 +01:00
render :identite
end
end
def siret
@dossier = dossier
end
def update_siret
@dossier = dossier
# 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
begin
2020-08-05 18:40:47 +02:00
etablissement = APIEntrepriseService.create_etablissement(@dossier, sanitized_siret, current_user.id)
rescue APIEntreprise::API::Error::RequestFailed, APIEntreprise::API::Error::BadGateway, APIEntreprise::API::Error::TimedOut
return render_siret_error(t('errors.messages.siret_network_error'))
end
if etablissement.nil?
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
flash.alert = 'Aucun établissement nest associé à ce dossier'
return redirect_to siret_dossier_path(@dossier)
end
end
def brouillon
2018-02-21 18:32:07 +01:00
@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 siret_dossier_path(@dossier)
2018-02-21 18:32:07 +01:00
end
end
end
# FIXME:
# - delegate draft save logic to champ ?
def update_brouillon
2018-02-21 18:32:07 +01:00
@dossier = dossier_with_champs
errors = update_dossier_and_compute_errors
2018-02-21 18:32:07 +01:00
if passage_en_construction? && errors.blank?
@dossier.passer_en_construction!
NotificationMailer.send_initiated_notification(@dossier).deliver_later
@dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
end
return redirect_to(merci_dossier_path(@dossier))
elsif errors.present?
2018-02-21 18:32:07 +01:00
flash.now.alert = errors
else
flash.now.notice = 'Votre brouillon a bien été sauvegardé.'
end
respond_to do |format|
format.html { render :brouillon }
format.js { render :brouillon }
2018-02-21 18:32:07 +01:00
end
end
def extend_conservation
dossier.update(en_construction_conservation_extension: dossier.en_construction_conservation_extension + 1.month)
flash[:notice] = 'Votre dossier sera conservé un mois supplémentaire'
redirect_to dossier_path(@dossier)
end
2018-09-05 18:23:10 +02:00
def modifier
@dossier = dossier_with_champs
end
2018-09-06 11:39:46 +02:00
def update
@dossier = dossier_with_champs
errors = update_dossier_and_compute_errors
2018-09-06 11:39:46 +02:00
if errors.present?
flash.now.alert = errors
render :modifier
else
2018-10-02 12:23:53 +02:00
redirect_to demande_dossier_path(@dossier)
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
2018-09-05 13:56:12 +02:00
def create_commentaire
@commentaire = CommentaireService.build(current_user, dossier, commentaire_params)
2018-09-05 13:56:12 +02:00
if @commentaire.save
@commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
dossier.followers_instructeurs
.with_instant_email_message_notifications
.each do |instructeur|
DossierMailer.notify_new_commentaire_to_instructeur(dossier, instructeur.email).deliver_later
end
2018-09-04 18:27:34 +02:00
flash.notice = "Votre message a bien été envoyé à linstructeur en charge de votre dossier."
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
def ask_deletion
dossier = current_user.dossiers.includes(:user, procedure: :administrateurs).find(params[:id])
if dossier.can_be_deleted_by_user?
dossier.discard_and_keep_track!(current_user, :user_request)
flash.notice = 'Votre dossier a bien été supprimé.'
redirect_to 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."
2018-11-01 13:00:35 +01:00
redirect_to dossier_path(dossier)
end
end
def recherche
@search_terms = params[:q]
return redirect_to dossiers_path if @search_terms.blank?
@dossiers = DossierSearchService.matching_dossiers_for_user(@search_terms, current_user).page(page)
if @dossiers.present?
# we need the page condition when accessing page n with n>1 when the page has only 1 result
# in order to avoid an unpleasant redirection when changing page
if @dossiers.count == 1 && page == 1
redirect_to url_for_dossier(@dossiers.first)
else
render :index
end
else
flash.alert = "Vous navez pas de dossiers contenant « #{@search_terms} »."
redirect_to dossiers_path
end
end
2018-11-01 13:00:35 +01:00
def new
erase_user_location!
begin
if params[:brouillon]
procedure = Procedure.brouillon.find(params[:procedure_id])
else
procedure = Procedure.publiees.find(params[:procedure_id])
end
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.procedure_not_found')
return redirect_to url_for dossiers_path
2018-11-01 13:00:35 +01:00
end
dossier = Dossier.new(
2020-06-26 12:00:21 +02:00
revision: procedure.active_revision,
groupe_instructeur: procedure.defaut_groupe_instructeur,
user: current_user,
state: Dossier.states.fetch(:brouillon)
)
dossier.build_default_individual
dossier.save!
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
def dossier_for_help
dossier_id = params[:id] || params[:dossier_id]
@dossier || (dossier_id.present? && Dossier.find_by(id: dossier_id.to_i))
end
private
2020-12-07 15:10:26 +01:00
# if the status tab is filled, then this tab
# else first filled tab
# else mes-dossiers
def statut(mes_dossiers, dossiers_invites, dossiers_supprimes, params_statut)
tabs = {
'mes-dossiers' => mes_dossiers.present?,
'dossiers-invites' => dossiers_invites.present?,
'dossiers-supprimes' => dossiers_supprimes.present?
}
if tabs[params_statut]
params_statut
2020-11-26 15:13:32 +01:00
else
2020-12-07 15:10:26 +01:00
tabs
.filter { |_tab, filled| filled }
.map { |tab, _| tab }
.first || 'mes-dossiers'
2020-11-26 15:13:32 +01:00
end
end
2018-11-01 13:00:35 +01:00
def store_user_location!
store_location_for(:user, request.fullpath)
end
def erase_user_location!
clear_stored_location_for(:user)
2018-11-01 13:00:35 +01:00
end
def show_demarche_en_test_banner
if @dossier.present? && @dossier.procedure.brouillon?
2020-12-16 10:27:46 +01:00
flash.now.alert = "Ce dossier est déposé sur une démarche en test. Toute modification de la démarche par l'administrateur (ajout d'un champ, publication de la démarche...) entraînera sa suppression."
end
end
def ensure_dossier_can_be_updated
2019-02-06 18:20:35 +01:00
if !dossier.can_be_updated_by_user?
flash.alert = 'Votre dossier ne peut plus être modifié'
redirect_to dossiers_path
end
end
def page
[params[:page].to_i, 1].max
end
# FIXME: require(:dossier) when all the champs are united
def champs_params
params.permit(dossier: {
2018-04-03 17:53:14 +02:00
champs_attributes: [
:id, :value, :external_id, :primary_value, :secondary_value, :piece_justificative_file, value: [],
champs_attributes: [:id, :_destroy, :value, :external_id, :primary_value, :secondary_value, :piece_justificative_file, value: []]
2018-04-03 17:53:14 +02:00
]
})
2018-02-21 18:32:07 +01:00
end
def dossier
2018-09-04 17:47:34 +02:00
@dossier ||= Dossier.find(params[:id] || params[:dossier_id])
end
2018-02-21 18:32:07 +01:00
def dossier_with_champs
Dossier.with_champs.find(params[:id])
2018-02-21 18:32:07 +01:00
end
def change_groupe_instructeur?
params[:dossier][:groupe_instructeur_id].present? && @dossier.groupe_instructeur_id != params[:dossier][:groupe_instructeur_id].to_i
end
def update_dossier_and_compute_errors
errors = []
if champs_params[:dossier]
@dossier.assign_attributes(champs_params[:dossier])
# FIXME in some cases a removed repetition bloc row is submitted.
# In this case it will be trated as a new records and action will fail.
@dossier.champs.filter(&:repetition?).each do |champ|
champ.champs = champ.champs.filter(&:persisted?)
end
if @dossier.champs.any?(&:changed?)
@dossier.last_champ_updated_at = Time.zone.now
end
if !@dossier.save
errors += @dossier.errors.full_messages
elsif change_groupe_instructeur?
groupe_instructeur = @dossier.procedure.groupe_instructeurs.find(params[:dossier][:groupe_instructeur_id])
@dossier.assign_to_groupe_instructeur(groupe_instructeur)
end
end
if !save_draft?
2019-01-30 16:14:15 +01:00
errors += @dossier.check_mandatory_champs
end
errors
end
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!
end
end
2018-02-08 17:13:15 +01:00
2018-03-29 15:25:05 +02:00
def ensure_ownership_or_invitation!
if !current_user.owns_or_invite?(dossier)
2018-03-29 15:25:05 +02:00
forbidden!
end
end
def forbid_invite_submission!
2018-05-30 18:26:23 +02:00
if passage_en_construction? && !current_user.owns?(dossier)
forbidden!
end
end
def forbid_closed_submission!
if passage_en_construction? && !dossier.can_transition_to_en_construction?
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
def render_siret_error(error_message)
flash.alert = error_message
render :siret
end
2018-02-08 17:13:15 +01:00
def individual_params
params.require(:individual).permit(:gender, :nom, :prenom, :birthdate)
end
def siret_params
params.require(:user).permit(:siret)
end
2018-09-05 13:56:12 +02:00
def commentaire_params
params.require(:commentaire).permit(:body, :piece_jointe)
2018-09-05 13:56:12 +02:00
end
def passage_en_construction?
dossier.brouillon? && !save_draft?
end
def save_draft?
dossier.brouillon? && !params[:submit_draft]
2018-02-21 18:32:07 +01:00
end
end
end