Skip to content

Commit

Permalink
Merge pull request #11068 from tchak/dossier-en-construction-user-stream
Browse files Browse the repository at this point in the history
ETQ Usager, je voudrais modifier un dossier en construction via les streams
  • Loading branch information
tchak authored Feb 20, 2025
2 parents ef69241 + 0b07635 commit 5992072
Show file tree
Hide file tree
Showing 33 changed files with 934 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%span.autosave-explanation-text
- if annotation?
= t('.annotations.explanation')
- elsif dossier.editing_fork?
- elsif dossier.en_construction_for_editor?
= t('.en_construction.explanation')
- if dossier.can_passer_en_construction?
= t('.en_construction.submit_them')
Expand All @@ -16,7 +16,7 @@
%span.fr-icon-success-fill.fr-mr-1v{ 'aria-hidden': 'true' }
- if annotation?
= t('.annotations.confirmation_html')
- elsif dossier.editing_fork?
- elsif dossier.en_construction_for_editor?
= t('.en_construction.confirmation_html')
- else
= t('.brouillon.confirmation_html')
Expand All @@ -27,7 +27,7 @@
%span.fr-icon-warning-fill.fr-mr-1v{ 'aria-hidden': 'true' }
- if annotation?
= t('.annotations.error_html')
- elsif dossier.editing_fork?
- elsif dossier.en_construction_for_editor?
= t('.en_construction.error_html')
- else
= t('.brouillon.error_html')
Expand Down
8 changes: 5 additions & 3 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?, :can_transition_to_en_construction?, :forked_with_changes?, to: :@dossier
delegate :can_passer_en_construction?, :can_transition_to_en_construction?, :user_buffer_changes?, to: :@dossier

def initialize(dossier:, annotation:)
@dossier = dossier
Expand All @@ -27,7 +27,7 @@ def can_submit_draft?
end

def can_submit_en_construction?
forked_with_changes? && owner?
owner? && user_buffer_changes?
end

def submit_button_label
Expand All @@ -41,8 +41,10 @@ def submit_button_label
def submit_button_path
if can_submit_draft?
brouillon_dossier_path(@dossier)
else
elsif @dossier.editing_fork?
modifier_dossier_path(@dossier.editing_fork_origin)
else
modifier_dossier_path(@dossier)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ class Dossiers::EnConstructionNotSubmittedComponent < ApplicationComponent
def initialize(dossier:, user:)
@dossier = dossier
@user = user

@fork = @dossier.find_editing_fork(user, rebase: false)
end

def render?
@fork&.forked_with_changes?
@dossier.user_buffer_changes?
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- if @champ.mandatory?
= render EditableChamp::AsteriskMandatoryComponent.new

- if @champ.forked_with_changes?
- if @champ.user_buffer_changes?
%span.updated-at.highlighted
= t('.changes_to_save')
- elsif @champ.updated_at.present? && @seen_at.present?
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def destroy

if champ?
@attachment = champ.piece_justificative_file.find { _1.blob.id == @blob.id }
@attachment&.purge_later
if @attachment.present?
@attachment.purge_later
champ.update_timestamps
end
champ.piece_justificative_file.reload
else
@attachment.purge_later
Expand All @@ -50,6 +53,7 @@ def champ?

def champ
@champ ||= if champ?
record.dossier.with_update_stream(current_user)
record.dossier.champ_for_update(record.type_de_champ, row_id: record.row_id, updated_by: current_user.email)
end
end
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/champs/carte_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def create
geo_area = @champ.geo_areas.build(source: params_source, properties: {})

if save_feature(geo_area, create_params_feature)
@champ.update_timestamps
FetchCadastreRealGeometryJob.perform_later(geo_area) if geo_area.cadastre?
render json: { feature: geo_area.to_feature }, status: :created
else
Expand All @@ -28,6 +29,7 @@ def update
geo_area = @champ.geo_areas.find(params[:id])

