From 23a3b69353f2c9203deb62b8d681b857fcc535fa Mon Sep 17 00:00:00 2001 From: Samnang Chhun Date: Tue, 17 Dec 2024 15:12:10 +0700 Subject: [PATCH] Update province and country iso code --- .../call_flow_logic/ews_registration.rb | 2 +- app/models/contact.rb | 10 +++++++- app/request_schemas/types.rb | 7 ++++++ .../v1/beneficiary_request_schema.rb | 17 ++++++++----- .../call_flow_logic/ews_registration_spec.rb | 2 +- .../open_ews_api/v1/beneficiaries_spec.rb | 11 +++++--- .../workflows/handle_phone_call_event_spec.rb | 25 +++++++++++++------ 7 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 app/request_schemas/types.rb diff --git a/app/models/call_flow_logic/ews_registration.rb b/app/models/call_flow_logic/ews_registration.rb index e0f67074f..e83ec570c 100644 --- a/app/models/call_flow_logic/ews_registration.rb +++ b/app/models/call_flow_logic/ews_registration.rb @@ -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 ) diff --git a/app/models/contact.rb b/app/models/contact.rb index ac73b3325..c7ab55e3d 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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 @@ -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 diff --git a/app/request_schemas/types.rb b/app/request_schemas/types.rb new file mode 100644 index 000000000..6006fbf1e --- /dev/null +++ b/app/request_schemas/types.rb @@ -0,0 +1,7 @@ +module Types + include Dry::Types() + + UpcaseString = Types::String.constructor do |str| + str ? str.upcase : str + end +end diff --git a/app/request_schemas/v1/beneficiary_request_schema.rb b/app/request_schemas/v1/beneficiary_request_schema.rb index 0d66b8c95..d41345187 100644 --- a/app/request_schemas/v1/beneficiary_request_schema.rb +++ b/app/request_schemas/v1/beneficiary_request_schema.rb @@ -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 @@ -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 diff --git a/spec/models/call_flow_logic/ews_registration_spec.rb b/spec/models/call_flow_logic/ews_registration_spec.rb index eb3cf6699..91ac6c647 100644 --- a/spec/models/call_flow_logic/ews_registration_spec.rb +++ b/spec/models/call_flow_logic/ews_registration_spec.rb @@ -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" ) diff --git a/spec/requests/open_ews_api/v1/beneficiaries_spec.rb b/spec/requests/open_ews_api/v1/beneficiaries_spec.rb index b9dbeb1a3..241a8d2b7 100644 --- a/spec/requests/open_ews_api/v1/beneficiaries_spec.rb +++ b/spec/requests/open_ews_api/v1/beneficiaries_spec.rb @@ -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" } } @@ -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 @@ -116,6 +116,7 @@ example "Update a beneficiary" do beneficiary = create( :beneficiary, + msisdn: "+85510999001", gender: nil, language_code: nil, date_of_birth: nil, @@ -129,6 +130,7 @@ id: beneficiary.id, type: :beneficiary, attributes: { + msisdn: "+85510999002", gender: "F", status: "disabled", language_code: "en", @@ -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", diff --git a/spec/workflows/handle_phone_call_event_spec.rb b/spec/workflows/handle_phone_call_event_spec.rb index b30fc461f..b8335b4f1 100644 --- a/spec/workflows/handle_phone_call_event_spec.rb +++ b/spec/workflows/handle_phone_call_event_spec.rb @@ -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) @@ -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