Skip to content

Commit

Permalink
Merge pull request #11067 from tchak/refactor-dossier-controller
Browse files Browse the repository at this point in the history
refactor(dossier): cleanup user controller
  • Loading branch information
tchak authored Nov 26, 2024
2 parents 7932085 + 1f5dd3b commit 755a526
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 158 deletions.
3 changes: 1 addition & 2 deletions app/components/attachment/edit_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ class Attachment::EditComponent < ApplicationComponent

EXTENSIONS_ORDER = ['jpeg', 'png', 'pdf', 'zip'].freeze

def initialize(champ: nil, auto_attach_url: nil, attached_file:, direct_upload: true, index: 0, as_multiple: false, view_as: :link, user_can_destroy: true, user_can_replace: false, attachments: [], max: nil, **kwargs)
def initialize(champ: nil, auto_attach_url: nil, attached_file:, direct_upload: true, index: 0, as_multiple: false, view_as: :link, user_can_destroy: true, attachments: [], max: nil, **kwargs)
@champ = champ
@attached_file = attached_file
@direct_upload = direct_upload
@index = index
@view_as = view_as
@user_can_destroy = user_can_destroy
@user_can_replace = user_can_replace
@as_multiple = as_multiple
@auto_attach_url = auto_attach_url

Expand Down
2 changes: 1 addition & 1 deletion app/components/attachment/multiple_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Attachment::MultipleComponent < ApplicationComponent

delegate :count, :empty?, to: :attachments, prefix: true

def initialize(champ: nil, attached_file:, form_object_name: nil, view_as: :link, user_can_destroy: true, user_can_replace: false, max: nil)
def initialize(champ: nil, attached_file:, form_object_name: nil, view_as: :link, user_can_destroy: true, max: nil)
@champ = champ
@attached_file = attached_file
@form_object_name = form_object_name
Expand Down
40 changes: 38 additions & 2 deletions app/components/dossiers/edit_footer_component.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Dossiers::EditFooterComponent < ApplicationComponent
delegate :can_passer_en_construction?, to: :@dossier
delegate :can_passer_en_construction?, :can_transition_to_en_construction?, :forked_with_changes?, to: :@dossier

def initialize(dossier:, annotation:)
@dossier = dossier
Expand All @@ -18,7 +18,43 @@ def annotation?
@annotation.present?
end

def disabled_submit_buttons_options
def can_submit?
can_submit_draft? || can_submit_en_construction?
end

def can_submit_draft?
!annotation? && can_transition_to_en_construction?
end

def can_submit_en_construction?
forked_with_changes?
end

def submit_button_label
if can_submit_draft?
t('.submit')
else
t('.submit_changes')
end
end

def submit_button_path
if can_submit_draft?
brouillon_dossier_path(@dossier)
else
modifier_dossier_path(@dossier.editing_fork_origin)
end
end

def submit_button_options
if can_submit_draft?
submit_draft_button_options
else
submit_en_construction_button_options
end
end

