diff --git a/app/jobs/migrations/normalize_communes_job.rb b/app/jobs/migrations/normalize_communes_job.rb new file mode 100644 index 000000000..736ca0383 --- /dev/null +++ b/app/jobs/migrations/normalize_communes_job.rb @@ -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 diff --git a/lib/tasks/deployment/20230407132756_normalize_communes.rake b/lib/tasks/deployment/20230407132756_normalize_communes.rake new file mode 100644 index 000000000..e273175b9 --- /dev/null +++ b/lib/tasks/deployment/20230407132756_normalize_communes.rake @@ -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 diff --git a/spec/jobs/migrations/normalize_communes_job_spec.rb b/spec/jobs/migrations/normalize_communes_job_spec.rb new file mode 100644 index 000000000..cdee14971 --- /dev/null +++ b/spec/jobs/migrations/normalize_communes_job_spec.rb @@ -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