Skip to content

Commit

Permalink
Merge branch 'develop' into contact-data-migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Jan 7, 2025
2 parents 8f4c159 + 3129880 commit 48a58fa
Show file tree
Hide file tree
Showing 23 changed files with 286 additions and 59 deletions.
30 changes: 15 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ GEM
after_commit_everywhere (1.5.0)
activerecord (>= 4.2)
activesupport
appsignal (4.2.1)
appsignal (4.3.2)
logger
rack
ast (2.4.2)
Expand All @@ -135,7 +135,7 @@ GEM
aws-sdk-sqs (~> 1, >= 1.56.0)
concurrent-ruby (~> 1)
aws-eventstream (1.3.0)
aws-partitions (1.1023.0)
aws-partitions (1.1031.0)
aws-record (2.13.2)
aws-sdk-dynamodb (~> 1, >= 1.85.0)
aws-record-rails (0.1.0)
Expand All @@ -144,7 +144,7 @@ GEM
aws-sdk-cloudwatch (1.108.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sigv4 (~> 1.5)
aws-sdk-core (3.214.0)
aws-sdk-core (3.214.1)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.992.0)
aws-sigv4 (~> 1.9)
Expand All @@ -162,7 +162,7 @@ GEM
aws-activejob-sqs (~> 0)
aws-record-rails (~> 0)
railties (>= 7.1.0)
aws-sdk-s3 (1.176.1)
aws-sdk-s3 (1.177.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
Expand All @@ -175,7 +175,7 @@ GEM
aws-sdk-sns (1.91.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sigv4 (~> 1.5)
aws-sdk-sqs (1.89.0)
aws-sdk-sqs (1.90.0)
aws-sdk-core (~> 3, >= 3.210.0)
aws-sigv4 (~> 1.5)
aws-sessionstore-dynamodb (3.0.1)
Expand All @@ -190,7 +190,7 @@ GEM
bigdecimal (3.1.8)
bootsnap (1.18.4)
msgpack (~> 1.2)
brakeman (6.2.2)
brakeman (7.0.0)
racc
builder (3.3.0)
capybara (3.40.0)
Expand All @@ -208,7 +208,7 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
countries (7.0.0)
countries (7.1.0)
unaccent (~> 0.3)
coverband (6.1.4)
redis (>= 3.0)
Expand Down Expand Up @@ -329,7 +329,7 @@ GEM
jmespath (1.6.2)
jsbundling-rails (1.3.1)
railties (>= 6.0.0)
json (2.9.0)
json (2.9.1)
jsonapi-serializer (2.2.0)
activesupport (>= 4.2)
jwt (2.9.3)
Expand All @@ -353,7 +353,7 @@ GEM
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.3)
logger (1.6.4)
lograge (0.14.0)
actionpack (>= 4)
activesupport (>= 4)
Expand Down Expand Up @@ -404,7 +404,7 @@ GEM
google-protobuf (>= 3.25.3)
pghero (3.6.1)
activerecord (>= 6.1)
phony (2.20.15)
phony (2.21.1)
phony_rails (0.15.0)
activesupport (>= 3.0)
phony (>= 2.18.12)
Expand All @@ -413,7 +413,7 @@ GEM
activesupport (>= 7.0.0)
rack
railties (>= 7.0.0)
pry (0.15.0)
pry (0.15.2)
coderay (~> 1.1)
method_source (~> 1.0)
psych (5.2.1)
Expand Down Expand Up @@ -478,7 +478,7 @@ GEM
redis-client (>= 0.22.0)
redis-client (0.22.2)
connection_pool
regexp_parser (2.9.3)
regexp_parser (2.10.0)
reline (0.6.0)
io-console (~> 0.5)
request_store (1.7.0)
Expand Down Expand Up @@ -518,12 +518,12 @@ GEM
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.36.2)
rubocop-ast (1.37.0)
parser (>= 3.3.1.0)
rubocop-minitest (0.35.0)
rubocop (>= 1.61, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-performance (1.23.0)
rubocop-performance (1.23.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.24.1)
Expand Down Expand Up @@ -583,7 +583,7 @@ GEM
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.2)
thruster (0.1.9)
thruster (0.1.10)
tilt (2.3.0)
timeout (0.4.2)
turbo-rails (2.0.5)
Expand Down
35 changes: 35 additions & 0 deletions app/controllers/api/v1/addresses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module API
module V1
class AddressesController < BaseController
def index
respond_with_resource(beneficiary.addresses)
end

