diff --git a/app/components/dossiers/edit_footer_component.rb b/app/components/dossiers/edit_footer_component.rb index b5acc51f24d..ef40ba0669f 100644 --- a/app/components/dossiers/edit_footer_component.rb +++ b/app/components/dossiers/edit_footer_component.rb @@ -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 @@ -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? + 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" }, diff --git a/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml b/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml index 2f0f59b2b1a..f6a727a3e47 100644 --- a/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml +++ b/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml @@ -2,16 +2,10 @@ .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 @@ -19,7 +13,5 @@ - 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 } diff --git a/app/components/editable_champ/piece_justificative_component.rb b/app/components/editable_champ/piece_justificative_component.rb index 47b873e8d51..a0d1f944f75 100644 --- a/app/components/editable_champ/piece_justificative_component.rb +++ b/app/components/editable_champ/piece_justificative_component.rb @@ -14,7 +14,7 @@ def view_as end def user_can_destroy? - !@champ.mandatory? || @champ.dossier.brouillon? + true end def max diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index fbc1648889b..2e82070d973 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -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] @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/models/concerns/dossier_clone_concern.rb b/app/models/concerns/dossier_clone_concern.rb index 193561fe41a..6f5aab6f280 100644 --- a/app/models/concerns/dossier_clone_concern.rb +++ b/app/models/concerns/dossier_clone_concern.rb @@ -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) @@ -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) @@ -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 diff --git a/app/models/concerns/dossier_state_concern.rb b/app/models/concerns/dossier_state_concern.rb index 7b60de6adae..a9ea4c7412f 100644 --- a/app/models/concerns/dossier_state_concern.rb +++ b/app/models/concerns/dossier_state_concern.rb @@ -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 @@ -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 + end remove_piece_justificative_file_not_visible! end @@ -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 diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 3d4b9ab9b97..f41be6147e7 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -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! diff --git a/app/views/administrateurs/procedures/apercu.html.haml b/app/views/administrateurs/procedures/apercu.html.haml index 5941fdbb4bd..c847b25b843 100644 --- a/app/views/administrateurs/procedures/apercu.html.haml +++ b/app/views/administrateurs/procedures/apercu.html.haml @@ -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 } diff --git a/app/views/shared/dossiers/_edit.html.haml b/app/views/shared/dossiers/_edit.html.haml index c797d35f237..da11f1c03c4 100644 --- a/app/views/shared/dossiers/_edit.html.haml +++ b/app/views/shared/dossiers/_edit.html.haml @@ -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 } @@ -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 diff --git a/app/views/users/dossiers/brouillon.html.haml b/app/views/users/dossiers/brouillon.html.haml index 0acdb89e571..428347dafe5 100644 --- a/app/views/users/dossiers/brouillon.html.haml +++ b/app/views/users/dossiers/brouillon.html.haml @@ -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 } diff --git a/app/views/users/dossiers/modifier.html.haml b/app/views/users/dossiers/modifier.html.haml index 7217909e07a..6a2b986750e 100644 --- a/app/views/users/dossiers/modifier.html.haml +++ b/app/views/users/dossiers/modifier.html.haml @@ -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 } diff --git a/config/routes.rb b/config/routes.rb index 518f513aaee..7352258ee10 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -380,7 +380,6 @@ post 'brouillon', to: 'dossiers#submit_brouillon' get 'modifier', to: 'dossiers#modifier' post 'modifier', to: 'dossiers#submit_en_construction' - patch 'modifier', to: 'dossiers#modifier_legacy' get 'champs/:stable_id', to: 'dossiers#champ', as: :champ get 'merci' get 'demande' diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 68fe7a83089..2b855d3a6e0 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -836,10 +836,11 @@ before { sign_in(user) } let(:procedure) { create(:procedure, :published, types_de_champ_public: [{}, { type: :piece_justificative }]) } - let!(:dossier) { create(:dossier, :en_construction, user:, procedure:) } - let(:first_champ) { dossier.project_champs_public.first } + let(:dossier) { create(:dossier, :en_construction, user:, procedure:) } + let!(:editing_fork) { dossier.owner_editing_fork } + let(:first_champ) { editing_fork.project_champs_public.first } + let(:piece_justificative_champ) { editing_fork.project_champs_public.last } let(:anchor_to_first_champ) { controller.helpers.link_to I18n.t('views.users.dossiers.fix_champ'), brouillon_dossier_path(anchor: first_champ.labelledby_id), class: 'error-anchor' } - let(:piece_justificative_champ) { dossier.project_champs_public.last } let(:value) { 'beautiful value' } let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') } let(:now) { Time.zone.parse('01/01/2100') } @@ -887,14 +888,9 @@ it 'updates the dossier timestamps' do subject - dossier.reload - expect(dossier.updated_at).to eq(now) - expect(dossier.last_champ_updated_at).to eq(now) - end - - it 'updates the dossier state' do - subject - expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) + editing_fork.reload + expect(editing_fork.updated_at).to eq(now) + expect(editing_fork.last_champ_updated_at).to eq(now) end it { is_expected.to have_http_status(:ok) } @@ -923,9 +919,9 @@ it 'updates the dossier timestamps' do subject - dossier.reload - expect(dossier.updated_at).to eq(now) - expect(dossier.last_champ_updated_at).to eq(now) + editing_fork.reload + expect(editing_fork.updated_at).to eq(now) + expect(editing_fork.last_champ_updated_at).to eq(now) end end end @@ -958,14 +954,10 @@ end context 'iban error' do + let(:types_de_champ_public) { [{ type: :iban }] } let(:value) { 'abc' } - before do - first_champ.type_de_champ.update!(type_champ: :iban, mandatory: true, libelle: 'l') - dossier.project_champs_public.first.becomes!(Champs::IbanChamp).save! - - subject - end + before { subject } it { expect(response).to have_http_status(:success) } end @@ -998,9 +990,7 @@ end context 'when the champ is a phone number' do - let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :phone }]) } - let!(:dossier) { create(:dossier, :en_construction, user:, procedure:) } - let(:first_champ) { dossier.project_champs_public.first } + let(:types_de_champ_public) { [{ type: :phone }] } let(:now) { Time.zone.parse('01/01/2100') } let(:submit_payload) do diff --git a/spec/views/shared/dossiers/_edit.html.haml_spec.rb b/spec/views/shared/dossiers/_edit.html.haml_spec.rb index efccf5c7d25..d2222c027e1 100644 --- a/spec/views/shared/dossiers/_edit.html.haml_spec.rb +++ b/spec/views/shared/dossiers/_edit.html.haml_spec.rb @@ -6,10 +6,11 @@ allow(view).to receive(:administrateur_signed_in?).and_return(false) end - subject { render 'shared/dossiers/edit', dossier: dossier, apercu: false } + subject { render 'shared/dossiers/edit', dossier:, dossier_for_editing:, apercu: false } let(:procedure) { create(:procedure, types_de_champ_public:) } let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:dossier_for_editing) { dossier } context 'when there are some champs' do let(:type_de_champ_header_section) { procedure.draft_types_de_champ_public.find(&:header_section?) } @@ -116,6 +117,7 @@ context 'when dossier is en construction' do let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure:) } + let(:dossier_for_editing) { dossier.owner_editing_fork } it 'can delete a piece justificative' do expect(subject).to have_selector("[title='Supprimer le fichier #{champ.piece_justificative_file.attachments[0].filename}']")