diff --git a/app/tasks/maintenance/create_procedure_tags_task.rb b/app/tasks/maintenance/create_procedure_tags_task.rb new file mode 100644 index 000000000..7b8c7c1ab --- /dev/null +++ b/app/tasks/maintenance/create_procedure_tags_task.rb @@ -0,0 +1,110 @@ +# frozen_string_literal: true + +# this task is used to create the procedure_tags and backfill the procedures that have the tag in their tags array + +module Maintenance + class CreateProcedureTagsTask < MaintenanceTasks::Task + include RunnableOnDeployConcern + include StatementsHelpersConcern + run_on_first_deploy + + def collection + [ + "Aap", + "Accompagnement", + "Action sociale", + "Adeli", + "Affectation", + "Agrément", + "Agriculture", + "agroécologie", + "Aide aux entreprises", + "Aide financière", + "Appel à manifestation d'intérêt", + "AMI", + "Animaux", + "Appel à projets", + "Association", + "Auto-école", + "Autorisation", + "Autorisation d'exercer", + "Bilan", + "Biodiversité", + "Candidature", + "Cerfa", + "Chasse", + "Cinéma", + "Cmg", + "Collectivé territoriale", + "Collège", + "Convention", + "Covid", + "Culture", + "Dérogation", + "Diplôme", + "Drone", + "DSDEN", + "Eau", + "Ecoles", + "Education", + "Elections", + "Energie", + "Enseignant", + "ENT", + "Environnement", + "Étrangers", + "Formation", + "FPRNM", + "Funéraire", + "Handicap", + "Hygiène", + "Industrie", + "innovation", + "Inscription", + "Logement", + "Lycée", + "Manifestation", + "Médicament", + "Micro-crèche", + "MODELE DS", + "Numérique", + "Permis", + "Pompiers", + "Préfecture", + "Professionels de santé", + "Recrutement", + "Rh", + "Santé", + "Scolaire", + "SDIS", + "Sécurité", + "Sécurité routière", + "Sécurité sociale", + "Séjour", + "Service civique", + "Subvention", + "Supérieur", + "Taxi", + "Télétravail", + "Tirs", + "Transition écologique", + "Transport", + "Travail", + "Université", + "Urbanisme" + ] + end + + def process(tag) + procedure_tag = ProcedureTag.find_or_create_by(name: tag) + + Procedure.where("? ILIKE ANY(tags)", tag).find_each(batch_size: 500) do |procedure| + procedure.procedure_tags << procedure_tag unless procedure.procedure_tags.include?(procedure_tag) + end + end + + def count + collection.size + end + end +end diff --git a/spec/tasks/maintenance/create_procedure_tags_task_spec.rb b/spec/tasks/maintenance/create_procedure_tags_task_spec.rb new file mode 100644 index 000000000..1672cf398 --- /dev/null +++ b/spec/tasks/maintenance/create_procedure_tags_task_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe CreateProcedureTagsTask do + describe "#process" do + subject(:process) { described_class.new.process(tag) } + + let(:tag) { "Accompagnement" } + let!(:procedure) { create(:procedure, tags: ["Accompagnement"]) } + + it "creates the ProcedureTag if it does not exist" do + expect { process }.to change { ProcedureTag.count }.by(1) + expect(ProcedureTag.last.name).to eq(tag) + end + + context "when the ProcedureTag already exists" do + let!(:procedure_tag) { ProcedureTag.create(name: tag) } + + it "does not create a duplicate ProcedureTag" do + expect { process }.not_to change { ProcedureTag.count } + end + end + + it "associates procedures with the ProcedureTag" do + process + expect(procedure.reload.procedure_tags.map(&:name)).to include(tag) + end + end + end +end