def create
validate_request_schema(
with: ::V1::BeneficiaryAddressRequestSchema,
schema_options: { beneficiary: },
location: ->(resource) { api_v1_beneficiary_address_path(beneficiary, resource) }
) do |permitted_params|
beneficiary.addresses.create!(permitted_params)
end
end

def show
address = beneficiary.addresses.find(params[:id])
respond_with_resource(address)
end

def destroy
address = beneficiary.addresses.find(params[:id])
address.destroy!

head :no_content
end

def beneficiary
@beneficiary ||= current_account.beneficiaries.find(params[:beneficiary_id])
end
end
end
end
5 changes: 4 additions & 1 deletion app/controllers/api/v1/beneficiaries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def create
# TODO: can remove this once after we rename the model to beneficiary
location: ->(resource) { api_v1_beneficiary_path(resource) }
) do |permitted_params|
CreateBeneficiaryWithAddress.new(permitted_params).call
CreateBeneficiaryWithAddress.new(
account: current_account,
**permitted_params
).call
end
end

Expand Down
6 changes: 4 additions & 2 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Account < ApplicationRecord
DEFAULT_PERMISSIONS_BITMASK = 0
TWILIO_ACCOUNT_SID_PREFIX = "AC".freeze
DEFAULT_PLATFORM_PROVIDER = "twilio".freeze
PLATFORM_PROVIDERS = [DEFAULT_PLATFORM_PROVIDER, "somleng"].freeze
PLATFORM_PROVIDERS = [ DEFAULT_PLATFORM_PROVIDER, "somleng" ].freeze
DEFAULT_CALL_FLOW_LOGIC = "CallFlowLogic::HelloWorld".freeze

include MetadataHelpers
Expand All @@ -27,6 +27,8 @@ class Account < ApplicationRecord

has_many :contacts,
dependent: :restrict_with_error
has_many :beneficiaries, class_name: "Contact",
dependent: :restrict_with_error

has_many :callouts,
dependent: :restrict_with_error
Expand Down Expand Up @@ -93,7 +95,7 @@ def write_batch_operation_access_token
end

def phone_call_queue_limit
[settings.fetch("phone_call_queue_limit").to_i, 1000].max
[ settings.fetch("phone_call_queue_limit").to_i, 1000 ].max
end

def from_phone_number
Expand Down
4 changes: 4 additions & 0 deletions app/models/beneficiary_address.rb
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
4 changes: 1 addition & 3 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class Contact < ApplicationRecord
extend Enumerize

COUNTRY_CODES = ISO3166::Country.codes.freeze

include MsisdnHelpers
include MetadataHelpers

enumerize :status, in: [ :active, :disabled ], scope: :shallow
enumerize :gender, in: { male: "M", female: "F" }
enumerize :iso_country_code, in: COUNTRY_CODES
enumerize :iso_country_code, in: ISO3166::Country.codes.freeze

belongs_to :account

Expand Down
11 changes: 0 additions & 11 deletions app/request_schemas/v1/base_request_schema.rb

This file was deleted.

19 changes: 19 additions & 0 deletions app/request_schemas/v1/beneficiary_address_request_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module V1
class BeneficiaryAddressRequestSchema < JSONAPIRequestSchema
params do
required(:data).value(:hash).schema do
required(:type).filled(:str?, eql?: "address")
required(:attributes).value(:hash).schema do
required(:iso_country_code).filled(Types::UpcaseString, included_in?: Contact.iso_country_code.values)
required(:iso_region_code).filled(: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)
optional(:administrative_division_level_3_name).maybe(:string)
optional(:administrative_division_level_4_code).maybe(:string)
optional(:administrative_division_level_4_name).maybe(:string)
end
end
end
end
end
4 changes: 2 additions & 2 deletions app/request_schemas/v1/beneficiary_request_schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module V1
class BeneficiaryRequestSchema < BaseRequestSchema
class BeneficiaryRequestSchema < JSONAPIRequestSchema
params do
required(:data).value(:hash).schema do
required(:type).filled(:str?, eql?: "beneficiary")
Expand All @@ -13,7 +13,7 @@ class BeneficiaryRequestSchema < BaseRequestSchema

