diff --git a/lib/tasks/deployment/20240920130741_migrate_procedure_presentation_to_columns.rake b/lib/tasks/deployment/20240920130741_migrate_procedure_presentation_to_columns.rake new file mode 100644 index 000000000..56236afe1 --- /dev/null +++ b/lib/tasks/deployment/20240920130741_migrate_procedure_presentation_to_columns.rake @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +namespace :after_party do + desc 'Deployment task: migrate_procedure_presentation_to_columns' + task migrate_procedure_presentation_to_columns: :environment do + total = ProcedurePresentation.count + + progress = ProgressReport.new(total) + + ProcedurePresentation.find_each do |presentation| + procedure_id = presentation.procedure.id + + presentation.displayed_columns = presentation.displayed_fields + .filter(&:present?) + .map { Column.new(**_1.deep_symbolize_keys.merge(procedure_id:)) } + .map(&:h_id) + + sort = presentation.sort + + presentation.sorted_column = { + 'order' => sort['order'], + 'id' => make_id(procedure_id, sort['table'], sort['column']) + } + + presentation.filters.each do |key, filters| + raw_columns = filters.map do + { + id: make_id(procedure_id, _1['table'], _1['column']), + filter: _1['value'] + } + end + + presentation.send("#{presentation.filters_name_for(key)}=", raw_columns) + end + + presentation.save!(validate: false) + progress.inc + end + + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end + + private + + def make_id(procedure_id, table, column) + { procedure_id:, column_id: "#{table}/#{column}" } + end +end diff --git a/spec/lib/tasks/deployment/20240920130741_migrate_procedure_presentation_to_columns.rake_spec.rb b/spec/lib/tasks/deployment/20240920130741_migrate_procedure_presentation_to_columns.rake_spec.rb new file mode 100644 index 000000000..6545cf027 --- /dev/null +++ b/spec/lib/tasks/deployment/20240920130741_migrate_procedure_presentation_to_columns.rake_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +describe '20240920130741_migrate_procedure_presentation_to_columns.rake' do + let(:rake_task) { Rake::Task['after_party:migrate_procedure_presentation_to_columns'] } + + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :text }]) } + let(:instructeur) { create(:instructeur) } + let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: instructeur) } + let(:stable_id) { procedure.active_revision.types_de_champ.first.stable_id } + let!(:procedure_presentation) do + displayed_fields = [ + { "table" => "etablissement", "column" => "entreprise_raison_sociale" }, + { "table" => "type_de_champ", "column" => stable_id.to_s } + ] + + sort = { "order" => "desc", "table" => "self", "column" => "en_construction_at" } + + filters = { + "tous" => [], + "suivis" => [], + "traites" => [{ "label" => "Libellé NAF", "table" => "etablissement", "value" => "Administration publique générale", "column" => "libelle_naf", "value_column" => "value" }], + "a-suivre" => [], + "archives" => [], + "expirant" => [], + "supprimes" => [], + "supprimes_recemment" => [] + } + + create(:procedure_presentation, assign_to:, displayed_fields:, filters:, sort:) + end + + before do + rake_task.invoke + + procedure_presentation.reload + end + + it 'populates the columns' do + procedure_id = procedure.id + + expect(procedure_presentation.displayed_columns).to eq([ + { "procedure_id" => procedure_id, "column_id" => "etablissement/entreprise_raison_sociale" }, + { "procedure_id" => procedure_id, "column_id" => "type_de_champ/#{stable_id}" } + ]) + + order, column_id = procedure_presentation + .sorted_column + .then { |sorted| [sorted['order'], sorted['id']] } + + expect(order).to eq('desc') + expect(column_id).to eq("procedure_id" => procedure_id, "column_id" => "self/en_construction_at") + + expect(procedure_presentation.tous_filters).to eq([]) + + traites = procedure_presentation.traites_filters + .map { [_1['id'], _1['filter']] } + + expect(traites).to eq([[{ "column_id" => "etablissement/libelle_naf", "procedure_id" => procedure_id }, "Administration publique générale"]]) + end +end