From 917f25dcfd9569564567caafc6a53b016d015322 Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 19 Aug 2024 17:43:55 +0200 Subject: [PATCH] =?UTF-8?q?data(backfill):=20Champs::RnaChamp.value=5Fjson?= =?UTF-8?q?=20[=C2=B17.5k=20occurences]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../populate_rna_json_value_task.rb | 20 ++++++++++ .../populate_rna_json_value_task_spec.rb | 39 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 app/tasks/maintenance/populate_rna_json_value_task.rb create mode 100644 spec/tasks/maintenance/populate_rna_json_value_task_spec.rb diff --git a/app/tasks/maintenance/populate_rna_json_value_task.rb b/app/tasks/maintenance/populate_rna_json_value_task.rb new file mode 100644 index 000000000..b809f9345 --- /dev/null +++ b/app/tasks/maintenance/populate_rna_json_value_task.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Maintenance + class PopulateRNAJSONValueTask < MaintenanceTasks::Task + def collection + Champs::RNAChamp.where.not(value: nil) + end + + def process(champ) + return if champ&.dossier&.procedure&.id.blank? + data = APIEntreprise::RNAAdapter.new(champ.value, champ&.dossier&.procedure&.id).to_params + return if data.blank? + champ.update(value_json: APIGeoService.parse_rna_address(data['adresse'])) + end + + def count + # not really interested in counting because it raises PG Statement timeout + end + end +end diff --git a/spec/tasks/maintenance/populate_rna_json_value_task_spec.rb b/spec/tasks/maintenance/populate_rna_json_value_task_spec.rb new file mode 100644 index 000000000..9103b9a95 --- /dev/null +++ b/spec/tasks/maintenance/populate_rna_json_value_task_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe PopulateRNAJSONValueTask do + describe "#process" do + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :rna }]) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:element) { dossier.champs.first } + subject(:process) { described_class.process(element) } + + let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') } + let(:status) { 200 } + + before do + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v4\/djepva\/api-association\/associations\/open_data\/#{element.value}/) + .to_return(body: body, status: status) + allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) + end + it 'updates value_json' do + expect { subject }.to change { element.reload.value_json } + .from(nil) + .to({ + "street_number" => "33", + "street_name" => "de Modagor", + "street_address" => "33 rue de Modagor", + "postal_code" => "75009", + "city_name" => "Paris", + "city_code" => "75108", + "departement_code" => nil, + "departement_name" => nil, + "region_code" => nil, + "region_name" => nil + }) + end + end + end +end