fix(dossier): normalize champs commune data

This commit is contained in:
Paul Chavard 2023-04-07 16:02:13 +02:00
parent 0d1aa5cf96
commit a6e3ea4839
3 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,28 @@
class Migrations::NormalizeCommunesJob < ApplicationJob
def perform(ids)
Champs::CommuneChamp.where(id: ids).find_each do |champ|
next if champ.external_id.blank?
value_json = champ.value_json || {}
if !champ.departement?
metro_code = champ.external_id[0..1]
drom_com_code = champ.external_id[0..2]
if metro_code == '97' || metro_code == '98'
value_json[:code_departement] = drom_com_code
else
value_json[:code_departement] = metro_code
end
end
if !champ.code_postal? && champ.code_postal_with_fallback?
value_json[:code_postal] = champ.code_postal_with_fallback
end
if value_json.present?
champ.update_column(:value_json, value_json)
end
end
end
end

View file

@ -0,0 +1,21 @@
namespace :after_party do
desc 'Deployment task: normalize_communes'
task normalize_communes: :environment do
puts "Running deploy task 'normalize_communes'"
champs = Champs::CommuneChamp.where.not(external_id: nil)
progress = ProgressReport.new(champs.count)
champs.pluck(:id).in_groups_of(10_000, false) do |champ_ids|
Migrations::NormalizeCommunesJob.perform_later(champ_ids)
progress.inc(champ_ids.count)
end
progress.finish
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -0,0 +1,32 @@
describe Migrations::NormalizeCommunesJob, type: :job do
let(:champ) { create(:champ_communes, external_id: code_insee, code_departement: nil) }
let(:code_insee) { '97209' }
let(:value) { 'Fort-de-France (97200)' }
before { champ.update_column(:value, value) }
subject { described_class.perform_now([champ.id]) }
context 'Fort-de-France' do
it 'assign code_departement and code_postal' do
expect(champ.reload.code_postal).to be_nil
expect(champ.reload.code_departement).to be_nil
subject
expect(champ.reload.code_postal).to eq('97200')
expect(champ.reload.code_departement).to eq('972')
end
end
context 'Ajaccio' do
let(:code_insee) { '2A004' }
let(:value) { 'Ajaccio (20000)' }
it 'assign code_departement and code_postal' do
expect(champ.reload.code_postal).to be_nil
expect(champ.reload.code_departement).to be_nil
subject
expect(champ.reload.code_postal).to eq('20000')
expect(champ.reload.code_departement).to eq('2A')
end
end
end