feat(admin): task removing type de champ from draft revision from a csv

This commit is contained in:
Colin Darie 2023-12-11 19:04:49 +01:00
parent 7618b8c549
commit 6a2e68b697
2 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,33 @@
# frozen_string_literal: true
module Maintenance
class DeleteDraftRevisionTypeDeChampsTask < MaintenanceTasks::Task
csv_collection
# See UpdateDraftRevisionTypeDeChampsTask for more information
# Just add delete_flag with "true" to effectively remove the type de champ from the draft.
def process(row)
return unless row["delete_flag"] == "true"
procedure_id = row["demarche_id"]
typed_id = row["id"]
Rails.logger.info { "#{self.class.name}: Processing #{row.inspect}" }
revision = Procedure.find(procedure_id).draft_revision
stable_id = revision.types_de_champ.find { _1.to_typed_id == typed_id }&.stable_id
fail "TypeDeChamp not found ! #{typed_id}" if stable_id.nil?
coordinate, type_de_champ = revision.coordinate_and_tdc(stable_id)
if coordinate&.used_by_routing_rules?
fail "#{typed_id} / #{type_de_champ.libelle} » est utilisé pour le routage, vous ne pouvez pas le supprimer."
end
revision.remove_type_de_champ(stable_id)
end
end
end

View file

@ -0,0 +1,67 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe DeleteDraftRevisionTypeDeChampsTask do
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
let(:types_de_champ_public) {
[
{ type: :text, libelle: "Text", stable_id: 11, mandatory: true },
{ type: :number, libelle: "Number", description: "Old desc", stable_id: 12 },
{
type: :repetition, libelle: "Bloc", stable_id: 13, children: [
{ type: :text, libelle: "RepText", stable_id: 131, description: "Remove me", mandatory: true },
{ type: :number, libelle: "RepNum", stable_id: 132 }
]
}
]
}
let(:csv) do
<<~CSV
demarche_id,id,new_libelle,new_description,new_required,new_position,delete_flag
#{procedure.id},#{find_by_stable_id(12).to_typed_id},[NEW] Number,[NEW] Number desc,true,0,
#{procedure.id},#{find_by_stable_id(13).to_typed_id},Bloc,[NEW] bloc desc,,1,
#{procedure.id},#{find_by_stable_id(132).to_typed_id},[NEW] RepNum,,true,2,true
#{procedure.id},#{find_by_stable_id(131).to_typed_id},[NEW] RepText,,,3,
#{procedure.id},#{find_by_stable_id(11).to_typed_id},[supp] Text,,,4,true
CSV
end
let(:rows) do
CSV.parse(csv, headers: true, skip_blanks: true).map(&:to_h)
end
describe "#process" do
subject(:process) do
rows.each { described_class.process(_1) }
procedure.reload
end
it "delete the flagged rows" do
process
tdc = find_by_stable_id(12)
expect(tdc).not_to be_nil
tdc = find_by_stable_id(13)
expect(tdc).not_to be_nil
tdc = find_by_stable_id(11)
expect(tdc).to be_nil
tdc = find_by_stable_id(131)
expect(tdc).not_to be_nil
expect(tdc.revision_type_de_champ.position).to eq(0) # reindexed
tdc = find_by_stable_id(132)
expect(tdc).to be_nil
end
end
def find_by_stable_id(stable_id)
procedure.draft_revision.types_de_champ.find { _1.stable_id == stable_id }
end
end
end