Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dossier): on fork merge, always remove previous champ version #11144

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/concerns/dossier_clone_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def apply_diff(diff)
added_champs.each { _1.update_column(:dossier_id, id) }

if updated_champs.present?
champs_index = filled_champs_public.index_by(&:public_id)
champs_index = champs.index_by(&:public_id)
updated_champs.each do |champ|
champs_index[champ.public_id]&.destroy!
champ.update_column(:dossier_id, id)
Expand Down
22 changes: 22 additions & 0 deletions app/tasks/maintenance/t20241216remove_non_unique_champs_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Maintenance
class T20241216removeNonUniqueChampsTask < MaintenanceTasks::Task
# Documentation: cette tâche supprime les champs en double dans un dossier

include RunnableOnDeployConcern
include StatementsHelpersConcern

def collection
Dossier.state_not_brouillon.includes(champs: true)

Check warning on line 11 in app/tasks/maintenance/t20241216remove_non_unique_champs_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/t20241216remove_non_unique_champs_task.rb#L11

Added line #L11 was not covered by tests
end

def process(dossier)
dossier.champs.filter { _1.row_id.nil? }.group_by(&:public_id).each do |_, champs|
if champs.size > 1
champs.sort_by(&:id)[1..].each(&:destroy)
end
end
end
end
end
21 changes: 13 additions & 8 deletions spec/models/concerns/dossier_clone_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,15 @@
}
let(:removed_champ) { dossier.champs.find { _1.stable_id == 99 } }
let(:updated_champ) { dossier.champs.find { _1.stable_id == 991 } }
let(:repetition_updated_champ) { champ_for_update(dossier.champs.find { _1.stable_id == 994 }) }
let(:forked_updated_champ) { champ_for_update(forked_dossier.champs.find { _1.stable_id == 991 }) }
let(:forked_repetition_updated_champ) { champ_for_update(forked_dossier.champs.find { _1.stable_id == 994 }) }

before do
dossier.champs.each do |champ|
champ.update(value: 'old value')
end
dossier.reload
procedure.draft_revision.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un nouveau champ text"
Expand All @@ -395,18 +399,19 @@
procedure.draft_revision.remove_type_de_champ(removed_champ.stable_id)
procedure.draft_revision.find_and_ensure_exclusive_use(updated_champ.stable_id).update(libelle: "Un nouveau libelle")
procedure.publish_revision!
end

subject {
added_champ.update(value: 'new value for added champ')
updated_champ.update(value: 'new value for updated champ')
added_repetition_champ.update(value: "new value in repetition champ")

forked_updated_champ.update(value: 'new value for updated champ')
forked_repetition_updated_champ.update(value: 'new value for updated champ in repetition')
updated_champ.update(type: 'Champs::TextareaChamp')
repetition_updated_champ.update(type: 'Champs::TextareaChamp')
dossier.reload
super()
}
forked_dossier.reload
end

it { expect { subject }.to change { dossier.filled_champs.size }.by(1) }
it { expect { subject }.to change { dossier.filled_champs.sort_by(&:created_at).map(&:to_s) }.from(['old value', 'old value', 'Non', 'old value', 'old value']).to(['new value for updated champ', 'Non', 'old value', 'old value', 'new value for added champ', 'new value in repetition champ']) }
it { expect { subject }.to change { dossier.filled_champs.size }.by(3) }
it { expect { subject }.to change { dossier.filled_champs.sort_by(&:created_at).map(&:to_s) }.from(['old value', 'Non', 'old value']).to(['new value for updated champ', 'Non', 'new value for updated champ in repetition', 'old value', 'new value for added champ', 'new value in repetition champ']) }

it "dossier after merge should be on last published revision" do
expect(dossier.revision_id).to eq(procedure.revisions.first.id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "rails_helper"

module Maintenance
RSpec.describe T20241216removeNonUniqueChampsTask do
describe "#process" do
subject(:process) { described_class.process(dossier) }
let(:procedure) { create(:procedure, types_de_champ_public: [{}]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:type_de_champ) { dossier.revision.types_de_champ_public.first }
let(:champ_id) { dossier.champs.first.id }

before {
dossier.champs.create(**type_de_champ.params_for_champ)
}

it { expect { subject }.to change { dossier.reload.project_champ(type_de_champ).id }.from(dossier.champs.last.id).to(champ_id) }
it { expect { subject }.to change { Champ.count }.by(-1) }
end
end
end
Loading