def disabled_submit_button_options
{
class: 'fr-text--sm fr-mb-0 fr-mr-2w',
data: { 'fr-opened': "true" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@
.send-dossier-actions-bar
= render Dossiers::AutosaveFooterComponent.new(dossier: @dossier, annotation: annotation?)

- if !annotation? && @dossier.can_transition_to_en_construction?
- if can_submit?
- if !can_passer_en_construction?
= link_to t('.submit_disabled'), "#", disabled_submit_buttons_options
= button_to t('.submit'), brouillon_dossier_url(@dossier), submit_draft_button_options

- if @dossier.forked_with_changes?
- if !can_passer_en_construction?
= link_to t('.submit_disabled'), "#", disabled_submit_buttons_options
= button_to t('.submit_changes'), modifier_dossier_url(@dossier.editing_fork_origin), submit_en_construction_button_options

= link_to t('.submit_disabled'), "#", disabled_submit_button_options
= button_to submit_button_label, submit_button_path, submit_button_options

- if @dossier.brouillon? && !owner?
.fr-pb-2w.invite-cannot-submit
= render ::Dsfr::AlertComponent.new(state: :info, title: nil, size: :sm, heading_level: :p, extra_class_names:'') do |c|
- c.with_body do
%p.fr-pb-0= t('.invite_notice').html_safe



- if !annotation?
= render partial: "shared/dossiers/submit_is_over", locals: { dossier: @dossier }
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def view_as
end
end

def user_can_destroy?
!@champ.mandatory? || @champ.dossier.brouillon?
end

def max
[true, nil].include?(@champ.procedure&.piece_justificative_multiple?) ? Attachment::MultipleComponent::DEFAULT_MAX_ATTACHMENTS : 1
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= render Attachment::MultipleComponent.new(champ: @champ, attached_file: @champ.piece_justificative_file, form_object_name: @form.object_name, view_as:, user_can_destroy: user_can_destroy?, max:) do |c|
= render Attachment::MultipleComponent.new(champ: @champ, attached_file: @champ.piece_justificative_file, form_object_name: @form.object_name, view_as:, max:) do |c|
- if @champ.type_de_champ.piece_justificative_template&.attached?
- c.with_template do
= render partial: "shared/piece_justificative_template", locals: { champ: @champ }
4 changes: 0 additions & 4 deletions app/components/editable_champ/titre_identite_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ class EditableChamp::TitreIdentiteComponent < EditableChamp::EditableChampBaseCo
def dsfr_input_classname
'fr-input'
end

def user_can_destroy?
!@champ.mandatory? || @champ.dossier.brouillon?
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- if @champ.type_de_champ.piece_justificative_template&.attached?
= render partial: "shared/piece_justificative_template", locals: { champ: @champ }
= render Attachment::EditComponent.new(champ: @form.object, attached_file: @champ.piece_justificative_file, attachment: @champ.piece_justificative_file[0], form_object_name: @form.object_name,
user_can_destroy: user_can_destroy?)
= render Attachment::EditComponent.new(champ: @form.object, attached_file: @champ.piece_justificative_file, attachment: @champ.piece_justificative_file[0], form_object_name: @form.object_name)
8 changes: 7 additions & 1 deletion app/controllers/champs/piece_justificative_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ def attach_piece_justificative
save_succeed = @champ.save
end

@champ.dossier.update(last_champ_updated_at: Time.zone.now.utc) if save_succeed
if save_succeed && dossier.brouillon?
dossier.touch(:last_champ_updated_at, :last_champ_piece_jointe_updated_at)
end

save_succeed
end

def dossier
@champ.dossier
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/create_avis_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create_avis_from_params(dossier, instructeur_or_expert, confidentiel = false
persisted, failed = create_results.partition(&:persisted?)

if persisted.any?
dossier.update!(last_avis_updated_at: Time.zone.now)
dossier.touch(:last_avis_updated_at)
sent_emails_addresses = []
persisted.each do |avis|
avis.dossier.demander_un_avis!(avis)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/instructeurs/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def pending_correction

if commentaire.valid?
dossier.flag_as_pending_correction!(commentaire, params[:reason].presence)
dossier.update!(last_commentaire_updated_at: Time.zone.now)
dossier.touch(:last_commentaire_updated_at)
current_instructeur.follow(dossier)

flash.notice = "Dossier marqué comme en attente de correction."
Expand All @@ -275,7 +275,7 @@ def create_commentaire
@commentaire = CommentaireService.create(current_instructeur, dossier, commentaire_params)

if @commentaire.errors.empty?
@commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
@commentaire.dossier.touch(:last_commentaire_updated_at)
current_instructeur.follow(dossier)
flash.notice = "Message envoyé"
redirect_to messagerie_instructeur_dossier_path(procedure, dossier)
Expand All @@ -300,7 +300,7 @@ def create_avis
def update_annotations
dossier_with_champs.update_champs_attributes(champs_private_attributes_params, :private, updated_by: current_user.email)
if dossier.champs.any?(&:changed_for_autosave?)
dossier.last_champ_private_updated_at = Time.zone.now
dossier.touch(:last_champ_private_updated_at)
end

dossier.save(context: :champs_private_value)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/instructeurs/procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def create_multiple_commentaire
dossiers.each do |dossier|
commentaire = CommentaireService.create(current_instructeur, dossier, bulk_message_params.except(:targets))
if commentaire.errors.empty?
commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
commentaire.dossier.touch(:last_commentaire_updated_at)
else
errors << dossier.id
end
Expand Down
84 changes: 28 additions & 56 deletions app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class DossiersController < UserController
layout 'procedure_context', only: [:identite, :update_identite, :siret, :update_siret]

ACTIONS_ALLOWED_TO_ANY_USER = [:index, :new, :transferer_all, :deleted_dossiers]
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]
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :destroy, :demande, :messagerie, :brouillon, :submit_brouillon, :submit_en_construction, :modifier, :update, :create_commentaire, :papertrail, :restore, :champ]

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
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_siret, :brouillon, :submit_brouillon, :submit_en_construction, :modifier, :modifier_legacy, :update, :champ]
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_siret, :brouillon, :submit_brouillon, :submit_en_construction, :modifier, :update, :champ]
before_action :ensure_dossier_can_be_filled, only: [:brouillon, :modifier, :submit_brouillon, :submit_en_construction, :update]
before_action :ensure_dossier_can_be_viewed, only: [:show]
before_action :forbid_invite_submission!, only: [:submit_brouillon]
Expand Down Expand Up @@ -233,17 +233,9 @@ def submit_brouillon

