From 9fa07532c1427342bf93347f32971e7e4f5da88b Mon Sep 17 00:00:00 2001 From: James Aimonetti Date: Fri, 27 Mar 2020 16:40:51 -0700 Subject: [PATCH] [4.3] KZOO-59: calculate account CID name (#6413) * [4.3] KZOO-59: calculate account CID name Prior, the caller's CID name would be returned from kz_attributes:get_account_external_cid/1 along with the calculated account CID number. Particularly for DISA callers, this meant their CID name was passed through (often their mobile number) instead of being set to the account's external CID name. This change calculates the CID name and number of the account and returns them both if configured. * catch if fixturedb isn't running --- core/kazoo_endpoint/Makefile | 3 + core/kazoo_endpoint/src/kz_attributes.erl | 65 ++++++++++++------- .../test/kz_attributes_tests.erl | 43 ++++++------ .../kazoo_endpoint/test/kz_endpoint_tests.erl | 1 - .../account0000000000000000000000003.json | 2 +- .../views/phone_numbers+crossbar_listing.json | 9 +++ .../docs/%2B19995552600.json | 7 ++ core/kazoo_number_manager/Makefile | 1 + core/kazoo_number_manager/src/knm_number.erl | 6 +- .../src/knm_phone_number.erl | 11 +++- 10 files changed, 95 insertions(+), 53 deletions(-) create mode 100644 core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/views/phone_numbers+crossbar_listing.json create mode 100644 core/kazoo_fixturedb/priv/dbs/numbers%2F%2B1999/docs/%2B19995552600.json diff --git a/core/kazoo_endpoint/Makefile b/core/kazoo_endpoint/Makefile index ac900fd4692..7ea4f318ac7 100644 --- a/core/kazoo_endpoint/Makefile +++ b/core/kazoo_endpoint/Makefile @@ -14,5 +14,8 @@ kze-test: $(MAKE) compile-test -C $(ROOT)/core/kazoo_sip/ $(MAKE) compile-test -C $(ROOT)/core/kazoo_stdlib/ $(MAKE) compile-test -C $(ROOT)/core/kazoo_schemas/ + $(MAKE) compile-test -C $(ROOT)/core/kazoo_call/ + $(MAKE) compile-test -C $(ROOT)/core/kazoo_data/ + $(MAKE) compile-test -C $(ROOT)/core/kazoo_fixturedb/ include $(ROOT)/make/kz.mk diff --git a/core/kazoo_endpoint/src/kz_attributes.erl b/core/kazoo_endpoint/src/kz_attributes.erl index be7d1fce850..60bb614e5e2 100644 --- a/core/kazoo_endpoint/src/kz_attributes.erl +++ b/core/kazoo_endpoint/src/kz_attributes.erl @@ -256,48 +256,61 @@ get_account_external_cid(Call) -> -spec maybe_get_account_cid(kz_term:ne_binary(), kz_term:ne_binary(), kapps_call:call()) -> cid(). maybe_get_account_cid(Number, Name, Call) -> case kzd_accounts:fetch(kapps_call:account_id(Call)) of - {'error', _} -> maybe_get_assigned_number(Number, Name, Call); - {'ok', JObj} -> maybe_get_account_external_number(Number, Name, JObj, Call) + {'error', _E} -> + ?LOG_INFO("failed to open ~s: ~p", [kapps_call:account_id(Call), _E]), + maybe_get_assigned_number(Number, Name, Call); + {'ok', AccountDoc} -> maybe_get_account_external_cid(Number, Name, Call, AccountDoc) end. --spec maybe_get_account_external_number(kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object(), kapps_call:call()) -> cid(). -maybe_get_account_external_number(Number, Name, Account, Call) -> - External = kz_json:get_ne_value([<<"caller_id">>, <<"external">>, <<"number">>], Account), - case is_valid_caller_id(External, Call) of +-spec maybe_get_account_external_cid(kz_term:ne_binary(), kz_term:ne_binary(), kapps_call:call(), kzd_accounts:doc()) -> cid(). +maybe_get_account_external_cid(Number, Name, Call, AccountDoc) -> + ExternalNumber = kz_json:get_ne_value([<<"caller_id">>, <<"external">>, <<"number">>], AccountDoc), + ExternalName = kz_json:get_ne_value([<<"caller_id">>, <<"external">>, <<"name">>], AccountDoc, Name), + + case is_valid_caller_id(ExternalNumber, Call) of 'true' -> - lager:info("determined valid account external caller id is <~s> ~s", [Name, External]), - {External, Name}; + ?LOG_INFO("determined valid account external caller id is <~s> ~s", [ExternalName, ExternalNumber]), + {ExternalNumber, ExternalName}; 'false' -> - maybe_get_account_default_number(Number, Name, Account, Call) + ?LOG_DEBUG("external number ~s not valid, trying default", [ExternalNumber]), + maybe_get_account_default_number(Number, ExternalName, Call, AccountDoc) end. --spec maybe_get_account_default_number(kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object(), kapps_call:call()) -> cid(). -maybe_get_account_default_number(Number, Name, Account, Call) -> - Default = kz_json:get_ne_value([<<"caller_id">>, <<"default">>, <<"number">>], Account), - case is_valid_caller_id(Default, Call) of +-spec maybe_get_account_default_number(kz_term:ne_binary(), kz_term:ne_binary(), kapps_call:call(), kzd_accounts:doc()) -> cid(). +maybe_get_account_default_number(Number, Name, Call, AccountDoc) -> + DefaultNumber = kz_json:get_ne_value([<<"caller_id">>, <<"default">>, <<"number">>], AccountDoc), + DefaultName = kz_json:get_ne_value([<<"caller_id">>, <<"default">>, <<"name">>], AccountDoc, Name), + + case is_valid_caller_id(DefaultNumber, Call) of 'true' -> - lager:info("determined valid account default caller id is <~s> ~s", [Name, Default]), - {Default, Name}; + ?LOG_INFO("determined valid account default caller id is <~s> ~s", [DefaultName, DefaultNumber]), + {DefaultNumber, DefaultName}; 'false' -> + ?LOG_DEBUG("default number ~s not valid, trying assigned numbers", [DefaultNumber]), maybe_get_assigned_number(Number, Name, Call) end. -spec maybe_get_assigned_number(kz_term:api_ne_binary(), kz_term:api_ne_binary(), kz_term:api_ne_binary()|kapps_call:call()) -> cid(). maybe_get_assigned_number(CandidateNumber, Name, ?MATCH_ACCOUNT_ENCODED(_)=AccountDb) -> - AccountId = kz_util:format_account_id(AccountDb), case knm_numbers:account_listing(AccountDb) of [_|_] = NumbersList -> Numbers = [Num - || {Num,JObj} <- NumbersList, - kz_json:get_value(<<"state">>, JObj) =:= ?NUMBER_STATE_IN_SERVICE + || {Num, JObj} <- NumbersList, + kz_json:get_ne_binary_value(<<"state">>, JObj) =:= ?NUMBER_STATE_IN_SERVICE ], case lists:member(CandidateNumber, Numbers) of - 'true' -> {CandidateNumber, Name}; - 'false' -> maybe_get_assigned_numbers(AccountId, Numbers, Name) + 'true' -> + ?LOG_DEBUG("using assigned number <~s> ~s", [Name, CandidateNumber]), + {CandidateNumber, Name}; + 'false' -> + AccountId = kz_util:format_account_id(AccountDb), + maybe_get_assigned_numbers(AccountId, Numbers, Name) end; - _ -> + _Else -> + ?LOG_DEBUG("failed to list account numbers: ~p", [_Else]), + AccountId = kz_util:format_account_id(AccountDb), Number = default_cid_number(AccountId), - lager:warning("no numbers available, proceed with <~s> ~s", [Name, Number]), + ?LOG_WARNING("no numbers available, proceed with <~s> ~s", [Name, Number]), {Number, Name} end; maybe_get_assigned_number(CandidateNumber, Name, Call) -> @@ -307,12 +320,12 @@ maybe_get_assigned_number(CandidateNumber, Name, Call) -> -spec maybe_get_assigned_numbers(kz_term:ne_binary(), kz_term:ne_binaries(), kz_term:ne_binary()) -> cid(). maybe_get_assigned_numbers(AccountId, [], Name) -> Number = default_cid_number(AccountId), - lager:info("failed to find any in-service numbers, using default <~s> ~s", [Name, Number]), + ?LOG_INFO("failed to find any in-service numbers, using default <~s> ~s", [Name, Number]), {Number, Name}; maybe_get_assigned_numbers(_AccountId, [Number|_], Name) -> %% This could optionally cycle all found numbers and ensure they valid %% but that could be a lot of wasted db lookups... - lager:info("using first assigned number caller id <~s> ~s", [Name, Number]), + ?LOG_INFO("using first assigned number caller id <~s> ~s", [Name, Number]), {Number, Name}. -spec is_valid_caller_id(kz_term:api_binary(), kapps_call:call()) -> boolean(). @@ -321,7 +334,9 @@ is_valid_caller_id(Number, Call) -> AccountId = kapps_call:account_id(Call), case knm_number:lookup_account(Number) of {'ok', AccountId, _} -> 'true'; - _Else -> 'false' + _Else -> + ?LOG_DEBUG("failed to find ~s in account ~s: ~p", [Number, AccountId, _Else]), + 'false' end. -spec maybe_get_presence_number(kz_json:object(), kapps_call:call()) -> kz_term:api_binary(). diff --git a/core/kazoo_endpoint/test/kz_attributes_tests.erl b/core/kazoo_endpoint/test/kz_attributes_tests.erl index 7c8669516c1..ed474740626 100644 --- a/core/kazoo_endpoint/test/kz_attributes_tests.erl +++ b/core/kazoo_endpoint/test/kz_attributes_tests.erl @@ -23,36 +23,24 @@ ,fun(C) -> kapps_call:set_authorizing_id(<<"trunkstore0000000000000000000002">>, C) end ]). +-define(DISA_CID_CALL, [fun(C) -> kapps_call:set_account_id(?FIXTURE_PARENT_ACCOUNT_ID, C) end + ,fun(C) -> kapps_call:set_caller_id_name(<<"disa">>, C) end + ,fun(C) -> kapps_call:set_caller_id_number(<<"+12225552600">>, C) end + ]). + kz_attributes_test_() -> - {setup - ,fun setup_db/0 - ,fun terminate_db/1 + {'setup' + ,fun kzd_test_fixtures:setup/0 + ,fun kzd_test_fixtures:cleanup/1 ,fun(_ReturnOfSetup) -> [test_get_flags_callflow() ,test_get_flags_trunkstore() ,test_process_dynamic_flags() + ,test_account_cid() ] end }. -setup_db() -> - ?LOG_DEBUG(":: Starting Kazoo FixtureDB"), - {ok, _} = application:ensure_all_started(kazoo_config), - kazoo_fixturedb:start(). - -terminate_db(Pid) -> - _DataLink = erlang:exit(Pid, normal), - Ref = monitor(process, Pid), - receive - {'DOWN', Ref, process, Pid, _Reason} -> - _KConfig = application:stop(kazoo_config), - ?LOG_DEBUG(":: Stopped Kazoo FixtureDB, data_link: ~p kazoo_config: ~p", [_DataLink, _KConfig]) - after 1000 -> - _KConfig = application:stop(kazoo_config), - ?LOG_DEBUG(":: Stopped Kazoo FixtureDB, data_link: timeout kazoo_config: ~p", [_KConfig]) - end. - - test_get_flags_callflow() -> Call = kapps_call_tests:create_callflow_call(), ExpectedOld = [<<"user_old_static_flag">> @@ -115,3 +103,16 @@ test_process_dynamic_flags() -> ,?_assertEqual([<<"local">>, <<"static">>], kz_attributes:process_dynamic_flags([<<"zone">>], [<<"static">>], Call)) } ]. + +test_account_cid() -> + Call = kapps_call_tests:create_callflow_call(), + DISACall = kapps_call:exec(?DISA_CID_CALL, Call), + {CIDNumber, CIDName} = kz_attributes:get_account_external_cid(DISACall), + + [{"account external caller id number chosen" + ,?_assertEqual(<<"+19995552600">>, CIDNumber) + } + ,{"account external caller id name chosen" + ,?_assertEqual(<<"account-external-name">>, CIDName) + } + ]. diff --git a/core/kazoo_endpoint/test/kz_endpoint_tests.erl b/core/kazoo_endpoint/test/kz_endpoint_tests.erl index 5d6724d648d..13a50ba11ae 100644 --- a/core/kazoo_endpoint/test/kz_endpoint_tests.erl +++ b/core/kazoo_endpoint/test/kz_endpoint_tests.erl @@ -135,7 +135,6 @@ device_undefined_user_undefined_account_on() -> DeviceDoc = kzd_devices:new(), Merged = kz_endpoint:merge_attribute(<<"call_recording">>, AccountDoc, DeviceDoc, UserDoc), - ?debugFmt("merged: ~p~n", [Merged]), check_expectations(?FUNCTION_NAME, Merged, Expectations). diff --git a/core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/docs/account0000000000000000000000003.json b/core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/docs/account0000000000000000000000003.json index 6386156aee7..6344e95200e 100644 --- a/core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/docs/account0000000000000000000000003.json +++ b/core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/docs/account0000000000000000000000003.json @@ -144,7 +144,7 @@ }, "external": { "name": "account-external-name", - "number": "account-external-number" + "number": "+19995552600" }, "internal": { "name": "account-internal-name", diff --git a/core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/views/phone_numbers+crossbar_listing.json b/core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/views/phone_numbers+crossbar_listing.json new file mode 100644 index 00000000000..8b874ec63cc --- /dev/null +++ b/core/kazoo_fixturedb/priv/dbs/account%2Fac%2Fco%2Funt0000000000000000000000003/views/phone_numbers+crossbar_listing.json @@ -0,0 +1,9 @@ +[ + { + "id": "+19995552600", + "key": "+19995552600", + "value": { + "assigned_to": "account0000000000000000000000003" + } + } +] diff --git a/core/kazoo_fixturedb/priv/dbs/numbers%2F%2B1999/docs/%2B19995552600.json b/core/kazoo_fixturedb/priv/dbs/numbers%2F%2B1999/docs/%2B19995552600.json new file mode 100644 index 00000000000..5862808a58e --- /dev/null +++ b/core/kazoo_fixturedb/priv/dbs/numbers%2F%2B1999/docs/%2B19995552600.json @@ -0,0 +1,7 @@ +{ + "_id": "+19995552600", + "pvt_assigned_to": "account0000000000000000000000003", + "pvt_created": 63752475360, + "pvt_modified": 63752475360, + "pvt_state": "in_service" +} diff --git a/core/kazoo_number_manager/Makefile b/core/kazoo_number_manager/Makefile index 52ed62d6b71..bf405533182 100644 --- a/core/kazoo_number_manager/Makefile +++ b/core/kazoo_number_manager/Makefile @@ -38,6 +38,7 @@ compile-test: compile-also compile-also: $(MAKE) compile-test -C $(ROOT)/core/kazoo_config/ $(MAKE) compile-test -C $(ROOT)/core/kazoo_apps/ + $(MAKE) compile-test -C $(ROOT)/core/kazoo_data/ include $(ROOT)/make/kz.mk diff --git a/core/kazoo_number_manager/src/knm_number.erl b/core/kazoo_number_manager/src/knm_number.erl index e5a78c2dd03..f01d1249b7e 100644 --- a/core/kazoo_number_manager/src/knm_number.erl +++ b/core/kazoo_number_manager/src/knm_number.erl @@ -385,8 +385,10 @@ lookup_account(Num) -> fetch_account_from_number(Num) -> case knm_phone_number:fetch(Num) of - {'ok', PN} -> check_number(PN); - {'error', _}=Error -> maybe_fetch_account_from_ports(Num, Error) + {'ok', PN} -> + check_number(PN); + {'error', _}=Error -> + maybe_fetch_account_from_ports(Num, Error) end. check_number(PN) -> diff --git a/core/kazoo_number_manager/src/knm_phone_number.erl b/core/kazoo_number_manager/src/knm_phone_number.erl index 74f298147a2..2911037f9da 100644 --- a/core/kazoo_number_manager/src/knm_phone_number.erl +++ b/core/kazoo_number_manager/src/knm_phone_number.erl @@ -205,7 +205,7 @@ from_number_with_options(DID, Options) -> -spec fetch(kz_term:ne_binary() | knm_numbers:collection()) -> knm_phone_number_return() | knm_numbers:collection(). -fetch(?NE_BINARY=Num) -> +fetch(<>) -> fetch(Num, knm_number_options:default()); fetch(T0=#{todo := Nums, options := Options}) -> Pairs = group_by_db(lists:usort(knm_converters:normalize(Nums))), @@ -466,8 +466,13 @@ test_fetch(?TEST_PORT_IN_NUM) -> test_fetch(?TEST_PORT_IN2_NUM) -> {'ok', ?PORT_IN2_NUMBER}; test_fetch(?TEST_PORT_IN3_NUM) -> {'ok', ?PORT_IN3_NUMBER}; -test_fetch(_DID=?NE_BINARY) -> - {'error', 'not_found'}. +test_fetch(<>) -> + NormalizedNum = knm_converters:normalize(DID), + NumberDb = knm_converters:to_db(NormalizedNum), + try kz_datamgr:open_cache_doc(NumberDb, NormalizedNum) + catch _:_ -> {'error', 'not_found'} + end. + -else. -spec fetch(kz_term:ne_binary(), knm_number_options:options()) -> knm_phone_number_return().