Skip to content

Commit

Permalink
Update province and country iso code
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Dec 17, 2024
1 parent 2e3337a commit 23a3b69
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/models/call_flow_logic/ews_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def update_contact
commune = Pumi::Commune.find_by_id(phone_call_metadata(:commune_code))

contact.addresses.find_or_create_by!(
iso_region_code: commune.province_id,
iso_region_code: commune.province.iso3166_2,
administrative_division_level_2_code: commune.district_id,
administrative_division_level_3_code: commune.id
)
Expand Down
10 changes: 9 additions & 1 deletion app/models/contact.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Contact < ApplicationRecord
extend Enumerize

COUNTRY_CODES = ISO3166::Country.codes.map(&:downcase).freeze
COUNTRY_CODES = ISO3166::Country.codes.freeze

include MsisdnHelpers
include MetadataHelpers
Expand Down Expand Up @@ -30,7 +30,15 @@ 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
7 changes: 7 additions & 0 deletions app/request_schemas/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Types
include Dry::Types()

UpcaseString = Types::String.constructor do |str|
str ? str.upcase : str
end
end
17 changes: 11 additions & 6 deletions app/request_schemas/v1/beneficiary_request_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class BeneficiaryRequestSchema < BaseRequestSchema
required(:type).filled(:str?, eql?: "beneficiary")
required(:attributes).value(:hash).schema do
required(:msisdn).filled(:string)
required(:iso_country_code).filled(:string, included_in?: Contact.iso_country_code.values)
required(:iso_country_code).filled(Types::UpcaseString, included_in?: Contact.iso_country_code.values)
optional(:language_code).maybe(:string)
optional(:date_of_birth).maybe(:date)
optional(:gender).maybe(:string, included_in?: Contact.gender.values)
optional(:gender).maybe(Types::UpcaseString, included_in?: Contact.gender.values)
optional(:metadata).maybe(:hash?)

optional(:address).filled(:hash).schema do
Expand Down Expand Up @@ -37,16 +37,21 @@ def output

class Rules < SchemaRules::JSONAPISchemaRules
def validate
return true if resource&.persisted?
return key(:msisdn).failure(text: "can't be blank") if values[:msisdn].blank?
if resource.blank?
return key(:msisdn).failure(text: "can't be blank") if values[:msisdn].blank?

key(:msisdn).failure(text: "must be unique") if contact_exists?
key(:msisdn).failure(text: "must be unique") if contact_exists?
elsif values[:msisdn].present?
key(:msisdn).failure(text: "must be unique") if contact_exists?
end
end

private

def contact_exists?
account.contacts.where_msisdn(values.fetch(:msisdn)).exists?
relation = account.contacts.where_msisdn(values.fetch(:msisdn))
relation = relation.where.not(id: resource.id) if resource.present?
relation.exists?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/call_flow_logic/ews_registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
"latest_address_en" => "Samraong Commune, Ou Chrov District, Banteay Meanchey Province"
)
expect(contact.addresses.last).to have_attributes(
iso_region_code: "01",
iso_region_code: "KH-1",
administrative_division_level_2_code: "0105",
administrative_division_level_3_code: "010505"
)
Expand Down
11 changes: 7 additions & 4 deletions spec/requests/open_ews_api/v1/beneficiaries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
gender: "M",
date_of_birth: "1990-01-01",
metadata: { "foo" => "bar" },
iso_country_code: "kh",
iso_country_code: "KH",
address: {
iso_region_code: "01",
iso_region_code: "KH-1",
administrative_division_level_2_code: "01"
}
}
Expand All @@ -65,12 +65,12 @@
"gender" => "M",
"date_of_birth" => "1990-01-01",
"metadata" => { "foo" => "bar" },
"iso_country_code" => "kh",
"iso_country_code" => "KH",
)

expect(json_response.dig("included", 0).to_json).to match_api_response_schema("address")
expect(json_response.dig("included", 0, "attributes")).to include(
"iso_region_code" => "01",
"iso_region_code" => "KH-1",
"administrative_division_level_2_code" => "01"
)
end
Expand Down Expand Up @@ -116,6 +116,7 @@
example "Update a beneficiary" do
beneficiary = create(
:beneficiary,
msisdn: "+85510999001",
gender: nil,
language_code: nil,
date_of_birth: nil,
Expand All @@ -129,6 +130,7 @@
id: beneficiary.id,
type: :beneficiary,
attributes: {
msisdn: "+85510999002",
gender: "F",
status: "disabled",
language_code: "en",
Expand All @@ -143,6 +145,7 @@
expect(response_status).to eq(200)
expect(response_body).to match_jsonapi_resource_schema("beneficiary")
expect(jsonapi_response_attributes).to include(
"msisdn" => "+85510999002",
"language_code" => "en",
"gender" => "F",
"date_of_birth" => "1990-01-01",
Expand Down
25 changes: 18 additions & 7 deletions spec/workflows/handle_phone_call_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class MyCallFlowLogic < CallFlowLogic::Base; end
it "handles new phone calls" do
account = create_account(call_flow_logic: MyCallFlowLogic)
event_details = generate_event_details(
account: account, direction: "inbound", call_status: "in-progress"
account: account,
direction: "inbound",
call_status: "in-progress",
from: "85510900123"
)

result = HandlePhoneCallEvent.call(url, event_details)
Expand All @@ -23,12 +26,20 @@ class MyCallFlowLogic < CallFlowLogic::Base; end
expect(event.remote_direction).to eq("inbound")
expect(event.call_flow_logic).to eq(MyCallFlowLogic.to_s)

expect(event.phone_call).to be_persisted
expect(event.phone_call).to be_in_progress
expect(event.phone_call.remote_call_id).to eq(event.remote_call_id)
expect(event.phone_call.remote_direction).to eq("inbound")
expect(event.phone_call.msisdn).to match(event_details.fetch(:From))
expect(event.phone_call.remote_status).to eq("in-progress")
phone_call = event.phone_call
expect(phone_call).to be_persisted
expect(phone_call).to be_in_progress
expect(phone_call.remote_call_id).to eq(event.remote_call_id)
expect(phone_call.remote_direction).to eq("inbound")
expect(phone_call.msisdn).to match(event_details.fetch(:From))
expect(phone_call.remote_status).to eq("in-progress")

expect(phone_call.contact).to have_attributes(
persisted?: true,
account: account,
msisdn: "+85510900123",
iso_country_code: "KH"
)
end

it "handles existing phone calls" do
Expand Down

0 comments on commit 23a3b69

Please sign in to comment.