if save_feature(geo_area, update_params_feature)
@champ.update_timestamps
FetchCadastreRealGeometryJob.perform_later(geo_area) if geo_area.cadastre?
head :no_content
else
Expand All @@ -37,7 +39,7 @@ def update

def destroy
@champ.geo_areas.find(params[:id]).destroy!
propagate_touch_champs_changed
@champ.update_timestamps

head :no_content
end
Expand Down Expand Up @@ -81,9 +83,6 @@ def save_feature(geo_area, feature)
if feature[:properties]
geo_area.properties.merge!(feature[:properties])
end
if geo_area.save
propagate_touch_champs_changed
true
end
geo_area.save
end
end
6 changes: 1 addition & 5 deletions app/controllers/champs/champ_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Champs::ChampController < ApplicationController

def find_champ
dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id])
dossier.with_update_stream(current_user)
type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id])
if type_de_champ.repetition?
dossier.project_champ(type_de_champ)
Expand All @@ -23,9 +24,4 @@ def params_row_id
def set_champ
@champ = find_champ
end

def propagate_touch_champs_changed
@champ.touch
@champ.dossier.touch_champs_changed([:last_champ_updated_at])
end
end
2 changes: 1 addition & 1 deletion app/controllers/champs/piece_justificative_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def attach_piece_justificative
end

if save_succeed && dossier.brouillon?
dossier.touch_champs_changed([:last_champ_updated_at, :last_champ_piece_jointe_updated_at])
@champ.update_timestamps
end

save_succeed
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/champs/repetition_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ def add
@row_id = @champ.add_row(updated_by: current_user.email)
@first_champ_id = @champ.focusable_input_id
@row_number = @row_id.nil? ? 0 : @champ.row_ids.find_index(@row_id) + 1
@champ.dossier.touch_champs_changed([:last_champ_updated_at])
@champ.update_timestamps
end

def remove
@champ.remove_row(params[:row_id], updated_by: current_user.email)
@to_remove = "safe-row-selector-#{params[:row_id]}"
@to_focus = @champ.focusable_input_id || helpers.dom_id(@champ, :create_repetition)
@champ.dossier.touch_champs_changed([:last_champ_updated_at])
@champ.update_timestamps
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/champs/rna_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def show
unless @champ.fetch_association!(rna)
@error = @champ.association_fetch_error_key
end
@champ.dossier.touch_champs_changed([:last_champ_updated_at])
@champ.update_timestamps
end
end
2 changes: 1 addition & 1 deletion app/controllers/champs/siret_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def show
# Anyway it would be clear when updating the value without validation
@champ.validate(params[:validate].to_sym) if params[:validate]

@champ.dossier.touch_champs_changed([:last_champ_updated_at])
@champ.update_timestamps
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/turbo_champs_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module TurboChampsConcern

def champs_to_turbo_update(params, champs)
to_update = champs.filter { _1.public_id.in?(params.keys) }
.filter { _1.refresh_after_update? || _1.forked_with_changes? }
.filter { _1.refresh_after_update? || _1.user_buffer_changes? }

to_show, to_hide = champs.filter(&:conditional?)
.partition(&:visible?)
Expand Down
14 changes: 9 additions & 5 deletions app/controllers/instructeurs/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,17 @@ def create_avis
end

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.touch(:last_champ_private_updated_at)
public_id, annotation_attributes = champs_private_attributes_params.to_h.first
annotation = dossier.private_champ_for_update(public_id, updated_by: current_user.email)
annotation.assign_attributes(annotation_attributes)
annotation_changed = annotation.changed_for_autosave?

if annotation.save(context: :champs_private_value) && annotation_changed
annotation.update_timestamps
dossier.index_search_terms_later
end

dossier.save(context: :champs_private_value)
dossier.index_search_terms_later
dossier.validate(context: :champs_private_value)

