From 68c15ba99fbbfc1a0e8734dcb35dd1cabd57de8c Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 13 May 2024 16:53:33 +0200 Subject: [PATCH] feat(champ): add updated_by column --- app/controllers/champs/champ_controller.rb | 2 +- .../instructeurs/dossiers_controller.rb | 2 +- app/controllers/users/dossiers_controller.rb | 2 +- .../mutations/dossier_modifier_annotation.rb | 2 +- app/models/concerns/dossier_champs_concern.rb | 17 +++++++++-------- .../prefill_repetition_type_de_champ.rb | 2 +- .../20240513140508_add_updated_by_to_champs.rb | 5 +++++ db/schema.rb | 1 + .../piece_justificative_controller_spec.rb | 3 +-- .../concerns/dossier_champs_concern_spec.rb | 8 ++++---- 10 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20240513140508_add_updated_by_to_champs.rb diff --git a/app/controllers/champs/champ_controller.rb b/app/controllers/champs/champ_controller.rb index 73ecb3044..adf03a778 100644 --- a/app/controllers/champs/champ_controller.rb +++ b/app/controllers/champs/champ_controller.rb @@ -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 diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 7657f9a48..3fa84c917 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -274,7 +274,7 @@ module Instructeurs end def update_annotations - dossier_with_champs.update_champs_attributes(champs_private_attributes_params, :private) + dossier_with_champs.update_champs_attributes(champs_private_attributes_params, :private, updated_by: current_user.email) if dossier.champs.any?(&:changed_for_autosave?) dossier.last_champ_private_updated_at = Time.zone.now end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 592f16f2c..acdfd1332 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -551,7 +551,7 @@ module Users 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 diff --git a/app/graphql/mutations/dossier_modifier_annotation.rb b/app/graphql/mutations/dossier_modifier_annotation.rb index 30dbd6f7e..de467fa00 100644 --- a/app/graphql/mutations/dossier_modifier_annotation.rb +++ b/app/graphql/mutations/dossier_modifier_annotation.rb @@ -44,7 +44,7 @@ module Mutations .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 diff --git a/app/models/concerns/dossier_champs_concern.rb b/app/models/concerns/dossier_champs_concern.rb index 40c0692c8..a5c05a5d0 100644 --- a/app/models/concerns/dossier_champs_concern.rb +++ b/app/models/concerns/dossier_champs_concern.rb @@ -55,18 +55,18 @@ module DossierChampsConcern .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:) @@ -87,13 +87,13 @@ module DossierChampsConcern end 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 @@ -101,6 +101,7 @@ module DossierChampsConcern .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] diff --git a/app/models/types_de_champ/prefill_repetition_type_de_champ.rb b/app/models/types_de_champ/prefill_repetition_type_de_champ.rb index e90457e4b..a61e7119d 100644 --- a/app/models/types_de_champ/prefill_repetition_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_repetition_type_de_champ.rb @@ -64,7 +64,7 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh 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 diff --git a/db/migrate/20240513140508_add_updated_by_to_champs.rb b/db/migrate/20240513140508_add_updated_by_to_champs.rb new file mode 100644 index 000000000..d7621ab6b --- /dev/null +++ b/db/migrate/20240513140508_add_updated_by_to_champs.rb @@ -0,0 +1,5 @@ +class AddUpdatedByToChamps < ActiveRecord::Migration[7.0] + def change + add_column :champs, :updated_by, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 74b41f6c7..6f7fcfe7c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -263,6 +263,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_27_090508) do t.string "type" t.integer "type_de_champ_id" t.datetime "updated_at", precision: nil + t.text "updated_by" t.string "value" t.jsonb "value_json" t.index ["dossier_id"], name: "index_champs_on_dossier_id" diff --git a/spec/controllers/champs/piece_justificative_controller_spec.rb b/spec/controllers/champs/piece_justificative_controller_spec.rb index 0c0eba1e1..d63c4b77a 100644 --- a/spec/controllers/champs/piece_justificative_controller_spec.rb +++ b/spec/controllers/champs/piece_justificative_controller_spec.rb @@ -10,7 +10,6 @@ describe Champs::PieceJustificativeController, type: :controller do subject do put :update, params: { - position: '1', dossier_id: champ.dossier_id, stable_id: champ.stable_id, blob_signed_id: file @@ -49,7 +48,7 @@ describe Champs::PieceJustificativeController, type: :controller do # 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 diff --git a/spec/models/concerns/dossier_champs_concern_spec.rb b/spec/models/concerns/dossier_champs_concern_spec.rb index a9e58ca77..42d22a340 100644 --- a/spec/models/concerns/dossier_champs_concern_spec.rb +++ b/spec/models/concerns/dossier_champs_concern_spec.rb @@ -124,7 +124,7 @@ RSpec.describe DossierChampsConcern do 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 @@ -165,7 +165,7 @@ RSpec.describe DossierChampsConcern do 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 @@ -191,7 +191,7 @@ RSpec.describe DossierChampsConcern do 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 @@ -229,7 +229,7 @@ RSpec.describe DossierChampsConcern do 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