Skip to content

Commit

Permalink
refactor(dossier): cleanup user controller
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Nov 21, 2024
1 parent ed42eaa commit ebc9332
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 130 deletions.
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? || forked_with_changes?
end

def can_submit_draft?
!annotation? && can_transition_to_en_construction?
end

def can_submit_en_construction?
forked_with_changes?

Check warning on line 30 in app/components/dossiers/edit_footer_component.rb

View check run for this annotation

Codecov / codecov/patch

app/components/dossiers/edit_footer_component.rb#L30

Added line #L30 was not covered by tests
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 @@ -14,7 +14,7 @@ def view_as
end

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

def max
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])

Check warning on line 302 in app/controllers/users/dossiers_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/users/dossiers_controller.rb#L301-L302

Added lines #L301 - L302 were not covered by tests

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
30 changes: 12 additions & 18 deletions app/models/concerns/dossier_clone_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ def owner_editing_fork
find_or_create_editing_fork(user).tap { DossierPreloader.load_one(_1) }
end

def reset_editing_fork!
if editing_fork? && forked_with_changes?
destroy_editing_fork!
end
end

def destroy_editing_fork!
if editing_fork?
update!(hidden_by_administration_at: Time.current, hidden_by_reason: :stale_fork)
Expand All @@ -43,6 +37,18 @@ def editing_fork?
editing_fork_origin_id.present?
end

def forked_with_changes?
if forked_diff.present?
forked_diff.values.any?(&:present?) || forked_groupe_instructeur_changed?
end
end

def champ_forked_with_changes?(champ)
if forked_diff.present?
forked_diff.values.any? { |champs| champs.any? { _1.public_id == champ.public_id } }
end
end

def make_diff(editing_fork)
origin_champs_index = project_champs_public_all.index_by(&:public_id)
forked_champs_index = editing_fork.project_champs_public_all.index_by(&:public_id)
Expand Down Expand Up @@ -126,18 +132,6 @@ def clone(user: nil, fork: false)
cloned_dossier.reload
end

def forked_with_changes?
if forked_diff.present?
forked_diff.values.any?(&:present?) || forked_groupe_instructeur_changed?
end
end

def champ_forked_with_changes?(champ)
if forked_diff.present?
forked_diff.values.any? { _1.include?(champ) }
end
end

private

def forked_diff
Expand Down
21 changes: 21 additions & 0 deletions app/models/concerns/dossier_state_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
module DossierStateConcern
extend ActiveSupport::Concern

def submit_en_construction!
self.traitements.submit_en_construction
save!

RoutingEngine.compute(self)

resolve_pending_correction!
process_sva_svr!
remove_piece_justificative_file_not_visible!

editing_forks.each(&:destroy_editing_fork!)
end

def after_passer_en_construction
self.conservation_extension = 0.days
self.depose_at = self.en_construction_at = self.traitements
Expand All @@ -16,12 +29,18 @@ def after_passer_en_construction
MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_construction))
procedure.compute_dossiers_count

process_declarative!
process_sva_svr!

index_search_terms_later
end

def after_commit_passer_en_construction
NotificationMailer.send_en_construction_notification(self).deliver_later
NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers?
groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
DossierMailer.notify_new_dossier_depose_to_instructeur(self, instructeur.email).deliver_later

Check warning on line 42 in app/models/concerns/dossier_state_concern.rb

View check run for this annotation

Codecov / codecov/patch

app/models/concerns/dossier_state_concern.rb#L42

Added line #L42 was not covered by tests
end
remove_piece_justificative_file_not_visible!
end

Expand Down Expand Up @@ -49,6 +68,8 @@ def after_commit_passer_en_instruction(h)
NotificationMailer.send_en_instruction_notification(self).deliver_later
NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers?
end

editing_forks.each(&:destroy_editing_fork!)
end

def after_passer_automatiquement_en_instruction
Expand Down
11 changes: 0 additions & 11 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -865,17 +865,6 @@ def email_template_for(state)
procedure.email_template_for(state)
end

def submit_en_construction!
self.traitements.submit_en_construction
save!

RoutingEngine.compute(self)

resolve_pending_correction!
process_sva_svr!
remove_piece_justificative_file_not_visible!
end

def process_declarative!
if procedure.declarative_accepte? && may_accepter_automatiquement?
accepter_automatiquement!
Expand Down
2 changes: 1 addition & 1 deletion app/views/administrateurs/procedures/apercu.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
active: @tab == 'annotations-privees')

- if @tab == 'dossier'
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier }
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier, dossier_for_editing: @dossier }
- else
= render partial: "shared/dossiers/edit_annotations", locals: { dossier: @dossier, seen_at: nil }
4 changes: 1 addition & 3 deletions app/views/shared/dossiers/_edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
- dossier_for_editing = dossier.en_construction? ? dossier.owner_editing_fork : dossier

- if dossier.france_connected_with_one_identity? && current_user.instructeur? && !current_user.owns_or_invite?(dossier)
- content_for(:notice_info) do
= render partial: "shared/dossiers/france_connect_informations_notice", locals: { user_information: dossier.user.france_connect_informations.first }
Expand All @@ -10,7 +8,7 @@
= render NestedForms::FormOwnerComponent.new
= form_for dossier_for_editing, url: brouillon_dossier_url(dossier), method: :patch, html: { id: 'dossier-edit-form', class: 'form', multipart: true, novalidate: 'novalidate' } do |f|

= render Dossiers::ErrorsFullMessagesComponent.new(dossier: dossier)
= render Dossiers::ErrorsFullMessagesComponent.new(dossier: dossier_for_editing)
%header.mb-6
.fr-highlight
%p.fr-text--sm
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/dossiers/brouillon.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
.fr-container
= render partial: "shared/dossiers/header", locals: { dossier: @dossier }

= render partial: "shared/dossiers/edit", locals: { dossier: @dossier }
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier, dossier_for_editing: @dossier }
2 changes: 1 addition & 1 deletion app/views/users/dossiers/modifier.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
= render partial: 'users/dossiers/show/header', locals: { dossier: @dossier }

.container
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier }
= render partial: "shared/dossiers/edit", locals: { dossier: @dossier, dossier_for_editing: @dossier_for_editing }
Loading

0 comments on commit ebc9332

Please sign in to comment.