Skip to content

Commit

Permalink
feat(dossier): enable dossier update by stream
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Dec 19, 2024
1 parent a5aa2d8 commit 1ac5866
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def champ?

def champ
@champ ||= if champ?
record.dossier.with_stream(record.dossier.stream_for_update)
record.dossier.champ_for_update(record.type_de_champ, row_id: record.row_id, updated_by: current_user.email)
end
end
Expand Down
1 change: 1 addition & 0 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_stream(dossier.stream_for_update)
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 Down
47 changes: 38 additions & 9 deletions app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class DossiersController < UserController
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_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 @@ -252,32 +253,47 @@ def extend_conservation_and_restore

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

if update_with_stream?

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/users/dossiers_controller.rb#L257

Added line #L257 was not covered by tests
@dossier_for_editing = dossier
else
# TODO remove when all forks are gone
@dossier_for_editing = dossier.owner_editing_fork
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

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/users/dossiers_controller.rb#L267-L268

Added lines #L267 - L268 were not covered by tests

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)
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)
else
dossier.merge_stream(Champ::USER_DRAFT_STREAM)
end

dossier_en_construction.submit_en_construction!
redirect_to dossier_path(dossier_en_construction)

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/users/dossiers_controller.rb#L284-L285

Added lines #L284 - L285 were not covered by tests
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 = dossier_with_champs(pj_template: false)
update_dossier_and_compute_errors

Expand Down Expand Up @@ -529,6 +545,18 @@ def set_dossier_as_editing_fork
redirect_to dossier_path(dossier)
end

def set_dossier_stream
dossier.with_stream(Champ::USER_DRAFT_STREAM)

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/users/dossiers_controller.rb#L549

Added line #L549 was not covered by tests
end

def update_with_stream?
dossier.update_with_stream?

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/users/dossiers_controller.rb#L553

Added line #L553 was not covered by tests
end

def update_with_fork?
dossier.with_editing_fork?

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

View check run for this annotation

Codecov / codecov/patch

app/controllers/users/dossiers_controller.rb#L557

Added line #L557 was not covered by tests
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?)
Expand All @@ -554,6 +582,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
12 changes: 12 additions & 0 deletions app/models/concerns/dossier_champs_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ def history
champs_in_revision.filter(&:history_stream?)

Check warning on line 237 in app/models/concerns/dossier_champs_concern.rb

View check run for this annotation

Codecov / codecov/patch

app/models/concerns/dossier_champs_concern.rb#L237

Added line #L237 was not covered by tests
end

def update_with_stream?
en_construction? && procedure.feature_enabled?(:user_draft_stream) && !with_editing_fork?
end

def stream_for_update
if update_with_stream?
Champ::USER_DRAFT_STREAM
else
Champ::MAIN_STREAM
end
end

private

def merge_user_draft_stream
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/dossier_state_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def after_commit_passer_en_instruction(h)
NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers?
end

# TODO remove when all forks are gone
editing_forks.each(&:destroy_editing_fork!)
end

Expand Down Expand Up @@ -334,6 +335,7 @@ def clean_champs_after_submit!
remove_discarded_rows!
remove_not_visible_rows!
remove_not_visible_or_empty_champs!
# TODO remove when all forks are gone
editing_forks.each(&:destroy_editing_fork!)
end

Expand Down
Loading

0 comments on commit 1ac5866

Please sign in to comment.