fix(dossier): remove champ duplicates
This commit is contained in:
parent
9dd1973e18
commit
dc5bfb8159
2 changed files with 44 additions and 0 deletions
|
@ -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)
|
||||
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
|
|
@ -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…
Add table
Add a link
Reference in a new issue