-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate contact's metadata into native columns (#1670)
- Loading branch information
Showing
26 changed files
with
200 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
class BeneficiaryAddress < ApplicationRecord | ||
extend Enumerize | ||
|
||
enumerize :iso_country_code, in: ISO3166::Country.codes.freeze | ||
|
||
belongs_to :beneficiary, class_name: "Contact" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
db/migrate/20250107072933_add_iso_country_code_to_beneficiary_addresses.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class AddIsoCountryCodeToBeneficiaryAddresses < ActiveRecord::Migration[8.0] | ||
def change | ||
change_column_null :beneficiary_addresses, :iso_region_code, false | ||
add_column :beneficiary_addresses, :iso_country_code, :citext, null: false | ||
|
||
remove_index :beneficiary_addresses, [ :iso_region_code, :administrative_division_level_2_code, :administrative_division_level_3_code, :administrative_division_level_4_code ] | ||
remove_index :beneficiary_addresses, [ :iso_region_code, :administrative_division_level_2_name, :administrative_division_level_3_name, :administrative_division_level_4_name ] | ||
|
||
add_index :beneficiary_addresses, [ :beneficiary_id, :iso_country_code, :iso_region_code, :administrative_division_level_2_code, :administrative_division_level_3_code, :administrative_division_level_4_code ] | ||
add_index :beneficiary_addresses, [ :beneficiary_id, :iso_country_code, :iso_region_code, :administrative_division_level_2_name, :administrative_division_level_3_name, :administrative_division_level_4_name ] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
lib/tasks/data_migrations/migrate_contact_metadata_to_native_fields.rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
module Subdivisions | ||
Province = Struct.new(:code, :iso3166, :name_en, keyword_init: true) | ||
|
||
ZAMBIA_PROVINCES = [ | ||
Province.new(code: "ZM-02", iso3166: "ZM-02", name_en: "Central"), | ||
Province.new(code: "ZM-08", iso3166: "ZM-08", name_en: "Copperbelt"), | ||
Province.new(code: "ZM-03", iso3166: "ZM-03", name_en: "Eastern"), | ||
Province.new(code: "ZM-04", iso3166: "ZM-04", name_en: "Luapula"), | ||
Province.new(code: "ZM-09", iso3166: "ZM-09", name_en: "Lusaka"), | ||
Province.new(code: "ZM-10", iso3166: "ZM-10", name_en: "Muchinga"), | ||
Province.new(code: "ZM-06", iso3166: "ZM-06", name_en: "North-Western"), | ||
Province.new(code: "ZM-05", iso3166: "ZM-05", name_en: "Northern"), | ||
Province.new(code: "ZM-07", iso3166: "ZM-07", name_en: "Southern"), | ||
Province.new(code: "ZM-01", iso3166: "ZM-01", name_en: "Western") | ||
] | ||
end | ||
|
||
ACCOUNT_COUNTRY_CODES = { | ||
2 => "SO", | ||
3 => "KH", | ||
4 => "KH", | ||
7 => "SL", | ||
8 => "TH", | ||
11 => "US", | ||
45 => "MX", | ||
77 => "EG", | ||
110 => "ZM", | ||
143 => "KH", | ||
209 => "LA" | ||
} | ||
|
||
cache = {} | ||
|
||
namespace :data_migrations do | ||
task migrate_contact_metadata_to_native_fields: :environment do | ||
Account.find_each do |account| | ||
puts "Migrating contacts for account: #{account.id}" | ||
|
||
contacts = account.contacts.where(iso_country_code: nil) | ||
contacts.find_each do |contact| | ||
ApplicationRecord.transaction do | ||
contact.update_columns( | ||
iso_country_code: ACCOUNT_COUNTRY_CODES.fetch(account.id), | ||
language_code: contact.metadata["language_code"], | ||
date_of_birth: contact.metadata["date_of_birth"], | ||
status: contact.metadata["deregistered_at"].present? ? :disabled : :active | ||
) | ||
|
||
# EWS Cambodia | ||
Array(contact.metadata["commune_ids"]).each do | commune_id | | ||
cache[commune_id] ||= Pumi::Commune.find_by_id(commune_id) | ||
commune = cache[commune_id] | ||
next if commune.blank? | ||
|
||
contact.addresses.find_or_create_by!( | ||
iso_country_code: "KH", | ||
iso_region_code: commune.province.iso3166_2, | ||
administrative_division_level_2_code: commune.district_id, | ||
administrative_division_level_2_name: commune.district.name_en, | ||
administrative_division_level_3_code: commune.id, | ||
administrative_division_level_3_name: commune.name_en, | ||
created_at: contact.updated_at, | ||
updated_at: contact.updated_at | ||
) | ||
end | ||
|
||
# EWS Laos | ||
Array(contact.metadata["registered_districts"]).each do | district_code | | ||
district = CallFlowLogic::EWSLaosRegistration::DISTRICTS.find { |d| d.code == district_code } | ||
next if district.blank? | ||
|
||
contact.addresses.find_or_create_by!( | ||
iso_country_code: "LA", | ||
iso_region_code: district.province.iso3166, | ||
administrative_division_level_2_code: district.code, | ||
administrative_division_level_2_name: district.name_en, | ||
created_at: contact.updated_at, | ||
updated_at: contact.updated_at | ||
) | ||
end | ||
|
||
# PIN Zambia | ||
if account.id == 110 | ||
province = Subdivisions::ZAMBIA_PROVINCES.find { |d| d.name_en == contact.metadata["province"] } | ||
next if province.blank? | ||
|
||
contact.addresses.find_or_create_by!( | ||
iso_country_code: "ZM", | ||
iso_region_code: province.iso3166, | ||
administrative_division_level_2_name: contact.metadata["district"], | ||
administrative_division_level_3_name: contact.metadata["facility"] | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.