if @dossier.errors.blank? && @dossier.can_passer_en_construction?
@dossier.passer_en_construction!
@dossier.process_declarative!
@dossier.process_sva_svr!
@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
redirect_to merci_dossier_path(@dossier)
else
respond_to do |format|
format.html { render :brouillon }
format.turbo_stream
end
render :brouillon
end
end

Expand All @@ -261,17 +253,7 @@ def extend_conservation_and_restore

def modifier
@dossier = dossier_with_champs
end

# 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
@dossier_for_editing = dossier.owner_editing_fork
end

def submit_en_construction
Expand All @@ -284,29 +266,21 @@ def submit_en_construction

submit_dossier_and_compute_errors

if @dossier.errors.blank? && @dossier.can_passer_en_construction?
editing_fork_origin.merge_fork(@dossier)
if dossier.errors.blank? && dossier.can_passer_en_construction?
editing_fork_origin.merge_fork(dossier)
editing_fork_origin.submit_en_construction!

redirect_to dossier_path(editing_fork_origin)
else
respond_to do |format|
format.html do
render :modifier
end

format.turbo_stream do
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_public_attributes_params, dossier.champs.filter(&:public?))
render :update, layout: false
end
end
@dossier_for_editing = dossier
@dossier = editing_fork_origin
render :modifier
end
end

def update
@dossier = dossier.en_construction? ? dossier.find_editing_fork(dossier.user) : dossier
@dossier = dossier_with_champs(pj_template: false)
@can_passer_en_construction_was, @can_passer_en_construction_is = @dossier.track_can_passer_en_construction do
@can_passer_en_construction_was, @can_passer_en_construction_is = dossier.track_can_passer_en_construction do
update_dossier_and_compute_errors
end

Expand All @@ -324,8 +298,8 @@ def merci

def champ
@dossier = dossier_with_champs(pj_template: false)
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])
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])

respond_to do |format|
format.turbo_stream do
Expand Down Expand Up @@ -559,36 +533,34 @@ def set_dossier_as_editing_fork
end

def update_dossier_and_compute_errors
@dossier.update_champs_attributes(champs_public_attributes_params, :public, updated_by: current_user.email)
updated_champs = @dossier.champs.filter(&:changed_for_autosave?)
if updated_champs.present?
@dossier.last_champ_updated_at = Time.zone.now
end
dossier.update_champs_attributes(champs_public_attributes_params, :public, updated_by: current_user.email)
updated_champs = dossier.champs.filter(&:changed_for_autosave?)

# We save the dossier without validating fields, and if it is successful and the client
# requests it, we ask for field validation errors.
if @dossier.save
if updated_champs.any?(&:used_by_routing_rules?)
@update_contact_information = true
RoutingEngine.compute(@dossier)
if dossier.save
if dossier.brouillon? && updated_champs.present?
dossier.touch(:last_champ_updated_at)
if updated_champs.any?(&:used_by_routing_rules?)
@update_contact_information = true
RoutingEngine.compute(dossier)
end
end

if params[:validate].present?
@dossier.valid?(:champs_public_value)
dossier.valid?(:champs_public_value)
end
end

@dossier.errors
end

def submit_dossier_and_compute_errors
@dossier.validate(:champs_public_value)
@dossier.check_mandatory_and_visible_champs
dossier.validate(:champs_public_value)
dossier.check_mandatory_and_visible_champs

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|
@dossier.errors.import(error)
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|
dossier.errors.import(error)
end
end
end
Expand Down
Loading

0 comments on commit 755a526

Please sign in to comment.