hotfix(ProcedurePresentation): backfill invalid procedure presentation private tabel name for type de champs private

This commit is contained in:
mfo 2024-08-20 17:50:11 +02:00
parent e4d460965f
commit 3662e38366
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
module Maintenance
# a previous commit : https://github.com/demarches-simplifiees/demarches-simplifiees.fr/pull/10625/commits/305b8c13c75a711a85521d0b19659293d8d92805
# the previous brokes a naming convention on ProcedurePresentation.filters|displayed_fields|sort
# this commit
# it adjusts live data to fit new convention and avoid validation error
class HotfixFormerProcedurePresentationNamingTask < MaintenanceTasks::Task
def collection
ProcedurePresentation.all
end
def process(element)
element.displayed_fields = element.displayed_fields.map do |displayed_field|
if displayed_field['table'] == 'type_de_champ_private'
displayed_field['table'] = 'type_de_champ'
end
displayed_field
end
element.filters.map do |status, filters_by_status|
element.filters[status] = filters_by_status.map do |filter_by_status|
if filter_by_status['table'] == 'type_de_champ_private'
filter_by_status['table'] = 'type_de_champ'
end
filter_by_status
end
end
if element.sort['table'] == 'type_de_champ_private'
element.sort['table'] = 'type_de_champ'
end
element.save!
rescue ActiveRecord::RecordInvalid
# do nothing, former invalid ProcedurePresentation still exist
# cf: La validation a échoué : Le champ « Displayed fields » etablissement.entreprise_siren nest pas une colonne permise
end
end
end

View file

@ -0,0 +1,31 @@
# frozen_string_literal: true
require "rails_helper"
# this commit : https://github.com/demarches-simplifiees/demarches-simplifiees.fr/pull/10625/commits/305b8c13c75a711a85521d0b19659293d8d92805
# it brokes a naming convention on ProcedurePresentation.filters|displayed_fields|sort
# we adjust live data to fit new convention
module Maintenance
RSpec.describe HotfixFormerProcedurePresentationNamingTask do
let(:procedure) { create(:procedure, types_de_champ_private: [{ type: :text }]) }
let(:groupe_instructeur) { create(:groupe_instructeur, procedure: procedure, instructeurs: [build(:instructeur)]) }
let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: groupe_instructeur.instructeurs.first) }
let(:procedure_presentation) { create(:procedure_presentation, procedure: procedure, assign_to: assign_to) }
describe "#process" do
subject(:process) { described_class.process(procedure_presentation) }
it "fix table naming" do
stable_id = procedure.draft_revision.types_de_champ.first.stable_id.to_s
procedure_presentation.update_column(:displayed_fields, [{ table: 'type_de_champ_private', column: stable_id }])
procedure_presentation.update_column(:filters, "a-suivre" => [{ table: 'type_de_champ_private', column: stable_id }])
procedure_presentation.update_column(:sort, table: 'type_de_champ_private', column: stable_id, order: 'asc')
subject
procedure_presentation.reload
expect(procedure_presentation.displayed_fields.map { _1['table'] }).to eq(['type_de_champ'])
expect(procedure_presentation.filters.flat_map { |_, filters| filters.map { _1['table'] } }).to eq(['type_de_champ'])
expect(procedure_presentation.sort['table']).to eq('type_de_champ')
end
end
end
end