diff --git a/app/tasks/maintenance/populate_rnf_json_value_task.rb b/app/tasks/maintenance/populate_rnf_json_value_task.rb new file mode 100644 index 000000000..50bf87a81 --- /dev/null +++ b/app/tasks/maintenance/populate_rnf_json_value_task.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Maintenance + class PopulateRNFJSONValueTask < MaintenanceTasks::Task + include Dry::Monads[:result] + + def collection + Champs::RNFChamp.where(value_json: nil) + # Collection to be iterated over + # Must be Active Record Relation or Array + end + + def process(champ) + result = champ.fetch_external_data + case result + in Success(data) + begin + champ.update_with_external_data!(data:) + rescue ActiveRecord::RecordInvalid + # some champ might have dossier nil + end + else + # not found + end + end + + def count + # not really interested in counting because it raises PG Statement timeout + end + end +end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 22f64c5e7..4ea63a544 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -576,7 +576,7 @@ describe Champ do end context "fetch_external_data_later" do - let(:data) { { data: { address: { city: "some external data" } } }.with_indifferent_access } + let(:data) { { address: { city: "some external data" } }.with_indifferent_access } it "fill data from external source" do expect_any_instance_of(Champs::RNFChamp).to receive(:fetch_external_data) { data } diff --git a/spec/tasks/maintenance/populate_rnf_json_value_task_spec.rb b/spec/tasks/maintenance/populate_rnf_json_value_task_spec.rb new file mode 100644 index 000000000..d3e739ab3 --- /dev/null +++ b/spec/tasks/maintenance/populate_rnf_json_value_task_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe PopulateRNFJSONValueTask do + describe "#process" do + include Dry::Monads[:result] + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :rnf }]) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:element) { dossier.champs.first } + let(:data) do + { + id: 3, + rnfId: '075-FDD-00003-01', + type: 'FDD', + department: '75', + title: 'Fondation SFR', + dissolvedAt: nil, + phone: '+33185060000', + email: 'fondation@sfr.fr', + addressId: 3, + createdAt: "2023-09-07T13:26:10.358Z", + updatedAt: "2023-09-07T13:26:10.358Z", + address: { + id: 3, + createdAt: "2023-09-07T13:26:10.358Z", + updatedAt: "2023-09-07T13:26:10.358Z", + label: "16 Rue du Général de Boissieu 75015 Paris", + type: "housenumber", + streetAddress: "16 Rue du Général de Boissieu", + streetNumber: "16", + streetName: "Rue du Général de Boissieu", + postalCode: "75015", + cityName: "Paris", + cityCode: "75115", + departmentName: "Paris", + departmentCode: "75", + regionName: "Île-de-France", + regionCode: "11" + }, + status: nil, + persons: [] + } + end + + subject(:process) { described_class.process(element) } + + before do + allow_any_instance_of(Champs::RNFChamp).to receive(:fetch_external_data).and_return(Success(data)) + end + + it 'updates value_json' do + expect { subject }.to change { element.reload.value_json } + .from(nil) + .to({ + "street_number" => "16", + "street_name" => "Rue du Général de Boissieu", + "street_address" => "16 Rue du Général de Boissieu", + "postal_code" => "75015", + "city_name" => "Paris 15e Arrondissement", + "city_code" => "75115", + "departement_code" => "75", + "departement_name" => "Paris", + "region_code" => "11", + "region_name" => "Île-de-France" + }) + end + end + end +end