Merge pull request #10415 from tchak/champs-updated-by

feat(champ): add updated_by column
This commit is contained in:
Paul Chavard 2024-06-03 19:58:58 +00:00 committed by GitHub
commit 395967e0a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 25 additions and 19 deletions

View file

@ -7,7 +7,7 @@ class Champs::ChampController < ApplicationController
def find_champ def find_champ
dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id]) 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]) 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 end
def params_row_id def params_row_id

View file

@ -274,7 +274,7 @@ module Instructeurs
end end
def update_annotations 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?) if dossier.champs.any?(&:changed_for_autosave?)
dossier.last_champ_private_updated_at = Time.zone.now dossier.last_champ_private_updated_at = Time.zone.now
end end

View file

@ -551,7 +551,7 @@ module Users
end end
def update_dossier_and_compute_errors 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?) if @dossier.champs.any?(&:changed_for_autosave?)
@dossier.last_champ_updated_at = Time.zone.now @dossier.last_champ_updated_at = Time.zone.now
end end

View file

@ -44,7 +44,7 @@ module Mutations
.find_by(type_champ: annotation_type_champ, stable_id:) .find_by(type_champ: annotation_type_champ, stable_id:)
return nil if type_de_champ.nil? 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 end
def annotation_type_champ def annotation_type_champ

View file

@ -55,18 +55,18 @@ module DossierChampsConcern
.types_de_champ .types_de_champ
.filter { _1.stable_id.in?(stable_ids) } .filter { _1.stable_id.in?(stable_ids) }
.filter { !revision.child?(_1) } .filter { !revision.child?(_1) }
.map { champ_for_update(_1, nil) } .map { champ_for_update(_1, nil, updated_by: nil) }
end end
def champ_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) champ, attributes = champ_with_attributes_for_update(type_de_champ, row_id, updated_by:)
champ.assign_attributes(attributes) champ.assign_attributes(attributes)
champ champ
end 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| 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 end
assign_attributes(champs_attributes:) assign_attributes(champs_attributes:)
@ -87,13 +87,13 @@ module DossierChampsConcern
end end
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('-') stable_id, row_id = public_id.split('-')
type_de_champ = find_type_de_champ_by_stable_id(stable_id, scope) 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 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 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` # 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 champ = champs
@ -101,6 +101,7 @@ module DossierChampsConcern
.find_or_create_by!(stable_id: type_de_champ.stable_id, row_id:) .find_or_create_by!(stable_id: type_de_champ.stable_id, row_id:)
attributes[:id] = champ.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 # Needed when a revision change the champ type in this case, we reset the champ data
if champ.type != attributes[:type] if champ.type != attributes[:type]

View file

@ -64,7 +64,7 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
type_de_champ = revision.types_de_champ.find { _1.stable_id == stable_id } type_de_champ = revision.types_de_champ.find { _1.stable_id == stable_id }
next unless type_de_champ 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) TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ, revision).to_assignable_attributes(subchamp, value)
end.compact end.compact
end end

View file

@ -0,0 +1,5 @@
class AddUpdatedByToChamps < ActiveRecord::Migration[7.0]
def change
add_column :champs, :updated_by, :text
end
end

View file

@ -263,6 +263,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_27_090508) do
t.string "type" t.string "type"
t.integer "type_de_champ_id" t.integer "type_de_champ_id"
t.datetime "updated_at", precision: nil t.datetime "updated_at", precision: nil
t.text "updated_by"
t.string "value" t.string "value"
t.jsonb "value_json" t.jsonb "value_json"
t.index ["dossier_id"], name: "index_champs_on_dossier_id" t.index ["dossier_id"], name: "index_champs_on_dossier_id"

View file

@ -10,7 +10,6 @@ describe Champs::PieceJustificativeController, type: :controller do
subject do subject do
put :update, params: { put :update, params: {
position: '1',
dossier_id: champ.dossier_id, dossier_id: champ.dossier_id,
stable_id: champ.stable_id, stable_id: champ.stable_id,
blob_signed_id: file blob_signed_id: file
@ -49,7 +48,7 @@ describe Champs::PieceJustificativeController, type: :controller do
# See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926 # See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926
before do before do
champ 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) expect_any_instance_of(Champs::PieceJustificativeChamp).to receive(:errors)
.and_return(double(full_messages: ['La pièce justificative nest pas dun type accepté'])) .and_return(double(full_messages: ['La pièce justificative nest pas dun type accepté']))
end end

View file

@ -124,7 +124,7 @@ RSpec.describe DossierChampsConcern do
let(:row_id) { nil } let(:row_id) { nil }
context "public champ" do 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 { it {
expect(subject.persisted?).to be_truthy expect(subject.persisted?).to be_truthy
@ -165,7 +165,7 @@ RSpec.describe DossierChampsConcern do
end end
context "private champ" do 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 { it {
expect(subject.persisted?).to be_truthy 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_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) } 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 { it {
subject 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) } 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 { it {
subject subject