optional(:address).filled(:hash).schema do
required(:iso_country_code).filled(Types::UpcaseString, included_in?: Contact.iso_country_code.values)
required(:iso_region_code).maybe(:string)
required(:iso_region_code).filled(: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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module V1
class UpdateBeneficiaryRequestSchema < BaseRequestSchema
class UpdateBeneficiaryRequestSchema < JSONAPIRequestSchema
params do
required(:data).value(:hash).schema do
required(:id).filled(:integer)
Expand Down
2 changes: 1 addition & 1 deletion app/serailizers/beneficiary_address_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class BeneficiaryAddressSerializer < ResourceSerializer
set_type :address

attributes :iso_region_code,
attributes :iso_country_code, :iso_region_code,
:administrative_division_level_2_code,
:administrative_division_level_2_name,
:administrative_division_level_3_code,
Expand Down
21 changes: 20 additions & 1 deletion app/workflows/handle_phone_call_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
class HandlePhoneCallEvent < ApplicationWorkflow
attr_accessor :url, :params

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


def initialize(url, params = {})
self.url = url
self.params = params
Expand Down Expand Up @@ -60,6 +75,10 @@ def create_or_find_phone_call!(event)

def create_or_find_contact!(platform_account_sid, msisdn)
account = Account.find_by_platform_account_sid(platform_account_sid)
Contact.find_or_create_by!(account: account, msisdn: PhonyRails.normalize_number(msisdn))
Contact.find_or_create_by!(
account: account,
msisdn: PhonyRails.normalize_number(msisdn),
iso_country_code: ACCOUNT_COUNTRY_CODES.fetch(account.id, "KH")
)
end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
end

namespace :v1, module: "api/v1", as: "api_v1", defaults: { format: "json" } do
resources :beneficiaries, only: [ :index, :create, :show, :update ]
resources :beneficiaries, only: [ :index, :create, :show, :update ] do
resources :addresses, only: [ :index, :create, :show, :destroy ]
end
end

namespace "api", defaults: { format: "json" } do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def change
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 ], unique: true
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 ], unique: true
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
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.citext "iso_country_code", null: false
t.index ["beneficiary_id", "iso_country_code", "iso_region_code", "administrative_division_level_2_code", "administrative_division_level_3_code", "administrative_division_level_4_code"], name: "idx_on_beneficiary_id_iso_country_code_iso_region_c_069288d0e5", unique: true
t.index ["beneficiary_id", "iso_country_code", "iso_region_code", "administrative_division_level_2_name", "administrative_division_level_3_name", "administrative_division_level_4_name"], name: "idx_on_beneficiary_id_iso_country_code_iso_region_c_e888f7dc18", unique: true
t.index ["beneficiary_id", "iso_country_code", "iso_region_code", "administrative_division_level_2_code", "administrative_division_level_3_code", "administrative_division_level_4_code"], name: "idx_on_beneficiary_id_iso_country_code_iso_region_c_069288d0e5"
t.index ["beneficiary_id", "iso_country_code", "iso_region_code", "administrative_division_level_2_name", "administrative_division_level_3_name", "administrative_division_level_4_name"], name: "idx_on_beneficiary_id_iso_country_code_iso_region_c_e888f7dc18"
t.index ["beneficiary_id"], name: "index_beneficiary_addresses_on_beneficiary_id"
end

Expand Down
6 changes: 6 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,10 @@

association :audio_file, factory: :active_storage_attachment, filename: "test.mp3"
end

factory :beneficiary_address do
beneficiary
iso_country_code { "KH" }
iso_region_code { "KH-1" }
end
end
14 changes: 0 additions & 14 deletions spec/models/contact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,4 @@
it { is_expected.not_to allow_value("252123456").for(:msisdn) }
it { is_expected.to allow_value("+252 66-(2)-345-678").for(:msisdn) }
end

describe "#assign_iso_country_code" do
it "assigns iso country code" do
beneficiary = create(:beneficiary, msisdn: "+85510999999", iso_country_code: nil)

expect(beneficiary.iso_country_code).to eq("KH")
end

it "preserves iso country code" do
beneficiary = create(:beneficiary, msisdn: "+85510999999", iso_country_code: "TH")

expect(beneficiary.iso_country_code).to eq("TH")
end
end
end
Loading

0 comments on commit 48a58fa

Please sign in to comment.