diff --git a/app/tasks/maintenance/hotfix_former_procedure_presentation_naming_task.rb b/app/tasks/maintenance/hotfix_former_procedure_presentation_naming_task.rb new file mode 100644 index 000000000..3d3a6f9af --- /dev/null +++ b/app/tasks/maintenance/hotfix_former_procedure_presentation_naming_task.rb @@ -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 n’est pas une colonne permise + end + end +end diff --git a/spec/tasks/maintenance/hotfix_former_procedure_presentation_naming_task_spec.rb b/spec/tasks/maintenance/hotfix_former_procedure_presentation_naming_task_spec.rb new file mode 100644 index 000000000..4fe8dd3ec --- /dev/null +++ b/spec/tasks/maintenance/hotfix_former_procedure_presentation_naming_task_spec.rb @@ -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