Skip to content

Commit

Permalink
feat(dossier): update champ change timestamps only when brouillon or …
Browse files Browse the repository at this point in the history
…merging
  • Loading branch information
tchak committed Feb 12, 2025
1 parent bc07526 commit 2c36318
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 39 deletions.
5 changes: 4 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.touch_changed
end
champ.piece_justificative_file.reload
else
@attachment.purge_later
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.touch_changed
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.touch_changed
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.touch_changed

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
5 changes: 0 additions & 5 deletions app/controllers/champs/champ_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,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.touch_changed
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.touch_changed
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.touch_changed
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.touch_changed

Check warning on line 11 in app/controllers/champs/rna_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/champs/rna_controller.rb#L11

Added line #L11 was not covered by tests
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 @@ -10,6 +10,6 @@ def show
else
@siret = @champ.etablissement_fetch_error_key
end
@champ.dossier.touch_champs_changed([:last_champ_updated_at])
@champ.touch_changed
end
end
2 changes: 1 addition & 1 deletion app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def update_dossier_and_compute_errors
if dossier.brouillon?
if dossier.save
if updated_champs.present?
dossier.touch_champs_changed([:last_champ_updated_at])
updated_champs.each(&:touch_changed)
if updated_champs.any?(&:used_by_routing_rules?)
@update_contact_information = true
RoutingEngine.compute(dossier)
Expand Down
21 changes: 21 additions & 0 deletions app/models/champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,27 @@ def clone_value_from(champ)
save!
end

def touch_changed
return unless private? || dossier.brouillon?

updated_at = Time.zone.now
attributes = { updated_at: }
update_columns(attributes) if persisted?

if piece_justificative_or_titre_identite?
attributes[:last_champ_piece_jointe_updated_at] = updated_at
end

if private?
attributes[:last_champ_private_updated_at] = updated_at
else
attributes[:last_champ_updated_at] = updated_at
attributes[:brouillon_close_to_expiration_notice_sent_at] = nil
end

dossier.update_columns(attributes) if attributes.present?
end

class NotImplemented < ::StandardError
def initialize(method)
super(":#{method} not implemented")
Expand Down
9 changes: 3 additions & 6 deletions app/models/concerns/dossier_champs_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,12 @@ def merge_user_buffer_stream!

now = Time.zone.now
history_stream = "#{Champ::HISTORY_STREAM}#{now}"
changed_champs = champs.filter { _1.id.in?(buffer_champ_ids) }

transaction do
champs.where(id: changed_main_champ_ids, stream: Champ::MAIN_STREAM).update_all(stream: history_stream)
champs.where(id: buffer_champ_ids, stream: Champ::USER_BUFFER_STREAM).update_all(stream: Champ::MAIN_STREAM, updated_at: now)

if Champ.exists?(id: buffer_champ_ids, type: ['Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp'])
touch_champs_changed([:last_champ_updated_at, :last_champ_piece_jointe_updated_at])
else
touch_champs_changed([:last_champ_updated_at])
end
touch_champs_changed(changed_champs)
end

# update loaded champ instances
Expand Down
6 changes: 2 additions & 4 deletions app/models/concerns/dossier_clone_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ def merge_fork(editing_fork)
diff = make_diff(editing_fork)
apply_diff(diff)

attributes_to_touch = [:last_champ_updated_at]
attributes_to_touch << :last_champ_piece_jointe_updated_at if diff[:updated].any? { |c| c.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) }

touch_champs_changed(attributes_to_touch)
changed_champs = diff.values.flatten
touch_champs_changed(changed_champs)
end
reload
index_search_terms_later
Expand Down
13 changes: 8 additions & 5 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,14 @@ def termine_and_accuse_lecture?
procedure.accuse_lecture? && termine?
end

def touch_champs_changed(attributes)
now = Time.zone.now
update_columns(attributes.each_with_object({ brouillon_close_to_expiration_notice_sent_at: nil, updated_at: now }) do |attribute, hash|
hash[attribute] = now
end)
def touch_champs_changed(changed_champs)
return if changed_champs.empty?
updated_at = Time.zone.now
attributes = { updated_at:, last_champ_updated_at: updated_at }
if changed_champs.any?(&:piece_justificative_or_titre_identite?)
attributes[:last_champ_piece_jointe_updated_at] = updated_at
end
update_columns(attributes)
end

private
Expand Down
22 changes: 15 additions & 7 deletions spec/models/dossier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2427,21 +2427,29 @@
end

describe '#touch_champs_changed' do
let(:dossier) { create(:dossier, brouillon_close_to_expiration_notice_sent_at: 10.days.ago) }
let(:procedure) { create(:procedure, types_de_champ_public: [{}, { type: :piece_justificative }, { type: :titre_identite }]) }
let(:dossier) { create(:dossier, procedure:, brouillon_close_to_expiration_notice_sent_at: 10.days.ago) }
let(:changed_champs) { dossier.champs.filter(&:text?) }

subject { -> { dossier.touch_champs_changed(attributes) } }

let(:attributes) { [:last_champ_updated_at] }
subject { -> { dossier.touch_champs_changed(changed_champs) } }

it { is_expected.to change(dossier, :last_champ_updated_at) }
it { is_expected.to change(dossier, :updated_at) }

context 'when there is piece justificative' do
let(:changed_champs) { dossier.champs.filter(&:piece_justificative?) }

it { is_expected.to change(dossier, :brouillon_close_to_expiration_notice_sent_at).to(nil) }
it { is_expected.to change(dossier, :last_champ_updated_at) }
it { is_expected.to change(dossier, :last_champ_piece_jointe_updated_at) }
it { is_expected.to change(dossier, :updated_at) }
end

context 'when there is two attributes' do
let(:attributes) { [:last_champ_updated_at, :last_champ_piece_jointe_updated_at] }
context 'when there is titre identite' do
let(:changed_champs) { dossier.champs.filter(&:titre_identite?) }

it { is_expected.to change(dossier, :last_champ_updated_at) }
it { is_expected.to change(dossier, :last_champ_piece_jointe_updated_at) }
it { is_expected.to change(dossier, :updated_at) }
end
end

Expand Down

0 comments on commit 2c36318

Please sign in to comment.