Skip to content

Commit

Permalink
Update data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Jan 7, 2025
1 parent 2d5e6d0 commit 34879c6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 30 deletions.
2 changes: 2 additions & 0 deletions app/models/call_flow_logic/ews_laos_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ def update_contact
district = DISTRICTS.find { |d| d.code == phone_call_metadata(:district_code) }

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,
)

registered_districts = contact.metadata.fetch("registered_districts", [])
Expand Down
5 changes: 4 additions & 1 deletion app/models/call_flow_logic/ews_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,12 @@ def update_contact
commune = Pumi::Commune.find_by_id(phone_call_metadata(:commune_code))

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_3_code: commune.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,
)

commune_ids = contact.metadata.fetch("commune_ids", [])
Expand Down
14 changes: 3 additions & 11 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Contact < ApplicationRecord
include MsisdnHelpers
include MetadataHelpers

enumerize :status, in: [ :active, :disabled ], scope: true
enumerize :gender, in: { male: "M", female: "F" }, scope: true
enumerize :iso_country_code, in: COUNTRY_CODES, scope: true
enumerize :status, in: [ :active, :disabled ], scope: :shallow
enumerize :gender, in: { male: "M", female: "F" }
enumerize :iso_country_code, in: COUNTRY_CODES

belongs_to :account

Expand All @@ -30,15 +30,7 @@ class Contact < ApplicationRecord
to: :account,
allow_nil: true

before_create :assign_iso_country_code, unless: :iso_country_code?

def self.jsonapi_serializer_class
BeneficiarySerializer
end

private

def assign_iso_country_code
self.iso_country_code = PhonyRails.country_from_number(msisdn)
end
end
3 changes: 2 additions & 1 deletion app/request_schemas/v1/beneficiary_request_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class BeneficiaryRequestSchema < BaseRequestSchema
optional(:metadata).value(:hash)

optional(:address).filled(:hash).schema do
optional(:iso_region_code).maybe(:string)
required(:iso_country_code).filled(Types::UpcaseString, included_in?: Contact.iso_country_code.values)
required(:iso_region_code).maybe(:string)
optional(:administrative_division_level_2_code).maybe(:string)
optional(:administrative_division_level_2_name).maybe(:string)
optional(:administrative_division_level_3_code).maybe(:string)
Expand Down
5 changes: 0 additions & 5 deletions db/migrate/20241219131752_update.rb

This file was deleted.

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, [ :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, [ :iso_country_code, :iso_region_code, :administrative_division_level_2_name, :administrative_division_level_3_name, :administrative_division_level_4_name ]
end
end
7 changes: 4 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2024_12_19_131752) do
ActiveRecord::Schema[8.0].define(version: 2025_01_07_072933) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_catalog.plpgsql"
Expand Down Expand Up @@ -89,9 +89,10 @@
t.string "administrative_division_level_4_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.citext "iso_country_code", null: false
t.index ["beneficiary_id"], name: "index_beneficiary_addresses_on_beneficiary_id"
t.index ["iso_region_code", "administrative_division_level_2_code", "administrative_division_level_3_code", "administrative_division_level_4_code"], name: "idx_on_iso_region_code_administrative_division_leve_a5183cd2b4"
t.index ["iso_region_code", "administrative_division_level_2_name", "administrative_division_level_3_name", "administrative_division_level_4_name"], name: "idx_on_iso_region_code_administrative_division_leve_c76774f7b0"
t.index ["iso_country_code", "iso_region_code", "administrative_division_level_2_code", "administrative_division_level_3_code", "administrative_division_level_4_code"], name: "idx_on_iso_country_code_iso_region_code_administrat_c1ceb2e20a"
t.index ["iso_country_code", "iso_region_code", "administrative_division_level_2_name", "administrative_division_level_3_name", "administrative_division_level_4_name"], name: "idx_on_iso_country_code_iso_region_code_administrat_af49107848"
end

create_table "callout_participations", force: :cascade do |t|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ module Subdivisions
Province.new(code: "ZM-07", iso3166: "ZM-07", name_en: "Southern"),
Province.new(code: "ZM-01", iso3166: "ZM-01", name_en: "Western")
]

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"
}
end

namespace :data_migrations do
Expand All @@ -23,19 +37,25 @@ module Subdivisions
contacts = account.contacts.where(iso_country_code: nil)
contacts.find_each do |contact|
ApplicationRecord.transaction do
contact.iso_country_code = PhonyRails.country_from_number(contact.msisdn)
contact.language_code = contact.metadata["language_code"]
contact.save!
contact.update_columns(
iso_country_code: ACCOUNT_COUNTRY_CODES.fetch(account.id),
language_code: contact.metadata["language_code"]
)

# EWS Cambodia
contact.metadata.fetch("commune_ids", []).each do | commune_id |
commune = Pumi::Commune.find_by_id(phone_call_metadata(:commune_code))
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_3_code: commune.id
administrative_division_level_2_code: commune.district.name_en,
administrative_division_level_3_code: commune.id,
administrative_division_level_3_code: commune.name_en,
created_at: contact.updated_at,
updated_at: contact.updated_at
)
end

Expand All @@ -45,8 +65,12 @@ module Subdivisions
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

Expand All @@ -56,15 +80,12 @@ module Subdivisions
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

# Africa's Voices (Somalia)
# metadata: {"group"=>"Wajid_2", "location"=>"Wajid Town", "householdname"=>"BAKWAJ0010233"}
# metadata: {"dec10"=>"True", "district"=>"Wajid", "location"=>"Wajid", "retailer"=>"World Vision Topup Kabasa IDP", "scope_id"=>"BAKWAJ0001996"}
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/models/call_flow_logic/ews_laos_registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ module CallFlowLogic
expect(contact.addresses.last).to have_attributes(
iso_region_code: "LA-CH",
administrative_division_level_2_code: "1604",
administrative_division_level_2_name: "Paksong",
)
assert_play("registration_successful-lao.mp3", response)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/models/call_flow_logic/ews_registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@
expect(contact.addresses.last).to have_attributes(
iso_region_code: "KH-1",
administrative_division_level_2_code: "0105",
administrative_division_level_3_code: "010505"
administrative_division_level_2_name: "Ou Chrov",
administrative_division_level_3_code: "010505",
administrative_division_level_3_name: "Samraong"
)
assert_play("registration_successful-krr.wav", response)
end
Expand Down

0 comments on commit 34879c6

Please sign in to comment.