diff --git a/app/tasks/maintenance/populate_siret_value_json_task.rb b/app/tasks/maintenance/populate_siret_value_json_task.rb new file mode 100644 index 000000000..b8531e705 --- /dev/null +++ b/app/tasks/maintenance/populate_siret_value_json_task.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Maintenance + class PopulateSiretValueJSONTask < MaintenanceTasks::Task + def collection + Champs::SiretChamp.where.not(value: nil) + end + + def process(champ) + return if champ.etablissement.blank? + champ.update!(value_json: APIGeoService.parse_etablissement_address(champ.etablissement)) + end + + def count + # not really interested in counting because it raises PG Statement timeout + end + end +end diff --git a/spec/tasks/maintenance/populate_siret_value_json_task_spec.rb b/spec/tasks/maintenance/populate_siret_value_json_task_spec.rb new file mode 100644 index 000000000..4fca8f9ce --- /dev/null +++ b/spec/tasks/maintenance/populate_siret_value_json_task_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe PopulateSiretValueJSONTask do + describe "#process" do + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :siret }]) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:element) { dossier.champs.first } + subject(:process) { described_class.process(element) } + + it 'updates value_json' do + expect { subject }.to change { element.reload.value_json } + .from(nil) + .to({ + "city_code" => "92009", + "city_name" => "Bois-Colombes", + "postal_code" => "92270", + "region_code" => "11", + "region_name" => "Île-de-France", + "street_name" => "RAOUL NORDLING", + "street_number" => "6", + "street_address" => "6 RUE RAOUL NORDLING", + "departement_code" => "92", + "departement_name" => "Hauts-de-Seine" + }) + end + end + end +end