Skip to content

Commit

Permalink
Merge commit '395967e0a1a454446f100599ae1bf1d6df06c64b' into feature/…
Browse files Browse the repository at this point in the history
…import-2024-06-10

# Conflicts:
#	app/controllers/instructeurs/dossiers_controller.rb
#	app/models/concerns/dossier_champs_concern.rb
#	db/schema.rb
  • Loading branch information
maatinito committed Oct 15, 2024
2 parents 8e66cc9 + 395967e commit e4826c9
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/controllers/champs/champ_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Champs::ChampController < ApplicationController
def find_champ
dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id])
type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id])
dossier.champ_for_update(type_de_champ, params_row_id)
dossier.champ_for_update(type_de_champ, params_row_id, updated_by: current_user.email)
end

def params_row_id
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/instructeurs/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def create_avis
end

def update_annotations
dossier_with_champs.update_champs_attributes(remove_changes_forbidden_by_visa, :private)
dossier_with_champs.update_champs_attributes(remove_changes_forbidden_by_visa, :private, updated_by: current_user.email)
if dossier.champs.any?(&:changed_for_autosave?)
dossier.last_champ_private_updated_at = Time.zone.now
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def set_dossier_as_editing_fork
end

def update_dossier_and_compute_errors
@dossier.update_champs_attributes(champs_public_attributes_params, :public)
@dossier.update_champs_attributes(champs_public_attributes_params, :public, updated_by: current_user.email)
if @dossier.champs.any?(&:changed_for_autosave?)
@dossier.last_champ_updated_at = Time.zone.now
end
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/dossier_modifier_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def find_annotation(dossier, annotation_id)
.find_by(type_champ: annotation_type_champ, stable_id:)

return nil if type_de_champ.nil?
dossier.champ_for_update(type_de_champ, row_id)
dossier.champ_for_update(type_de_champ, row_id, updated_by: current_administrateur.email)
end

def annotation_type_champ
Expand Down
17 changes: 9 additions & 8 deletions app/models/concerns/dossier_champs_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ def champs_for_prefill(stable_ids)
.types_de_champ
.filter { _1.stable_id.in?(stable_ids) }
.filter { !revision.child?(_1) }
.map { champ_for_update(_1, nil) }
.map { champ_for_update(_1, nil, updated_by: nil) }
end

def champ_for_update(type_de_champ, row_id)
champ, attributes = champ_with_attributes_for_update(type_de_champ, row_id)
def champ_for_update(type_de_champ, row_id, updated_by:)
champ, attributes = champ_with_attributes_for_update(type_de_champ, row_id, updated_by:)
champ.assign_attributes(attributes)
champ
end

def update_champs_attributes(attributes, scope)
def update_champs_attributes(attributes, scope, updated_by:)
champs_attributes = attributes.to_h.map do |public_id, attributes|
champ_attributes_by_public_id(public_id, attributes, scope)
champ_attributes_by_public_id(public_id, attributes, scope, updated_by:)
end

assign_attributes(champs_attributes:)
Expand All @@ -87,20 +87,21 @@ def champs_by_public_id
@champs_by_public_id ||= champs.sort_by(&:id).index_by(&:public_id)
end

def champ_attributes_by_public_id(public_id, attributes, scope)
def champ_attributes_by_public_id(public_id, attributes, scope, updated_by:)
stable_id, row_id = public_id.split('-')
type_de_champ = find_type_de_champ_by_stable_id(stable_id, scope)
champ_with_attributes_for_update(type_de_champ, row_id).last.merge(attributes)
champ_with_attributes_for_update(type_de_champ, row_id, updated_by:).last.merge(attributes)
end

def champ_with_attributes_for_update(type_de_champ, row_id)
def champ_with_attributes_for_update(type_de_champ, row_id, updated_by:)
attributes = type_de_champ.params_for_champ
# TODO: Once we have the right index in place, we should change this to use `create_or_find_by` instead of `find_or_create_by`
champ = champs
.create_with(type_de_champ:, **attributes)
.find_or_create_by!(stable_id: type_de_champ.stable_id, row_id:)

attributes[:id] = champ.id
attributes[:updated_by] = updated_by

# Needed when a revision change the champ type in this case, we reset the champ data
if champ.type != attributes[:type]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def to_assignable_attributes
type_de_champ = revision.types_de_champ.find { _1.stable_id == stable_id }
next unless type_de_champ

subchamp = champ.dossier.champ_for_update(type_de_champ, row_id)
subchamp = champ.dossier.champ_for_update(type_de_champ, row_id, updated_by: nil)
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ, revision).to_assignable_attributes(subchamp, value)
end.compact
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240513140508_add_updated_by_to_champs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUpdatedByToChamps < ActiveRecord::Migration[7.0]
def change
add_column :champs, :updated_by, :text
end
end
4 changes: 4 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

ActiveRecord::Schema[7.0].define(version: 2024_06_24_133648) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_buffercache"
enable_extension "pg_stat_statements"
enable_extension "pgcrypto"
enable_extension "plpgsql"
enable_extension "postgis"
enable_extension "sslinfo"
enable_extension "unaccent"

create_table "action_text_rich_texts", force: :cascade do |t|
Expand Down Expand Up @@ -276,6 +279,7 @@
t.string "type"
t.integer "type_de_champ_id"
t.datetime "updated_at"
t.text "updated_by"
t.string "value"
t.jsonb "value_json"
t.index ["dossier_id"], name: "index_champs_on_dossier_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@

subject do
put :update, params: {
position: '1',
dossier_id: champ.dossier_id,
stable_id: champ.stable_id,
blob_signed_id: file
Expand Down Expand Up @@ -204,7 +203,7 @@
# See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926
before do
champ
expect_any_instance_of(Champs::PieceJustificativeChamp).to receive(:save).twice.and_return(false)
expect_any_instance_of(Champs::PieceJustificativeChamp).to receive(:save).and_return(false)
expect_any_instance_of(Champs::PieceJustificativeChamp).to receive(:errors)
.and_return(double(full_messages: ['La pièce justificative n’est pas d’un type accepté']))
end
Expand Down
8 changes: 4 additions & 4 deletions spec/models/concerns/dossier_champs_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
let(:row_id) { nil }

context "public champ" do
subject { dossier.champ_for_update(type_de_champ_public, row_id) }
subject { dossier.champ_for_update(type_de_champ_public, row_id, updated_by: dossier.user.email) }

it {
expect(subject.persisted?).to be_truthy
Expand Down Expand Up @@ -165,7 +165,7 @@
end

context "private champ" do
subject { dossier.champ_for_update(type_de_champ_private, row_id) }
subject { dossier.champ_for_update(type_de_champ_private, row_id, updated_by: dossier.user.email) }

it {
expect(subject.persisted?).to be_truthy
Expand All @@ -191,7 +191,7 @@
let(:champ_991) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(991), nil) }
let(:champ_994) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(994), row_id) }

subject { dossier.update_champs_attributes(attributes, :public) }
subject { dossier.update_champs_attributes(attributes, :public, updated_by: dossier.user.email) }

it {
subject
Expand Down Expand Up @@ -229,7 +229,7 @@

let(:annotation_995) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(995), nil) }

subject { dossier.update_champs_attributes(attributes, :private) }
subject { dossier.update_champs_attributes(attributes, :private, updated_by: dossier.user.email) }

it {
subject
Expand Down

0 comments on commit e4826c9

Please sign in to comment.