respond_to do |format|
format.turbo_stream do
Expand Down
69 changes: 51 additions & 18 deletions app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class DossiersController < UserController
before_action :ensure_dossier_can_be_viewed, only: [:show]
before_action :ensure_editing_brouillon, only: [:brouillon]
before_action :forbid_closed_submission!, only: [:submit_brouillon]
before_action :set_dossier_as_editing_fork, only: [:submit_en_construction]
before_action :set_dossier_as_editing_fork, only: [:submit_en_construction], if: :update_with_fork?
before_action :set_dossier_stream, only: [:modifier, :update, :submit_en_construction, :champ], if: :update_with_stream?
before_action :show_demarche_en_test_banner
before_action :store_user_location!, only: :new

Expand Down Expand Up @@ -253,34 +254,51 @@ def extend_conservation_and_restore

def modifier
@dossier = dossier_with_champs
@dossier_for_editing = dossier.owner_editing_fork

if update_with_stream?
@dossier_for_editing = dossier
else
# TODO remove when all forks are gone
@dossier_for_editing = dossier.owner_editing_fork
DossierPreloader.load_one(@dossier_for_editing)
end
end

def submit_en_construction
@dossier = dossier_with_champs(pj_template: false)
editing_fork_origin = @dossier.editing_fork_origin
editing_fork_origin = dossier.editing_fork_origin
dossier_en_construction = editing_fork_origin || dossier

if cast_bool(params.dig(:dossier, :pending_correction))
editing_fork_origin.resolve_pending_correction
dossier_en_construction.resolve_pending_correction
end

submit_dossier_and_compute_errors

if dossier.errors.blank? && dossier.can_passer_en_construction?
editing_fork_origin.merge_fork(dossier)
# merge_fork do a `reload`, the preloader is used to reload the whole tree
editing_fork_origin = DossierPreloader.load_one(editing_fork_origin)
editing_fork_origin.submit_en_construction!
redirect_to dossier_path(editing_fork_origin)
if editing_fork_origin.present?
# TODO remove when all forks are gone
editing_fork_origin.merge_fork(dossier)
# merge_fork do a `reload`, the preloader is used to reload the whole tree
DossierPreloader.load_one(editing_fork_origin)
else
dossier.merge_user_buffer_stream!
end

dossier_en_construction.submit_en_construction!
redirect_to dossier_path(dossier_en_construction)
else
@dossier_for_editing = dossier
@dossier = editing_fork_origin
if editing_fork_origin.present?
@dossier = editing_fork_origin
end

render :modifier
end
end

def update
@dossier = dossier.en_construction? ? dossier.find_editing_fork(dossier.user) : dossier
@dossier = update_with_fork? ? dossier.find_editing_fork(dossier.user) : dossier
@dossier = dossier_with_champs(pj_template: false)
update_dossier_and_compute_errors

Expand Down Expand Up @@ -538,23 +556,37 @@ def set_dossier_as_editing_fork
redirect_to dossier_path(dossier)
end

def set_dossier_stream
dossier.with_update_stream(current_user)
end

def update_with_stream?
dossier.update_with_stream?
end

def update_with_fork?
dossier.update_with_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?)
public_id, champ_attributes = champs_public_attributes_params.to_h.first
champ = dossier.public_champ_for_update(public_id, updated_by: current_user.email)
champ.assign_attributes(champ_attributes)
champ_changed = champ.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 dossier.brouillon? && updated_champs.present?
dossier.touch_champs_changed([:last_champ_updated_at])
if updated_champs.any?(&:used_by_routing_rules?)
if Dossier.no_touching { champ.save }
if dossier.brouillon? && champ_changed
champ.update_timestamps
if champ.used_by_routing_rules?
@update_contact_information = true
RoutingEngine.compute(dossier)
end
end

if params[:validate].present?
dossier.valid?(:champs_public_value)
dossier.validate(:champs_public_value)
end
end
end
Expand All @@ -563,6 +595,7 @@ def submit_dossier_and_compute_errors
dossier.validate(:champs_public_value)
dossier.check_mandatory_and_visible_champs

# TODO remove when all forks are gone
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|
Expand Down
Loading

0 comments on commit 5992072

Please sign in to comment.