data(backfill): Champs::RnfChamp.value_json [±400 occurences]

This commit is contained in:
mfo 2024-08-19 17:07:43 +02:00
parent d866309d45
commit 4bf5725d6d
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 103 additions and 1 deletions

View file

@ -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

View file

@ -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 }

View file

@ -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