data(backfill): Champs::RnaChamp.value_json [±7.5k occurences]

This commit is contained in:
mfo 2024-08-19 17:43:55 +02:00
parent 4bf5725d6d
commit 917f25dcfd
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
2 changed files with 59 additions and 0 deletions

View file

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

View file

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